我有两个服务器,服务器A和服务器B.我想远程使用PowerShell脚本从服务器B停止服务器A。
Answer 1:
一要做到这一点最简单的方法是真的,只需使用命令行执行PSEXEC 。 并发送过来的机器
IISRESET / STOP或/ START或/ RESTART
所以,你会做这样的事
PsExec \\Server2 -u Administrator -p somePassword IISReset /STOP
只是要小心,密码管理,如果你走这条路线,或者涉及到某些类型的管理员级别的帐户,模拟,以便没有人能够获得管理员密码的明文副本的任何途径。
Answer 2:
选项1:
iisreset remotepcname /restart
选项2:
(Get-Service -ComputerName remotepc -Name 'IISAdmin').stop()
方案3:
Invoke-Command -ComputerName remotepc -ScriptBlock {iisreset}
Answer 3:
因为你问PowerShell的:
(Get-WmiObject Win32_Service -ComputerName ServerA -Filter "Name='iisadmin'").InvokeMethod("StopService", $null)
同意这个问题应移至ServerFault。
Answer 4:
$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
Answer 5:
在2.0的powershell,运行从命令提示符以下内容:
invoke-command -computername <yourremoteservername> -scriptblock {iisreset}
Answer 6:
您可以使用不同的命名空间为不同版本的IIS V6或V7的获得-WmiObject可以cmdlt,下面流水线命令可用于在IIS中这样的操作在本地或远程
对于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()
你需要为这些业务充分考虑特权,事件虽然我喜欢为我的网站做$ x.Recycle()。
文章来源: How To start/stop IIS 6.0/7.0 remotely using PowerShell Scripts?