I try to install .msu package using Poweshell. My code:
$Args = #(('"' + $Path_Temp + '\' + $Hotfix[1] + '.msu' + '"'), '/norestart')
echo $Args
Start-Process -FilePath ($Env:SystemRoot + '\System32\wusa.exe') -ArgumentList $Args -Verb RunAs -Wait
echo outputs this:
"C:\Users\MyUser\AppData\Local\Temp\Windows6.1-KB2758857-x64.msu"
/norestart
All looks good, but in the end wusa warns me about incorrect usage. It works well when I pass only the first argument (which is the path to .msu file) to ArgumentList. My code does not work when I pass multiple arguments, why? I use Powershell v5.1.14409.1005
Related
I am new to Powershell and trying to run a PS script using another PS script to run it as admin in Jenkins, the script gives its desired output but it also gives the error in the output.
Powershell : Start-Process : A positional parameter cannot be found
that accepts argument At line:4 char:1
+ Powershell -Command "Start-Process powershell ""cd "F:\RiskLink\RiskL ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Start-Process :...cepts argument :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Code:
Set-Location -Path F:\abd\abc
$path = ".\xyz.ps1"
Powershell -Command "Start-Process powershell ""cd "F:\abd\abc"; & $path"" -Verb RunAs"
Long ago, I have created a simple command line parser (scripts cliparser.ps1 and cli parser.ps1):
PS D:\PShell> Get-Content ".\cliparser.ps1"
if ( $args -ne $null ) { $aLen=$args.Count } else { $aLen=0 }
"$(Split-Path -Path $PSCommandPath -Leaf): $aLen parameters"
for ( $i = 0; $i -lt $aLen ; $i++ ) { "{0}. [{1}]" -f $i, $args[$i] }
Used instead of yours xyz.ps1, it shows that something must go wrong not only with Start-Process arguments but with quoting as well (shows supplied values -Verb and RunAs). Fixed with some little effort… Improved for space(s) in paths and file names, see the following script:
$cd = (Get-Location -PSProvider Filesystem).Path
$path = ".\cliparser.ps1"
'... original (known error)'
Write-Host Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
pause
'... fixed'
Write-Host Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
$path = ".\cli parser.ps1"
Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
'done'
Note that PShell's parameters -NoProfile and -NoExit are here merely for debugging purposes.
Result:
PS D:\PShell> D:\PShell\SO\53638416.ps1
... original (known error)
Powershell -Command Start-Process powershell "pushd D:\PShell; & .\cliparser.ps1" -Verb RunAs
Start-Process : A positional parameter cannot be found that accepts argument 'D:\PShell'.
At line:1 char:1
+ Start-Process powershell pushd D:\PShell; & .\cliparser.ps1 -Verb Ru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.Start
ProcessCommand
cliparser.ps1: 2 parameters
0. [-Verb]
1. [RunAs]
Press Enter to continue...:
... fixed
Powershell -NoProfile -Command Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'';' -Verb RunAs
done
PS D:\PShell>
Called (elevated) Powershell window looks as follows (Pop-Location used to ensure that popd ran well):
To run from a cmd prompt and pass arguments to the called script:
powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"
Works for pwsh instead of powershell as well:
pwsh -NoProfile -Command "Start-Process pwsh -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"
I want to pass a cmdlet to be executed immediately in child Process.
---> $cmd = $(Get-Item -FORCE "PATH\to\Folder").Attributes= "Normal"
Start-Process powershell -ArgumentList " multiple $cmd Here"
PS : when i use { } , () or " " in -ArgumentList i get different behaviour anyone care to explain why ?.
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
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.
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...
I am trying to execute an installation via start-process but i want it to execute as a job so i can execute a few other statements while also being able to check the status of the installation as it runs in the background.
Here is a section of the code i am trying to execute -
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"" + "$SetupPath\setup.exe" + "`""
$command = "{$SetupProcess=" + "Start-process" + " " + `
"$Setup" + " "+ "-ArgumentList" + " " + `
"/config config.xml" + " " + "-Wait -PassThru" + "}"
# The above command equals-> {$SetupProcess=Start-process "C:\Test Installs\setup.exe" -ArgumentList /config config.xml -Wait -PassThru}
#Change string to Scriptblock
$ScriptBlock = [Scriptblock]::Create($command)
$job1 = Start-Job -ScriptBlock $ScriptBlock -Name "SetupJob"
When I run this and try to get back the result via Receieve-Job i only get back the command string I passed via script block. It appears the Start-process command is not executing. Is there something i am missing?
Get rid of the {} in your $command definition. [ScriptBlock]::Create() expects some text in which it will wrap in a scriptblock. You can also simplify this:
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"$SetupPath\setup.exe`""
$command = "SetupProcess = Start-process `"$Setup`" -ArgumentList `"/config config.xml`" -Wait -PassThru"
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
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.
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...