简体   繁体   English

PowerShell远程处理时设置可执行文件的工作目录

[英]Set Executable's Working Directory When PowerShell Remoting

I'm using PowerShell remoting to execute an exe file on a remote server. 我正在使用PowerShell远程处理在远程服务器上执行exe文件。 The problem is that the exe needs to have its Working Directory set to the directory that the exe is in for it to run properly. 问题是exe需要将其工作目录设置为exe所在的目录才能使其正常运行。 If I run the exe locally (on the server) from a command prompt it works fine, and if I use Enter-PSSession (from my workstation) and then use Start-Process -FilePath [PathToExe] -WorkingDirectory [DirectoryPath] that works fine, but if I use Invoke-Command -ComputerName [Blah] -ScriptBlock [MyScriptBlock] or $session = New-PSSession -ComputerName [Blah]; Invoke-Command -Session $session -ScriptBlock [MyScriptBlock] 如果我从命令提示符本地(在服务器上)运行exe它工作正常,如果我使用Enter-PSSession(从我的工作站)然后使用Start-Process -FilePath [PathToExe] -WorkingDirectory [DirectoryPath]工作正常,但是如果我使用Invoke-Command -ComputerName [Blah] -ScriptBlock [MyScriptBlock]$session = New-PSSession -ComputerName [Blah]; Invoke-Command -Session $session -ScriptBlock [MyScriptBlock] $session = New-PSSession -ComputerName [Blah]; Invoke-Command -Session $session -ScriptBlock [MyScriptBlock] (from my workstation) then the working directory does not get set. $session = New-PSSession -ComputerName [Blah]; Invoke-Command -Session $session -ScriptBlock [MyScriptBlock] (来自我的工作站)然后工作目录没有设置。

This is what [MyScriptBlock] looks like: 这就是[MyScriptBlock]的样子:

$scriptBlock = {
        param($version, $database)
        $hubSyncronizerExeDirectoryPath = "C:\inetpubLive\ScheduledJobs\$version\"
        $hubSyncronizerExePath = Join-Path $hubSyncronizerExeDirectoryPath 'Test.exe'
        Set-Location -Path $hubSyncronizerExeDirectoryPath
        Get-Location
        Write-Output "$version $database"
        Start-Process -FilePath $hubSyncronizerExePath -WorkingDirectory $hubSyncronizerExeDirectoryPath -ArgumentList '/database',$database
}

I've also tried using Invoke-Command instead of Start-Process, but it has the same effect; 我也尝试过使用Invoke-Command而不是Start-Process,但它具有相同的效果; the Working Directory does not get set. 工作目录未设置。

I've verified this by using the SysInternals Process Explorer , right-clicking on the process and choosing Properties. 我已经通过使用SysInternals Process Explorer验证了这一点,右键单击该进程并选择Properties。 When I launch it locally or use Enter-PSSession, the Command Line and Current Directory properties are set, but not when using New-PSSession or just Invoke-Command with ComputerName. 当我在本地启动它或使用Enter-PSSession时,会设置命令行和当前目录属性,但在使用New-PSSession或仅使用带有ComputerName的Invoke-Command时则不会。

在此输入图像描述

I'm using both Set-Location and setting the -WorkingDirectory , which are the 2 typical recommended approaches for setting the working directory, and Get-Location does display the expected (server's local) path (eg C:\\inetpubLive\\ScheduledJobs\\1.2.3.4). 我正在使用Set-Location和设置-WorkingDirectory ,这是设置工作目录的两种典型推荐方法, Get-Location确实显示了预期的(服务器的本地)路径(例如C:\\ inetpubLive \\ ScheduledJobs \\ 1.2) .3.4)。 I'm guessing that this is just a bug with PowerShell (I'm using V4 on workstation and server), or maybe there's something I'm missing? 我猜这只是PowerShell的一个错误(我在工作站和服务器上使用V4),或者可能是我缺少的东西?


UPDATE UPDATE

It turns out that the working directory was a red herring (at least, I think it was). 事实证明,工作目录是一个红色的鲱鱼(至少,我认为是)。 For some reason everything works fine if I called my executable from a command prompt. 出于某种原因,如果我从命令提示符调用可执行文件,一切正常。

So in my Invoke-Command (I replaced Start-Process with Invoke-Command), changing this: 所以在我的Invoke-Command(我用Invoke-Command取代了Start-Process)中,改变了这个:

& ""$hubSyncronizerExePath"" /database $database

to this: 对此:

& cmd /c ""$hubSyncronizerExePath"" /database $database

fixed the problem. 解决了这个问题。

Thanks for all of the suggestions guys :) 谢谢你的所有建议:)

Try looking at New-PSDrive and see if that helps 试着看看New-PSDrive ,看看是否有帮助

I guess you'll want something like 我想你会想要类似的东西

New-PSDrive -Name WorkingDir -PSProvider FileSystem -Root "\\\\RemoteServer\\c$\\inetpubLive\\ScheduledJobs\\1.2.3.4" New-PSDrive -Name WorkingDir -PSProvider FileSystem -Root“\\\\ RemoteServer \\ c $ \\ inetpubLive \\ ScheduledJobs \\ 1.2.3.4”

CD WorkingDir: CD WorkingDir:

I assume you should be able to amend your script to include and put in the $version variable in to the path on the New-PSDrive command... 我假设您应该能够修改您的脚本以包含并将$ version变量放入New-PSDrive命令的路径中...

Not certain this will do what you need it to do, but it's the first thing that sprang to mind... 不确定这会做你需要做的事情,但这是首先想到的事情......

Alternatively, try amending your script as follows: 或者,尝试修改脚本,如下所示:

$hubSyncronizerExeDirectoryPath = "\\\\remoteserver\\C$\\inetpubLive\\ScheduledJobs\\$version\\" $ hubSyncronizerExeDirectoryPath =“\\\\ remoteserver \\ C $ \\ inetpubLive \\ ScheduledJobs \\ $ version \\”

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

相关问题 使用Powershell进行远程处理,然后执行一组命令 - Remoting with powershell, then executing a set of commands Powershell远程处理突然停止工作 - powershell remoting suddenly stops working Import-PfxCertificate Powershell功能在Localmachine上运行正常,但在其他虚拟机上使用Powershell远程处理时不起作用 - Import-PfxCertificate powershell function work fine on Localmachine but not working when using powershell remoting on other virtual machine PowerShell远程处理会话变量不起作用 - PowerShell remoting session variable isn't working 使用 PowerShell 的 Invoke-Command 进行远程处理时,如何包含本地定义的函数? - How do I include a locally defined function when using PowerShell's Invoke-Command for remoting? 确定机器何时处于Powershell Remoting的良好状态? - Determining when machine is in good state for Powershell Remoting? 将代理服务器用于 LAN 时的 PowerShell 远程处理 - PowerShell Remoting when proxy server is used for LAN 如何以编程方式在Azure VM上设置Powershell远程处理 - How to Programmatically Set Up Powershell Remoting on an Azure VM Powershell远程交换:Set-CASMailbox没有属性ActiveSyncBlockedDeviceIDs - powershell remoting to exchange: Set-CASMailbox does not have property ActiveSyncBlockedDeviceIDs 远程处理时通过Powershell脚本访问UNC路径时出错 - Error when accessing UNC paths through powershell script when remoting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM