11

I have two servers Server A and Server B. I want to stop server A from Server B remotely using Powershell script.

halfer
  • 19,824
  • 17
  • 99
  • 186
Selwyn
  • 1,621
  • 6
  • 21
  • 38
  • We need some more information about the configuration of your domain. Are both servers operating in the same domain that a single user account has equivalent permissions on the server? – Chris Marisic Aug 31 '09 at 12:55
  • Also this question should probably be moved to server fault. – Chris Marisic Aug 31 '09 at 13:00

6 Answers6

14

One of the simplest ways to do this is really with just a command line execution using PsExec. And send over to the machines

IISReset /STOP or /START or /RESTART

So you'd do something like this

PsExec \\Server2 -u Administrator -p somePassword IISReset /STOP

Just be careful with password management if you go this route or any route that involves some type of admin level account impersonation so that no one can get a plain text copy of the admin password.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
13

Option 1:

iisreset remotepcname /restart

Option 2:

(Get-Service -ComputerName remotepc -Name 'IISAdmin').stop()

Option 3:

Invoke-Command -ComputerName remotepc -ScriptBlock {iisreset}
SysDragon
  • 9,692
  • 15
  • 60
  • 89
shaiju
  • 131
  • 1
  • 2
  • All three fail for me. 1) The term 'iisreset' is not recognized as the name of a cmdlet..., 2) Cannot find any service with service name 'IISAdmin', 3) ...The WinRM client cannot process the request.... – steve Feb 16 '21 at 10:25
10

Because you asked for Powershell:

(Get-WmiObject Win32_Service -ComputerName ServerA -Filter "Name='iisadmin'").InvokeMethod("StopService", $null) 

Agreed this question should be moved to ServerFault.

Greg
  • 23,155
  • 11
  • 57
  • 79
fenster
  • 1,809
  • 6
  • 22
  • 24
3
$service = Get-WmiObject -computer 'ServerA' Win32_Service -Filter "Name='IISAdmin'"
$service
$service.InvokeMethod('StopService',$Null)
start-sleep -s 5
$service.InvokeMethod('StartService',$Null)
start-sleep -s 5
$service.State

$service = Get-WmiObject -computer 'ServerB' Win32_Service -Filter "Name='IISAdmin'"
$service
$service.InvokeMethod('StopService',$Null)
start-sleep -s 5
$service.InvokeMethod('StartService',$Null)
start-sleep -s 5
$service.State
sjngm
  • 12,423
  • 14
  • 84
  • 114
2

In powershell 2.0, run the following from cmd prompt:

invoke-command -computername <yourremoteservername> -scriptblock {iisreset}
kailash
  • 21
  • 1
0

You can use get-wmiobject cmdlt with different NameSpace for different versions of IIS v6 or v7, below pipelining command can be used for such operations in IIS locally or remotely

for IIS v6

$srv = "Server Name or IP Address"

$app = "Name of App Pool"

$x = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" -ComputerName $srv -Authentication PacketPrivacy | where-object {$_.Name -eq "W3SVC/AppPools/$app"}

$x.Stop()

$x.Start()

for IIS v7

$srv = "Server Name or IP Address"

$app = "Name of App Pool"

$x = Get-WMIObject -Namespace "root\webAdministration" -Class "ApplicationPool" -ComputerName $srv -Authentication PacketPrivacy | Where-Object {$_.Name -eq $app}

$x.Stop()

$x.Start()

you need to have sufficient account privilege for these operations, event though i prefer to do $x.Recycle() for my websites.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
Abdullah Shahin
  • 1,002
  • 15
  • 19