简体   繁体   English

如何在PowerShell中启动远程处理

[英]How to start remotely process in PowerShell

I have a problem, I have a script which: 我有一个问题,我有一个脚本

  • Connect with PSSession (I use PSSession with admin account) 与PSSession连接(我使用PSSession与管理员帐户)
  • Stop 2 process 停止2进程
  • Do change on them files 改变他们的文件
  • Start the 2 process (Problem here) 启动2流程(此处出现问题)

I want to start process on server, so i'm connect with PSSession (No problem) 我想在服务器上启动进程,所以我用PSSession连接(没问题)

I do Invoke-Command : 我做Invoke-Command:

# $pathProg path to my program
Invoke-Command -session $mySession -command {Start-Process $($args[0])} -ArgumentList $pathProg

But it does nothing (I verify with VNC) 但它什么都不做(我用VNC验证)

I do Invoke-Command too : 我也做Invoke-Command:

# $pathProg path to my program
Invoke-Command -session $mySession -command {&$($args[0])} -ArgumentList $pathProg

It lauch the program (Good) but my script wait the end program (Not good) 这个程序很好(好)但我的脚本等待结束程序(不好)

Anyone have an idea ? 有人有想法吗?

Thanks 谢谢

You can try using WMI: 您可以尝试使用WMI:

$command = "notepad.exe"
$process = [WMICLASS]"\\$CompName\ROOT\CIMV2:win32_process"
$result = $process.Create($command) 

If you need passing credentials: 如果您需要传递凭据:

$cred = get-credential
$process = get-wmiobject -query "SELECT * FROM Meta_Class WHERE __Class = 'Win32_Process'" -namespace "root\cimv2" -computername $CompName -credential $cred
$results = $process.Create( "notepad.exe" )

$pathProg may be not be available within the script block which gets run eventually. $pathProg可能在最终运行的脚本块中不可用。 You might want to pass it as an argument to the script block 您可能希望将其作为参数传递给脚本块

Invoke-Command -session $mySession -command { param($progPath) ... } -argumentlist $progPath

Not that the outer -argumentlist , passes the arguments to the scriptblock. 不是外部-argumentlist ,将参数传递给scriptblock。

Have you tried building the command as a string locally, then passing it to the Invoke-Command script as a ScriptBlock? 您是否尝试在本地将该命令构建为字符串,然后将其作为ScriptBlock传递给Invoke-Command脚本?

$remoteSession = New-PSSession -ComputerName 'MyServer'
$processName = 'MyProcess'

$command = 'Start-Service ' + $processName + ';'

Invoke-Command -Session      $remoteSession `
               -ScriptBlock  ([ScriptBlock]::create($command))

Remove-PSSession $remoteSession

If you want feedback from the remote server then you can get the output via Write-Output, like this: 如果您需要远程服务器的反馈,那么您可以通过Write-Output获取输出,如下所示:

$command = 'Start-Service ' + $processName + ' | Write-Output ;'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM