Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用powershell中的psexec启用winrm_Powershell - Fatal编程技术网

如何使用powershell中的psexec启用winrm

如何使用powershell中的psexec启用winrm,powershell,Powershell,我编写了一个powershell脚本来远程执行一些操作系统验证。但是,当远程服务器未使用winrm启用时,我会收到下面的消息。那么,如何使用psexec强制winrm远程启用呢 **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 r

我编写了一个powershell脚本来远程执行一些操作系统验证。但是,当远程服务器未使用winrm启用时,我会收到下面的消息。那么,如何使用psexec强制winrm远程启用呢

**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。

假设pstools位于system32 path,我们是否需要将路径更改为c:\windows\system32\pstools\psexec.exe\\$computer-s c:\windows\system32\winrm.cmd qc-quietYou应该。在我的情况下,我已经将pstools文件夹I Temp folder放在脚本所在的位置,也就是它computers.txt文件。如果您在其他文件夹中有pstools,您应该使用该路径。非常感谢Vladmir