我编写了一个 powershell 脚本来远程执行一些操作系统验证。但是当远程服务器未使用 winrm 启用时,我收到以下消息。那么如何强制 winrm 使用 psexec 远程启用?
**Connecting to remote server xxxxx.us.oim.com failed with the following error message : The WSMan service could not launch a host process to process the given request. Make sure the WSMan provider host server and proxy are properly registered.**
我在我的 powershell 脚本 PsExec.exe \$host -s powershell "Enable-PSRemoting -force" 中添加了一条命令(此处 $host 将给出主机名),该命令正在执行,同时我也收到以下消息。
PsExec.exe : Connecting to xxxxxxxxx.us.oim.com...
At line:72 char:1
+ PsExec.exe \\$fqdn -s powershell "Enable-PSRemoting -force"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Connecting to xxxxxxxxx.us.oim.com...:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Starting PSEXESVC service on xxxxxxxxx.us.oim.com...Connecting with PsExec service on xxxxxxxxx.us.oim.com...Starting powershell on xxxxxxxxx.us.oim.com...
powershell exited on xxxxxxxxx.us.oim.com with error code 0
仅当“WSMan 服务无法启动主机进程来处理给定请求”时才可以创建条件,然后运行命令来启用 winrm !
如果可以的话请告诉我如何做?
函数启用WinRM {
$computers = Get-Content "C:\temp\computers.txt"
foreach ($computer in $computers) {
$result = winrm id -r:$computer 2> $null
if ($lastExitCode -eq 0) {
Write-Host "WinRM already enabled on" $computer "..." -ForegroundColor green
} else {
Write-Host "Enabling WinRM on" $computer "..." -ForegroundColor red
.\pstools\psexec.exe \\$computer -s C:\Windows\System32\winrm.cmd qc -quiet
if ($LastExitCode -eq 0) {
.\pstools\psservice.exe \\$computer restart WinRM
$result = winrm id -r:$computer 2>$null
if ($LastExitCode -eq 0) {Write-Host "WinRM successfully enabled!" -ForegroundColor green}
else {exit 1}
} #end of if
} #end of else
} #end of foreach
只需在脚本末尾调用此函数即可。
确保脚本所在的文件夹中有 pstools。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)