简体   繁体   中英

How To start/stop IIS 6.0/7.0 remotely using PowerShell Scripts?

我有两个服务器服务器A和服务器B.我想使用Powershell脚本远程从服务器B停止服务器A.

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.

Option 1:

iisreset remotepcname /restart

Option 2:

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

Option 3:

Invoke-Command -ComputerName remotepc -ScriptBlock {iisreset}

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.

$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

在powershell 2.0中,从cmd提示符运行以下命令:

invoke-command -computername <yourremoteservername> -scriptblock {iisreset}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM