Setting the executable working directory when uninstalling PowerShell

I am using remote PowerShell to execute an exe file on a remote server. The problem is that the exe has to set its working directory to the directory where the exe is running in order for it to work properly. If I run the exe locally (on the server) from the command line it works fine and if I use Enter-PSSession (from my workstation) and then use Start-Process -FilePath [PathToExe] -WorkingDirectory [DirectoryPath]

that works great, but if I use Invoke-Command -ComputerName [Blah] -ScriptBlock [MyScriptBlock]

or $session = New-PSSession -ComputerName [Blah]; Invoke-Command -Session $session -ScriptBlock [MyScriptBlock]

(from my workstation) then the working directory will not be installed.

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 also tried to use Invoke-Command instead of Start-Process, but it has the same effect; The working directory is not installed.

I tested this using SysInternals Process Explorer by right clicking the process and choosing Properties. When I run it locally or use Enter-PSSession, command line and current directory properties are set, but not when using New-PSSession or just Invoke-Command named Computer.Name.

enter image description here

I use both Set-Location

and install -WorkingDirectory

, which are 2 typical recommended approaches for setting the working directory, and it Get-Location

displays the expected (local) server path (e.g. C: \ inetpubLive \ ScheduledJobs \ 1.2.3.4). I am guessing this is just a bug with PowerShell (I am using V4 on workstation and server), or maybe there is something I am missing?


UPDATE

It turns out 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 the command line.

So, in my Invoke-Command (I replaced Start-Process with Invoke-Command), changing this:

& ""$hubSyncronizerExePath"" /database $database

      

:

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

      

fixed the problem.

Thanks for all the suggestions guys :)

+3


source to share


1 answer


Try searching New-PSDrive and see if that helps

I think you need something like

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

CD WorkDir:



I assume you can modify your script to include and insert the $ version variable in the path in the New-PSDrive ...

Not sure if this will do what you need to do it, but this is the first thing that came into my mind ...

Alternatively, try changing your script like this:

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

0


source







All Articles