How to run the batch file on the computer from another computer?

时间:2016-09-01 06:19:28

标签: powershell batch-file remote-access remote-server

I have computers connected in LAN. From one PC say A I want to run the batch file stored in computer B. I don't want to use any resources of A. That batch file contains resource sensitive process. So the resource of B should only used. The batch file should be run on B but initiated from A.

3 个答案:

答案 0 :(得分:1)

通过批处理文件,您可以使用WMIC或SCHTASKS:

1)SCHTASKS

SCHTASKS /s remote_machine /U username /P password /create /tn "On demand demo" /tr "C:\some.bat" /sc ONCE /sd 01/01/1910 /st 00:00
SCHTASKS /s remote_machine /U username /P password /run /TN "On demand demo" 

2)WMIC(wmic将返回已启动进程的pid)

WMIC /NODE remote_machine /user user /password password process call create "c:\some.bat","c:\exec_dir"

答案 1 :(得分:0)

假设您有足够的特权并配置了WinRM。

Invoke-Command -ComputerName 'RemotePCName' -ScriptBlock { 'c:\temp\script.bat' } -Credential $(Get-Credential)

答案 2 :(得分:0)

您必须使用Invoke-Command并创建PSSession

它看起来像:

$session = New-PSSession -ComputerName "ComputerB"
Invoke-Command -Session $session -ScriptBlock { ./P:\Path\to\bat.bat  }
$session | Remove-Session

这应该是你需要的。 如果批处理文件中有任何输出,则可以使用

$session = New-PSSession -ComputerName "ComputerB"
$output = Invoke-Command -Session $session -ScriptBlock { ./P:\Path\to\bat.bat  }
$session | Remove-Session

如果您需要ScriptBlock中的任何$ vars,您可以使用:

$examplePath = "P:\Path\to"
$exampleFile = "mySCript.bat"
$session = New-PSSession -ComputerName "ComputerB"
$output = Invoke-Command -Session $session -ScriptBlock { ./$args[0] + "\" + $args[1]  } -ArgumentList $examplePath, $exampleFile
$session | Remove-Session
相关问题