开放远程配置
winrm quickconfig
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force
确认防火墙放行WinRM 相关的规则(例如“Windows Remote Management”)
或者允许对应的端口(5985和5986)的流量
远程登陆+回收脚本
$username = '帐号'
$password = '密码'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
Invoke-Command -ComputerName "服务器IP" -Credential $credential -ScriptBlock {
$machine = 'localhost' # 这里是已经远程登陆成功, 使用localhost
$appPool = '池名称'
$path = "IIS://" + $machine + "/W3SVC/AppPools/" + $appPool
$w3svc = New-Object System.DirectoryServices.DirectoryEntry($path)
try {
$w3svc.Invoke("Recycle", $null)
} catch {
echo $_.Exception.Message
exit 1
}
}