I am trying to use Power-shells Invoke to run an MSI install.
This code is not runing the MSI install.
param ($path1, $path2, $path3)
write-output "path1= $path1"
write-output "path2= $path2"
write-output "path3= $path3"
$PathToMSI = "D:\Install\$path1\'$path2$path3'"
write-output "PathToMSI= $PathToMSI"
$scriptblock = {Start-Process msiexec.exe -Argumentlist "/i $PathToMSI","/qn"}
invoke-command -scriptblock $scriptblock
I know that my PathToMSI is correct, as this is what is displayed, but it's not executing.
path1= 20191213.3
path2= X Y Z
path3= .msi
PathToMSI= D:\Install\20191213.3\'X Y Z.msi'
If I run it hard coded it works?
Invoke-Command -ScriptBlock {
D:\install\20191213.3\'X Y Z.msi' /quiet
}
It seems that the PathToMSI is not resolving to it's value. I have reviewed a few like Error invoking command to install a Msi through Powershell
You need to pass $PathToMSI
to the ScriptBlock
. You can either use -ArgumentList
for this:
Invoke-Command -ArgumentList $PathToMSI -ScriptBlock {
Start-Process msiexec.exe -Argumentlist '/i', $args[0], '/qn'
}
or you can use the $using:
scope if invoking on a remote computer:
Invoke-Command -ComputerName server.domain.tld -ScriptBlock {
Start-Process msiexec.exe -Argumentlist '/i', $using:PathToMSI, '/qn'
}
Invoke-Command
runs your ScriptBlock
in a new PowerShell session, which doesn't know of any local variables you may have declared. The $using
scope will look to the parent session for variable resolution, while -ArgumentList
will pass literal variables that can be referenced using the $args
variable within your ScriptBlock
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.