{"id":3411,"date":"2009-10-31T12:28:00","date_gmt":"2009-10-31T12:28:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/10\/31\/quick-dirty-super-useful-scripting\/"},"modified":"2019-02-18T13:12:23","modified_gmt":"2019-02-18T20:12:23","slug":"quick-dirty-super-useful-scripting","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/quick-dirty-super-useful-scripting\/","title":{"rendered":"Quick, Dirty, Super-Useful Scripting"},"content":{"rendered":"<p>Last weekend I installed the super-awesome W7 Ultimate on all the machines at home.&nbsp; This weekend I decided to install the <a href=\"http:\/\/www.microsoft.com\/windows\/virtual-pc\/download.aspx\">XP Virtual mode download<\/a>.&nbsp; I started to do this and noticed that it was 500MB.&nbsp; That is large and is going to take a long time FOR EACH PC I have.&nbsp; One of the reasons I got the Ultimate SKU is that it has <a href=\"http:\/\/technet.microsoft.com\/en-us\/network\/dd425028.aspx\">BranchCache<\/a>.&nbsp; This is EXACTLY what Branchcache was designed to do.&nbsp; One PC downloads something and puts it into a distributed cache and then when the other PCs go to download it, they get it from their peers instead of from the internet (given the way my son reacts when my downloads affects the network and&nbsp; his ability to kill Nazi Zombies, I\u2019m motivated to minimize the usage of the network \ud83d\ude42 ).<\/p>\n<p>That sounds great but the Branchcache is not enabled by default so you have to set it up.&nbsp; I was doing this and it was a pain in the butt so I decided to experiment with a quick and dirty script and I LOVE the results so I thought I would share.<\/p>\n<p>The issue is that they way you configure Branchcache is with NETSH.&nbsp; The problem with that is that NETSH isn\u2019t remotable.&nbsp; I was going back and forth between machines trying things out.&nbsp; I gave myself a dopeslap and I realized what I was doing and established a remote PowerShell session to the machines.&nbsp; For a while I was doing Enter-PSSession which gives you an interactive session to the remote machine.&nbsp; That was great but I have more than one machine so I either had to switch machines or have a couple of windows open.&nbsp; Then I decided I would use Invoke-Command.&nbsp; I created a remote connection to all the machines and then collected them in a variable $s and used ICM.<\/p>\n<p>PS&gt; $s = GSN <br \/>PS&gt; icm $s {netsh branchcache show localcache}<\/p>\n<p>That worked great but it was a little clumsy to work with so I decided to write a quick-n-dirty function to make my life better. <\/p>\n<p>function b { icm $s -ScriptBlock $([scriptblock]::create(&#8220;netsh branchcache $args &#8220;))}<\/p>\n<p>Notice that I\u2019m not using VERB-NOUN.&nbsp; Why should I?&nbsp; I\u2019m after FAST (minimal typing) and this is a throw away function.&nbsp; \u201cB\u201d is perfect.<\/p>\n<p>Notice that I didn\u2019t declare parameters.&nbsp; Why should I?&nbsp; I\u2019m going quick.&nbsp; I need it to work but I don\u2019t need it to be formal.<\/p>\n<p>Notice that I use positional parameters and aliases?&nbsp; Why not?&nbsp; <\/p>\n<p>Now what I\u2019m able to do is:<\/p>\n<p>PS&gt; b show localcache <br \/>PS&gt; b set service mode=DISTRIBUTED<\/p>\n<p>&lt;<strong>UPDATE<\/strong>&gt;<br \/><em>I realized from the comments that I didn&#8217;t deliver the punchline.&nbsp; The WHOLE point was that the command is going to run on ALL the machines!&nbsp; THAT&#8217;s the crazy wonderful part of it.<br \/><\/em>&lt;\/<strong>UPDATE<\/strong>&gt;<\/p>\n<p>I haven\u2019t got Branchcache working yet but I\u2019m am zooming and having a blast.&nbsp;&nbsp; We spend a lot of time talking about best practices and formal scripting.&nbsp; That is all super great and super important but there is NOTHING wrong with quick-n-dirty scripting for <strong>adhoc<\/strong> functions.&nbsp; The fact that we designed it so that you can do both with PowerShell is a source of great pride.<\/p>\n<p>GOD I LOVE POWERSHELL!<\/p>\n<p>Experiment!&nbsp; Enjoy!&nbsp; Engage! <\/p>\n<p>Jeffrey Snover [MSFT] <br \/>Distinguished Engineer <br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a> <br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last weekend I installed the super-awesome W7 Ultimate on all the machines at home.&nbsp; This weekend I decided to install the XP Virtual mode download.&nbsp; I started to do this and noticed that it was 500MB.&nbsp; That is large and is going to take a long time FOR EACH PC I have.&nbsp; One of the [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Last weekend I installed the super-awesome W7 Ultimate on all the machines at home.&nbsp; This weekend I decided to install the XP Virtual mode download.&nbsp; I started to do this and noticed that it was 500MB.&nbsp; That is large and is going to take a long time FOR EACH PC I have.&nbsp; One of the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3411"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3411\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}