使用PsEcec.exe运行远程PowerShell脚本时出错

时间:2015-09-29 06:04:35

标签: powershell remote-server psexec

我在远程窗口框上有一个powershell脚本,用于查找联结所指向的文件夹。脚本的内容如下所示:

return fsutil reparsepoint query C:\foo\bar\junction_name | where-object { $_ -imatch 'Print Name:' } | foreach-object { $_ -replace 'Print Name\:\s*','' }

当我在远程盒子上运行它时,它按预期执行:)

但是,当我尝试从本地计算机远程运行时:

C:\Users\foo>C:\pstools\PsExec.exe \\remote_server_name "powershell D:\bar\my_script.ps1"

我收到错误:

  

PsExec无法启动powershell D:\ bar \ my_script.ps1   remote_server_name:文件名,目录名或卷标   语法不正确。

这个错误告诉我的任何想法(假设我可以直接在远程盒子上运行脚本而没有问题)?

THX!

3 个答案:

答案 0 :(得分:1)

1-也许你应该避免使用psexec并利用PowerShell远程处理

invoke-command -computername remote_server_name -scriptblock {. "D:\bar\my_script.ps1"}

2-如果你想保留psexec,请查看起始目录开关-w

PsExec.exe \\remote_server_name -w D:\bar "powershell -file my_script.ps1"

答案 1 :(得分:1)

PS Remoting将是最好的方式来到这里,我实际上在你的机器上打开TCP / 5985的斗争。到目前为止,微不足道的安全风险值得您从中获得管理上的好处。

最糟糕的情况是使用WMI Win32_Process类。这样的事情可能有用。

$wmiParams = @{
    'ComputerName' = 'Somecomputer'
    'Class' = 'Win32_Process'
    'Name' = 'Create'
    'Args' = 'fsutil reparsepoint query C:\foo\bar\junction_name > C:\temp.txt'
}
Invoke-WmiMethod @wmiParams
Get-Content \\somecomputer\c$\temp.txt | where-object { $_ -imatch 'Print Name:' } | foreach-object { $_ -replace 'Print Name\:\s*', '' }

答案 2 :(得分:0)

我设法让以下工作:

PsExec.exe \\remote_server_name powershell.exe D:\bar\my_script.ps1

但是,PowerShell会话未按预期关闭,并且在我的脚本返回后仍处于挂起状态,因此通过cmd调用它,因为详细here似乎解决了这个问题:

PsExec.exe \\remote_server_name cmd /c "echo . | powershell.exe D:\bar\my_script.ps1"

感谢所有建议......

相关问题