![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Using invoke-expression and folders containing spaces My script uses invoke-expression to call a stand-alone executable and pass it some parameters. I determine the location where the script is located and then use that path to build a location to the exe (which is located in the same folder of the script). So, invoke-expression then uses a parameter like this: C:\TEMP\ActivityMonitor.exe /counter:12 /task:Verify Something which works just great. However, If I run the script from within a folder that contains spaces (e.g. C:\TEMP TEST2), things go terribly wrong. Instead of actually running the exe, I only get this output on screen C:\TEMP TEST\ActivityMonitor.exe /counter:12 /task:Verify Quote: > The term 'C:\TEMP' is not recognized as... adding single and double quotes but nothing seemed to work. I'm not sure how to handle this situation. Any suggestions? Thanks (see script below) function ReportScriptActivity { param([string]$MyCounter, [string]$MyTask) $ActivityAction=$ScriptRoot+"ActivityMonitor.exe /counter:"+$MyCounter $ActivityAction=$ActivityAction+" /task:"+$MyTask invoke-expression "$ActivityAction" } $ScriptRoot=split-path -path $myinvocation.mycommand.path if (-not ($ScriptRoot.EndsWith("\"))) { $ScriptRoot=$ScriptRoot+"\" } ReportScriptActivity 12 Verify return |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Using invoke-expression and folders containing spaces Try to wrap it with single quotes: iex "'C:\TEMP TEST\ActivityMonitor.exe' /counter:12 /task:Verify" ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > My script uses invoke-expression to call a stand-alone executable and > pass it some parameters. > > I determine the location where the script is located and then use that > path to build a location to the exe (which is located in the same > folder of the script). > > So, invoke-expression then uses a parameter like this: > C:\TEMP\ActivityMonitor.exe /counter:12 /task:Verify > > Something which works just great. > > However, If I run the script from within a folder that contains spaces > (e.g. C:\TEMP TEST2), things go terribly wrong. > > Instead of actually running the exe, I only get this output on screen > C:\TEMP TEST\ActivityMonitor.exe /counter:12 /task:Verify > Quote: >> The term 'C:\TEMP' is not recognized as... >> > adding single and double quotes but nothing seemed to work. > > I'm not sure how to handle this situation. Any suggestions? > > Thanks (see script below) > > function ReportScriptActivity > { > param([string]$MyCounter, [string]$MyTask) > $ActivityAction=$ScriptRoot+"ActivityMonitor.exe > /counter:"+$MyCounter > $ActivityAction=$ActivityAction+" /task:"+$MyTask > invoke-expression "$ActivityAction" > } > $ScriptRoot=split-path -path $myinvocation.mycommand.path if (-not > ($ScriptRoot.EndsWith("\"))) { $ScriptRoot=$ScriptRoot+"\" } > > ReportScriptActivity 12 Verify > > return > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Using invoke-expression and folders containing spaces I'm not sure the last will wotk, try this too: & 'c:\test test2\ActivityMonitor.exe' /counter:12 /task:Verify ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Try to wrap it with single quotes: > > iex "'C:\TEMP TEST\ActivityMonitor.exe' /counter:12 /task:Verify" > > ----- > Shay Levi > $cript Fanatic > http://scriptolog.blogspot.com Quote: >> My script uses invoke-expression to call a stand-alone executable and >> pass it some parameters. >> >> I determine the location where the script is located and then use >> that path to build a location to the exe (which is located in the >> same folder of the script). >> >> So, invoke-expression then uses a parameter like this: >> C:\TEMP\ActivityMonitor.exe /counter:12 /task:Verify >> >> Something which works just great. >> >> However, If I run the script from within a folder that contains >> spaces (e.g. C:\TEMP TEST2), things go terribly wrong. >> >> Instead of actually running the exe, I only get this output on screen >> C:\TEMP TEST\ActivityMonitor.exe /counter:12 /task:Verify >> Quote: >>> The term 'C:\TEMP' is not recognized as... >>> >> adding single and double quotes but nothing seemed to work. >> >> I'm not sure how to handle this situation. Any suggestions? >> >> Thanks (see script below) >> >> function ReportScriptActivity >> { >> param([string]$MyCounter, [string]$MyTask) >> $ActivityAction=$ScriptRoot+"ActivityMonitor.exe >> /counter:"+$MyCounter >> $ActivityAction=$ActivityAction+" /task:"+$MyTask >> invoke-expression "$ActivityAction" >> } >> $ScriptRoot=split-path -path $myinvocation.mycommand.path if (-not >> ($ScriptRoot.EndsWith("\"))) { $ScriptRoot=$ScriptRoot+"\" } >> ReportScriptActivity 12 Verify >> >> return >> |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Using invoke-expression and folders containing spaces Shay Levi wrote: Quote: > Try to wrap it with single quotes: > iex "'C:\TEMP TEST\ActivityMonitor.exe' /counter:12 /task:Verify" > I'm not sure the last will wotk, try this too: > & 'c:\test test2\ActivityMonitor.exe' /counter:12 /task:Verify iex "'C:\TEMP folder\am.exe' /counter:12 /task:Verify" Fails with: You must provide a value expression on the right-hand side of the '/' operator. However, this does work... & 'C:\Temp folder\am.exe' /counter:12 /task:verify Unfortunately, I can't use a fixed command since I need changeable command line parameters (different counter, different task). So, I first build a string (which I would normally do based on the current value of $counter and $task): $Command="'C:\Temp folder\am.exe' /counter:12 /task:verify" $Command then looks something like this: 'C:\Temp folder\am.exe' /counter:12 /task:verify Then I try to execute the $Command content by using: & $Command But this results in following error: The term ''C:\Temp folder\am.exe' /counter:12 /task:verify' is not recognized as a cmdlet So, I'm completely stuck on how to run the executable using command line parameters determined during-time and where the exe's path contains spaces. Feedback is much appreciated. Thanks, Kris |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Using invoke-expression and folders containing spaces How about this: $counter=12 $task="verify" & 'C:\Temp folder\am.exe' /counter:$counter /task:$task ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Shay Levi wrote: > Quote: >> Try to wrap it with single quotes: >> iex "'C:\TEMP TEST\ActivityMonitor.exe' /counter:12 /task:Verify" >> I'm not sure the last will wotk, try this too: >> & 'c:\test test2\ActivityMonitor.exe' /counter:12 /task:Verify > > iex "'C:\TEMP folder\am.exe' /counter:12 /task:Verify" > Fails with: > You must provide a value expression on the right-hand side of the '/' > operator. > However, this does work... > & 'C:\Temp folder\am.exe' /counter:12 /task:verify > Unfortunately, I can't use a fixed command since I need changeable > command line parameters (different counter, different task). > > So, I first build a string (which I would normally do based on the > current value of $counter and $task): > $Command="'C:\Temp folder\am.exe' /counter:12 /task:verify" > $Command then looks something like this: > 'C:\Temp folder\am.exe' /counter:12 /task:verify > Then I try to execute the $Command content by using: > & $Command > But this results in following error: > The term ''C:\Temp folder\am.exe' /counter:12 /task:verify' is not > recognized as a cmdlet > So, I'm completely stuck on how to run the executable using command > line parameters determined during-time and where the exe's path > contains spaces. > > Feedback is much appreciated. > > Thanks, > > Kris > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Using invoke-expression and folders containing spaces Shay Levi wrote: Quote: > How about this: > > $counter=12 > $task="verify" > > & 'C:\Temp folder\am.exe' /counter:$counter /task:$task > executable (determined at run-time). So, after some experimenting I came to this: $Counter=1234 $Task="verify" $ExeLocation="c:\temp folder\am.exe" & $ExeLocation /counter:$counter /task:$task And this works great! Thank you very much for your input and patience. Regards, Kris |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Re: running a program with spaces in Path with invoke-expression | PowerShell | |||
| Re: running a program with spaces in Path with invoke-expression | PowerShell | |||
| Invoke-Expression executing a command with spaces | PowerShell | |||
| invoke-expression with .exe that has spaces in its path | PowerShell | |||
| Issue: Invoke-Expression with $args in the expression | PowerShell | |||