Set Executable's Working Directory When PowerShell Remoting -
i'm using powershell remoting execute exe file on remote server. problem exe needs have working directory set directory exe in run properly. if run exe locally (on server) command prompt works fine, , if use enter-pssession (from workstation) , use start-process -filepath [pathtoexe] -workingdirectory [directorypath]
works fine, if use invoke-command -computername [blah] -scriptblock [myscriptblock]
or $session = new-pssession -computername [blah]; invoke-command -session $session -scriptblock [myscriptblock]
(from workstation) working directory not set.
this [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 tried using invoke-command instead of start-process, has same effect; working directory not set.
i've verified using sysinternals process explorer, right-clicking on process , choosing properties. when launch locally or use enter-pssession, command line , current directory properties set, not when using new-pssession or invoke-command computername.
i'm using both set-location
, setting -workingdirectory
, 2 typical recommended approaches setting working directory, , get-location
display expected (server's local) path (e.g. c:\inetpublive\scheduledjobs\1.2.3.4). i'm guessing bug powershell (i'm using v4 on workstation , server), or maybe there's i'm missing?
update
it turns out working directory red herring (at least, think was). reason works fine if called executable command prompt.
so in invoke-command (i replaced start-process invoke-command), changing this:
& ""$hubsyncronizerexepath"" /database $database
to this:
& cmd /c ""$hubsyncronizerexepath"" /database $database
fixed problem.
thanks of suggestions guys :)
try looking @ new-psdrive , see if helps
i guess you'll want like
new-psdrive -name workingdir -psprovider filesystem -root "\\remoteserver\c$\inetpublive\scheduledjobs\1.2.3.4"
cd workingdir:
i assume should able amend script include , put in $version variable in path on new-psdrive command...
not need do, it's first thing sprang mind...
alternatively, try amending script follows:
$hubsyncronizerexedirectorypath = "\\remoteserver\c$\inetpublive\scheduledjobs\$version\"
Comments
Post a Comment