tags:

views:

107

answers:

2

I have a command that i have build and stored in a variable in power shell. This command works if i do a write-host and copy and past into a standard cmd.exe window.

How do i execute this command from inside my script?

I have tried several combination of invoke-command or invoke-expression with no luck.

This is how i built the variable:

$cmd1= $arcprg + $arcdir + "\" + $site1+"-"+$hst+"-"+$yesterday+".zip "+$logpath1+"u_ex"+$yesterday+".log"

This is what the variable looks like if it is printed to the screen:

7z.exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log
A: 

Try invoking your command with invoke-expression.

invoke-expression $cmd1

Here is a working example on my machine:

$cmd = "& 'C:\Program Files\7-zip\7z.exe' a -tzip c:\temp\test.zip c:\temp\test.txt"
invoke-expression $cmd
kbrimington
Travis
@Travis: Oops. the ampersand is works if there are no args. I updated the post with a solution for your command.
kbrimington
I have tried invoke-expression before and it has not worked. It spits out the error:Bad numeric constant: 7.At :line:1 char:2+ 7z <<<< .exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.logIt almost seems like it is trying to evaluate it instead of executing it.
Travis
@Travis: It may be that 7z.exe is not on your path. Verify that it is on the path and/or try giving the full path name to the executable in your expression.
kbrimington
@Travis: I just confirmed on my own system that 7z.exe gave the error you described, just typing from the command prompt, but with the full path to 7z.exe (for me, it was `'C:\Program Files\7-zip\7z.exe'`, I could execute 7z.exe.
kbrimington
Yes i test that out and added 7z.exe to the path. Still have the problem.
Travis
@Travis: Sorry. It takes cobbling together all the elements of our conversation. I've updated my post with a working example. You will need to change out the paths, but the structure should still apply.
kbrimington
+1  A: 

Here is yet another way without Invoke-Expression but with two variables (command:string and parameters:array). It works fine for me. Assume 7z.exe is in the system path.

$cmd = '7z.exe'
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'

& $cmd $prm

If the command is known (7z.exe) and only parameters are variable then this will do:

$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'

& 7z.exe $prm

BTW, Invoke-Expression with one parameter works for me, too, e.g. this works:

$cmd = '& 7z.exe a -tzip "c:\temp\with space\test2.zip" "C:\TEMP\with space\changelog"'

Invoke-Expression $cmd

P.S. I usually prefer the way with a parameter array because it is easier to compose programmatically than to build an expression for Invoke-Expression.

Roman Kuzmin
That is great. It is starting to come together now. Instead of 'c:\temp\with space\test1.zip' can i just use a $variable ?Do i need to place it is "" or '' ?
Travis