From: https://www.aliyun.com/jiaocheng/871477.html
Write-output'restarting IIS servers ....... .....'$servers='Server1','Server2'$username='User name'$password= Convertto-securestring-string'Password'-asplaintext-force$credential=new-object System.Management.Automation.PSCredential-argumentlist $username, $passwordforeach($serverinch$servers) {$message='restarting IIS server:'+$serverwrite-Output $message $sessions=new-pssession-computername $server-Credential $credentialInvoke-command–session $sessions-scriptblock {restart-Service w3svc} $message='IIS Server restarted:'+$serverwrite-Output $message}write-output'IIS servers restarted ......... .....'
To restart the native IIS
Invoke-command-scriptblock {restart-service w3svc}
Restart a service (and exclude Net Logon)
Restart-service WEBMVC
' Net Logon '
Reference: https://www.jb51.net/article/53349.htm
Https://www.itstrike.cn/Question/264def6e-57be-40d1-a2d0-c8a2afe6f6eb.html
To view the PowerShell version:
$psversiontable
Reference: https://www.jb51.net/article/53150.htm
Go [Powershell]ps Performing a restart of IIS