I am learning PowerShell and trying to stop IIS on a remote server.
I am using the PowerGUI Script editor, which I started in admin mode.
I have this code
$service = Get-WmiObject Win32_Service -ComputerName 'myserver' -Filter "Name='IISAdmin'" $service $service.StopService(); $service.State
The state always returns as working. I do not know why this does not stop.
Edit
Startup error
Invoke-Command -ComputerName 'myserver' { Stop-Service IISAdmin }
Error connecting to the remote server with the following error message: The client cannot connect to the recipient specified in the request. Verify that the service at the destination is running and accepting requests. Consult logs and documentation. The intended WS-Management service, most often IIS or WinRM. If the destination is the WinRM service, run the following command at the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see about_Remote_Troubleshooting Help.
Edit2
I found this and it seemed to work. I donβt know how to return information from Stop-Service, so I had to use a different way to get it for 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
It works. I do not understand why. The only thing I can come up with was to use -Force, since this would not stop the server otherwise, maybe this is the reason?
source share