ChatGPT解决这个技术问题 Extra ChatGPT

Executing a command stored in a variable from PowerShell

I have a command that I have build and stored in a variable in PowerShell. This command works if I do a Write-Host and copy and paste 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
isn't this just the '&' symbol? stackoverflow.com/questions/22074507/…

Z
Zombo

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.


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 '' ?
Splatting seems to work the same: & $cmd @prm But I'm curious about why is that
This doesn't work: $path=C:\blah and & $path\test.exe blah1.
@RomanKuzmin It works! $path="C:\Windows\System32\inetsrv"; & "$path\appcmd"
invoke-expression needs one string whereas & can have several strings.
A
Aakash

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

iex is an alias for Invoke-Expression so you could do:

iex $cmd1

For a full list : Visit https://ss64.com/ps/ for more Powershell stuff.

Good Luck...


Tells me The term '7z.exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At :line:14 char:1 + & <<<< $cmd1
@Travis: Oops. the ampersand is works if there are no args. I updated the post with a solution for your command.
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.log It almost seems like it is trying to evaluate it instead of executing it.
@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.
@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.