简体   繁体   中英

Set Executable's Working Directory When PowerShell Remoting

I'm using PowerShell remoting to execute an exe file on a remote server. 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. 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] $session = New-PSSession -ComputerName [Blah]; Invoke-Command -Session $session -ScriptBlock [MyScriptBlock] (from my workstation) then the working directory does not get set.

This is what [MyScriptBlock] looks like:

$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; 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. 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.

在此输入图像描述

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). 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?


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:

& ""$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

I guess you'll want something like

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

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...

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\\"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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