我正在学习PowerShell并尝试在远程服务器上停止IIS.
我正在使用我在管理模式下启动的PowerGUI脚本编辑器.
我有这个代码
$service = Get-WmiObject Win32_Service -ComputerName 'myserver' -Filter "Name='IISAdmin'"
$service
$service.StopService();
$service.State
Run Code Online (Sandbox Code Playgroud)
国家总是回来跑步.我不知道它为什么不停止.
编辑
运行时出错
Invoke-Command -ComputerName 'myserver' { Stop-Service IISAdmin }
Run Code Online (Sandbox Code Playgroud)
连接到远程服务器失败,并显示以下错误消息:客户端无法连接到请求中指定的目标.验证目标上的服务是否正在运行并且正在接受请求.查阅目标上运行的WS-Management服务的日志和文档,最常见的是IIS或WinRM.如果目标是WinRM服务,请在目标上运行以下命令以分析和配置WinRM服务:"winrm quickconfig".有关详细信息,请参阅about_Remote_Troubleshooting帮助主题.
EDIT2
我发现了这个,似乎有效.我不知道如何从Stop-Service获取信息,所以我不得不用另一种方式来获取它.如果您知道如何请告诉我.
Stop-Service -Force -InputObject $(Get-Service -Computer myserver -Name IISAdmin)
$service = Get-WmiObject Win32_Service -ComputerName 'myserver ' -Filter "Name='IISAdmin'"
$service.State
Run Code Online (Sandbox Code Playgroud)
这有效.我不明白为什么.我唯一可以想到的是我必须使用-Force,因为它不会停止服务器,否则也许这是一个原因?
停止IIS(如iis-Manager中服务器上的"rightclick> stop"):
spsv iisadmin,was,w3svc -pa
Run Code Online (Sandbox Code Playgroud)
手段:
stop-service -name iisadmin,was,w3svc -passThru # -passThru is optional but basically outputs the result
Run Code Online (Sandbox Code Playgroud)
(替代方法:gsv iisadmin,was,w3svc |spsv -pa|sasv -pa重新启动服务并输出结果但在此示例中,第二个-pa是可选的!FYI:gsv = get-service,sasv = start-service,spsv = stop-service,pa = passThru)
Invoke-Command -ComputerName myserver { Stop-Service W3SVC}
Run Code Online (Sandbox Code Playgroud)
小智 5
还有另一种简单的方法如何停止 IIS = 仅从 Powershell 运行
iisreset /stop
Run Code Online (Sandbox Code Playgroud)
要启动 IIS,只需
iisreset /start
Run Code Online (Sandbox Code Playgroud)
尝试使用 PsExec \Server2 -u Administrator -p somePassword IISReset /STOP
或使用 PowerGUI 脚本编辑器
$service = Get-WmiObject -computer 'ServerA' Win32_Service -Filter "Name='IISAdmin'"
$service
$service.InvokeMethod('StopService',$Null)
$service.State
Run Code Online (Sandbox Code Playgroud)
尝试使用
$server = "servername"
$siteName = "Default Web Site"
$iis = [ADSI]"IIS://$server/W3SVC"
$site = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND
$_.ServerComment -eq $siteName }
$site.serverstate=3
$site.setinfo()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28575 次 |
| 最近记录: |