Power shell 重启IIS

最近根据项目需要写了一段power shell的代码 ,主要功能是批量重启IIS 具体的 Power shell 服务如下:

write-output 'Restarting IIS servers ................ '
$servers= 'server1' ,'Server2'
$username='用户名'
$password = ConvertTo-SecureString -String '密码' -AsPlainText -Force
$credential =New-Object System.Management.Automation.PSCredential -argumentlist $username ,$password

foreach($server in $servers){
$message= 'Restarting IIS server : '+$server
write-output $message
$sessions=New-PSSession -ComputerName $server -credential $credential
Invoke-Command –Session $sessions -scriptBlock {restart-Service w3svc}
$message= 'IIS server Restarted : '+$server
write-output $message
}
write-output 'IIS servers Restarted ...................'

系统执行的逻辑是: 

1用$credential =New-Object System.Management.Automation.PSCredential -argumentlist $username ,$password登录到远程服务器上

2 运用命令Invoke-Command –Session $sessions -scriptBlock {restart-Service w3svc} 逐个重启IIS Server

 

posted on 2017-08-23 14:36  风云无敌  阅读(638)  评论(0编辑  收藏

导航

统计