I am learning PowerShell and trying to stop IIS on a remote server.
I am using PowerGUI Script Editor that I started in admin mode.
I have this code
$service = Get-WmiObject Win32_Service -ComputerName 'myserver' -Filter "Name='IISAdmin'"
$service
$service.StopService();
$service.State
State always comes back as running. I don't know why it does not stop.
Edit
Error I get when running
Invoke-Command -ComputerName 'myserver' { Stop-Service IISAdmin }
Connecting to remote server failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
Edit2
I found this and it seems to work. I don't know how to get the information back from Stop-Service so I had to use the other way to get it to me. If you know how please let me know.
Stop-Service -Force -InputObject $(Get-Service -Computer myserver -Name IISAdmin)
$service = Get-WmiObject Win32_Service -ComputerName 'myserver ' -Filter "Name='IISAdmin'"
$service.State
This works. I don't understand why. The only thing I can think off is I had to use -Force as it would not stop the server otherwise so maybe that is a reason?
Invoke-Command -ComputerName myserver { Stop-Service W3SVC}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With