{"id":2124,"date":"2017-05-16T21:11:22","date_gmt":"2017-05-16T21:11:22","guid":{"rendered":"http:\/\/microsoftgeek.com\/?p=2124"},"modified":"2017-05-16T21:11:22","modified_gmt":"2017-05-16T21:11:22","slug":"restart-a-service-remotely-using-powershell-on-multiple-servers","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=2124","title":{"rendered":"Restart a Service Remotely Using PowerShell on Multiple Servers"},"content":{"rendered":"<p>I threw a quick PowerShell function together that would allow me to restart one service on 1 or more servers in one command.\u00a0 I hadn\u2019t found an easy way to do this in PowerShell before so I threw this together.\u00a0 What I did was pasted the function code into my PowerShell profile at c:\\users\\%USERNAME%\\Documents\\WindowsPowershell\\Microsoft.PowerShell_profiles.ps1 to ensure it was loaded each time I started my PowerShell window.\u00a0 The syntax of the script is simple:<\/p>\n<p>&nbsp;<\/p>\n<div id=\"codeSnippetWrapper\">\n<div id=\"codeSnippet\">\n<pre>Restart-MultipleServerServices -ServiceName MSExchangeTransport -ServerNames Server1,Server2<\/pre>\n<\/div>\n<\/div>\n<p>The service name is a simple string, and should be the short name of the service.\u00a0 The ServerNames option should be a comma separated list of servers that you want to restart the service on.\u00a0 It can be any number of servers, meaning you can use this to remotely stop the service on just 1 servers or 100.\u00a0 The output of the service being restarted looks like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"http:\/\/port25guy.com\/wp-content\/uploads\/2014\/04\/image_thumb.png\" alt=\"image\" width=\"926\" height=\"115\" border=\"0\" \/><\/p>\n<p>Copy the below script:<\/p>\n<div class=\"csharpcode\">\n<div id=\"codeSnippetWrapper\" class=\"csharpcode-wrapper\">\n<div id=\"codeSnippet\" class=\"csharpcode\">\n<div id=\"codeSnippetWrapper\">\n<div id=\"codeSnippet\">\n<pre>function Restart-MultipleServerServices([string]$ServiceName, [array]$ServerNames)<\/pre>\n<pre>{<\/pre>\n<pre><\/pre>\n<pre>foreach ($i in $ServerNames)<\/pre>\n<pre>{<\/pre>\n<pre>$service = Get-Service -ComputerName \"$i\" -name $ServiceName<\/pre>\n<pre>    $service.stop()<\/pre>\n<pre>    do { Start-sleep -Milliseconds 200}<\/pre>\n<pre>    until ((Get-Service -ComputerName \"$i\" -Name $ServiceName).status -eq 'Stopped')<\/pre>\n<pre>    Write-Host \"Attempting to Stop Service $($ServiceName) on Server $i\" -ForegroundColor Green<\/pre>\n<pre>    Start-Sleep 10<\/pre>\n<pre>    $service.start()<\/pre>\n<pre>    do { Start-sleep -Milliseconds 200}<\/pre>\n<pre>    until ((Get-Service -ComputerName \"$i\" -Name $ServiceName).status -eq 'Running')<\/pre>\n<pre>    Write-Host \"Attempting to Start Service $($ServiceName) on Server $i\" -ForegroundColor Green<\/pre>\n<pre><\/pre>\n<pre>    }<\/pre>\n<pre>    }<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Into your PowerShell profile at the below path.\u00a0 If the PS1 file isn\u2019t there, you can manually create it:<\/p>\n<div id=\"codeSnippetWrapper\">\n<div id=\"codeSnippet\">\n<pre><span id=\"lnum1\">   1:<\/span> C:\\Users\\%USERNAME%\\Documents\\WindowsPowershell\\Microsoft.PowerShell_profile.ps1<\/pre>\n<\/div>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"http:\/\/port25guy.com\/wp-content\/uploads\/2014\/04\/image_thumb2.png\" alt=\"image\" width=\"720\" height=\"340\" border=\"0\" \/><\/p>\n<p>Save and close the file and you should be ready to rock!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I threw a quick PowerShell function together that would allow me to restart one service on 1 or more servers in one command.\u00a0 I hadn\u2019t found an easy way to do this in PowerShell before so I threw this together.\u00a0 What I did was pasted the function code into my PowerShell profile at c:\\users\\%USERNAME%\\Documents\\WindowsPowershell\\Microsoft.PowerShell_profiles.ps1 to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-2124","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2124"}],"version-history":[{"count":1,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2124\/revisions"}],"predecessor-version":[{"id":2125,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2124\/revisions\/2125"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}