Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Using invoke-expression and folders containing spaces

Reply
 
Old 02-08-2008   #1 (permalink)
Kris


 
 

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...
So, there are issues with the space in the folder name. I've tried
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 SpecsSystem Spec
Old 02-08-2008   #2 (permalink)
Shay Levi


 
 

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...
>>
> So, there are issues with the space in the folder name. I've tried
> 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 SpecsSystem Spec
Old 02-08-2008   #3 (permalink)
Shay Levi


 
 

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...
>>>
>> So, there are issues with the space in the folder name. I've tried
>> 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 SpecsSystem Spec
Old 02-08-2008   #4 (permalink)
Kris


 
 

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
Still can't seem to get things working...

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 SpecsSystem Spec
Old 02-08-2008   #5 (permalink)
Shay Levi


 
 

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
> Still can't seem to get things working...
>
> 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 SpecsSystem Spec
Old 02-08-2008   #6 (permalink)
Kris


 
 

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
>
Yes. That works fine. However, I also needed a variable path to the
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46