且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

执行远程计算机上的批处理文件

更新时间:2022-12-22 23:10:12

有就设在这里的模块,这将通过WMI启用PSRemoting你。

http://gallery.technet.microsoft.com/scriptcenter/Enable-PSRemoting-Remotely-6cedfcb0

I am on host_computer and attempting to execute batch file on remote_computer (part of WORKGROUP) to enable psremoting and use winrm to add trustedhosts.

Based on http://social.technet.microsoft.com/Forums/scriptcenter/en-US/732495c0-114c-4a1e-bbe1-5a7a3b84012d/execute-a-batch-file-on-a-remote-pc?forum=ITCG

I tried creating a process, i.e.

$server = "remote_computer"
$process = [WMICLASS]"\\$server\ROOT\CIMV2:win32_process"  

But I get error

Cannot convert value "\\remote_computer\ROOT\CIMV2:win32_process" to type "System.Management.ManagementClass". Error: "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" At line:1 char:1
+ $process = [WMICLASS]"\\$server\ROOT\CIMV2:win32_process"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastToWMIClass

Then I tried using psexec

PsExec.exe \\remote_computer -u username -p password 'C:\share\enable.bat'

And get error

D:\pstools\PsExec.exe :  At line:1 char:1
+ D:\pstools\PsExec.exe \\remote_computer -u username -p password 'C:\share\ena ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError   PsExec v2.0 - Execute processes remotely Copyright (C) 2001-2013 Mark Russinovich Sysinternals - www.sysinternals.com The handle is invalid. Connecting to remote_computer...Couldn't access remote_computer: Connecting to remote_computer...

And this batch script is to enable psremoting and add trusted hosts using winrm

@echo off
C:\share\remote.ps1
winrm set winrm/config/client/auth @{Basic="true"}
winrm set winrm/config/client @{AllowUnencrypted="true"}
winrm set winrm/config/client @{TrustedHosts="host_computer"}

There is a module located here, which will enable PSRemoting via WMI for you.

http://gallery.technet.microsoft.com/scriptcenter/Enable-PSRemoting-Remotely-6cedfcb0