{"id":7891,"date":"2015-02-12T00:01:00","date_gmt":"2015-02-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/02\/12\/dont-learn-powershell-reaching-out\/"},"modified":"2019-02-18T10:30:39","modified_gmt":"2019-02-18T17:30:39","slug":"dont-learn-powershell-reaching-out","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/dont-learn-powershell-reaching-out\/","title":{"rendered":"Don&#8217;t Learn PowerShell: Reaching Out"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about running commands on remote servers.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! One of the things I really don&rsquo;t understand is why Windows PowerShell is broken. I look at commands like you suggest, but they don&rsquo;t work. Specifically, I wanted to see the status of services on my servers, but the commands just don&rsquo;t work. What&rsquo;s up with that?<\/p>\n<p>&mdash;BB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning it is cold and raining, which would not be unusual winter weather for Charlotte, North Carolina&mdash;except the weather app on Bing said it was going to be sunny all day. It seems that <a href=\"http:\/\/en.wikipedia.org\/wiki\/Punxsutawney_Phil\">Punxsutawney Phil<\/a> is more accurate&mdash;and he is just a gopher. I wonder, at times, why the weather forecast remains a mystery and isn&#039;t updated. I can look outside and see that it is raining, and yet the app says sunshine. Hmmm&#8230;<\/p>\n<p>BB, one thing that is not a mystery is why Windows PowerShell won&rsquo;t work. There is always a reason, and in your case, there are a few likely suspects:<\/p>\n<ol>\n<li><b>Rights<\/b>. <br \/> The account you are using to make your remote command may not have rights on the destination server.<\/li>\n<li><b>Firewall<\/b>. <br \/> The port used by the command you are attempting to run may be blocked at the firewall.<\/li>\n<li><b>Services<\/b>. <br \/> Some commands rely on services, such as the Remote Registry Service or others that may not be running by default.<\/li>\n<\/ol>\n<p>Because of these issues, I have pretty much quit using commands such as <b>Get-Service &ndash;ComputerName MyRemoteServer<\/b>. The command will fail by default unless you have made configuration changes on your servers&mdash;and I would not recommend making such changes due to the fact that you would be widening the attack surface on your servers.<\/p>\n<h2>A simple change<\/h2>\n<p>The solution is simple: change your command&#8230;just a little bit.<\/p>\n<p>I add the command <b>Invoke-Command<\/b>. When I use <b>Invoke-Command<\/b>, the command uses WinRM (which is enabled by default on Windows Server 2012 R2. Windows PowerShell uses WinRM for remoting commands. The great thing about this is that it is already enabled on servers, and I do not need to do anything.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> If WinRm is not enabled, I use the <b>Enable-PSRemoting<\/b> command. I can test if WinRM is enabled and configured properly by using the <b>Test-WsMan<\/b> command.<\/p>\n<p>The command to check the status of the BITS service on a remote server named <b>S1<\/b>, so the command would therefore be:<\/p>\n<p style=\"margin-left:30px\">Invoke-Command -ComputerName S1 { Get-Service bits }<\/p>\n<p>If I need to check the status of the BITS service on several remote servers, I can separate the computer names with a comma. The following command checks the status of the BITS service on the servers named S1, S2, and SGW.<\/p>\n<p style=\"margin-left:30px\">Invoke-Command -ComputerName S1, S2, SGW { Get-Service bits }<\/p>\n<p>The cool thing is that Windows PowerShell automatically adds a column named PSComputerName to the output. This is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-12-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-12-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I need to add different credentials, I use the <b>&ndash;Credential<\/b> parameter. This causes a dialog box to appear that prompts me to type my password. Here is the command:<\/p>\n<p style=\"margin-left:30px\">Invoke-Command -ComputerName S1, S2, SGW&nbsp; -Credential nwtraders\\administrator {&nbsp;<span style=\"font-size:12px\">Get-Service bits }<\/span><\/p>\n<p>The dialog box is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-12-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-12-15-02.png\" alt=\"Image of dialog box\" title=\"Image of dialog box\" \/><\/a><\/p>\n<p>BB, that is all there is to using Windows PowerShell against remote servers. Don&rsquo;t Learn Windows PowerShell Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about running commands on remote servers. &nbsp;Hey, Scripting Guy! One of the things I really don&rsquo;t understand is why Windows PowerShell is broken. I look at commands like you suggest, but they don&rsquo;t work. Specifically, I wanted to see the status of services on my servers, but the [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,45],"class_list":["post-7891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about running commands on remote servers. &nbsp;Hey, Scripting Guy! One of the things I really don&rsquo;t understand is why Windows PowerShell is broken. I look at commands like you suggest, but they don&rsquo;t work. Specifically, I wanted to see the status of services on my servers, but the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7891"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=7891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}