PowerShell+WinRM 批量远程操作

将所有计算机添加到受信任主机列表

set-item wsman:localhost\client\trustedhosts -Force -value *

远程批量读取c:\c.txt

$account = "zhangsan"  
$password = '!@#$%54321'  
$secpwd = convertto-securestring $password -asplaintext -force  
$cred = new-object System.Management.Automation.PSCredential -argumentlist $account,$secpwd
invoke-command -computername 10.12.1.1,10.12.1.2 -Credential $cred -ScriptBlock {$file = Get-Content "c:\c.txt";Write-Host "result: "$file}

批量远程执行脚本

invoke-command -comp (get-content servers.txt) -filepath c:\scripts\sample.ps1 -Credential $cred
invoke-command -comp (get-content servers.txt) -filepath c:\scripts\sample.ps1 -Credential test\zhangsan
invoke-command -comp (get-content servers.txt) -ScriptBlock {get-hotfix|select HotFixID} -Credential test\zhangsan
invoke-command -comp (get-content servers.txt) -ScriptBlock {Get-WmiObject -class Win32_OperatingSystem|select lastbootuptime} -Credential test\zhangsan
posted @ 2022-03-09 16:41  shenshu  阅读(204)  评论(0)    收藏  举报