Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Stop IIS with PowerShell?

Tags:

powershell

iis

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?

like image 833
chobo2 Avatar asked May 23 '12 21:05

chobo2


1 Answers

Invoke-Command -ComputerName myserver { Stop-Service W3SVC}
like image 59
David Brabant Avatar answered Oct 20 '22 11:10

David Brabant