How to stop IIS using PowerShell?

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?

+6
source share
4 answers

Try using PsExec \ Server2 -u Administrator -p somePassword IISReset / STOP

or using PowerGUI Script Editor

 $service = Get-WmiObject -computer 'ServerA' Win32_Service -Filter "Name='IISAdmin'" $service $service.InvokeMethod('StopService',$Null) $service.State 

Try using

 $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() 
+2
source

You can stop / restart iis remotely using iisreset:

 iisreset [computername] 
+7
source

To stop IIS (for example, "rightclick> stop" on the server in iis-Manager):

 spsv iisadmin,was,w3svc -pa 

means:

 stop-service -name iisadmin,was,w3svc -passThru # -passThru is optional but basically outputs the result 

(alternative: gsv iisadmin,was,w3svc |spsv -pa|sasv -pa to restart the service and print the result, but in this example JUST the second -pa is optional !!! FYI: gsv = get-service, sasv = start -service, spsv = stop-service, pa = passThru)

+5
source
 Invoke-Command -ComputerName myserver { Stop-Service W3SVC} 
+2
source

Source: https://habr.com/ru/post/916498/


All Articles