对服务端机器(被远程的机器),除过termservice服务正常运行、远程端口正常监听外,还要额外以管理员身份执行一句powershell命令:
winrm quickconfig -q 2>&1> $null;winrm quickconfig -q -force 2>&1> $null;netstat -ano|findstr :5985;
确保5985是监听的才行。
以下代码兼容server2008r2/2012r2/2016/2019/2022
对客户端机器,如果5985端口不在会报错
powershell命令:
reg delete "HKLM\SOFTWARE\Policies\Microsoft\Windows\WinRM" /f 2>&1 >$null
#stop-service mpssvc 2>&1 > $null
winrm quickconfig -q 2>&1 > $null
winrm quickconfig -q -force 2>&1 > $null
restart-service winrm 2>&1 > $null
#Set-Item WSMan:localhost\client\trustedhosts -value * -force 2>&1 > $null
winrm set winrm/config/client '@{TrustedHosts="*"}' 2>&1 > $null
netstat -ato|findstr :5985
Set-Item WSMan:localhost\client\trustedhosts -value * -Force
如果上面这段代码配置winrm报错-2147024894 0x80070002就是得装补丁,参考我整理的文档https://cloud.tencent.com/developer/article/2043723
先打全补丁,参考我这篇文档里微软的更新脚本来更新:https://cloud.tencent.com/developer/article/2345790
然后检查注册表,删掉HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WinRM
Registry Path:HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WinRM
powershell:reg delete "HKLM\SOFTWARE\Policies\Microsoft\Windows\WinRM" /f 2>&1 >$null
cmd:reg delete "HKLM\SOFTWARE\Policies\Microsoft\Windows\WinRM" /f 2>&1 >nul
powershell批量远程示例,批量给多台机器安装dotnet和iis:
$Username = 'Administrator'
$Password = '明文密码'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
$iparray = @('172.21.66.32','172.21.65.41','172.21.65.162')
for($i=0;$i -lt $iparray.Length;$i++){
"`$iparray["+$i+"]="+$iparray[$i]+"`n"
Invoke-Command -ComputerName $iparray[$i] -Credential $Cred -ScriptBlock {
Get-WindowsFeature -Name NET-*, Web-* | where {$_.Name -notmatch "Ftp|Web-Application-Proxy"} | Install-WindowsFeature;
}
}
上面黑体的部分就是要批量下发的指令,多行的话用英文分行隔开,比如要批量下发停止云监控组件和禁止云监控组件开机运行的命令
sc.exe stop BaradAgentSvc 2>$null 1>$null;sc.exe stop StargateSvc 2>$null 1>$null;sc.exe config BaradAgentSvc start= disabled 2>$null 1>$null;sc.exe config StargateSvc start= disabled 2>$null 1>$null;
如果是批量删除C:\Program Files\QCloud\Monitor\Barad\ntpdate.exe
那就批量下发
del C:\progra~1\QCloud\Monitor\Barad\ntpdate.exe 2>$null 1>$null;
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。