Power shell 重启IIS

发布日期:2018-02-24    浏览次数:482

最近根据项目需要写了一段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

 

本文网址:https://www.wyxxw.cn/blog-detail-22-24-430.html

返回列表

非特殊说明,本文版权归原作者所有,转载请注明出处

提示:本站所有资源仅供学习与参考,请勿用于商业用途。图片来自互联网~如侵犯您的权益,请联系QQ:1067507709.

提示:转载请注明来自:https://www.cnblogs.com/jimfighting/p/7418033.html 。 转载人:momo