2

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

Steve M
  • 43
  • 5
  • Can you separate `/i $PathToMSI` so that all arguments are elements in the `ArgumentList` array? e.g. `Start-Process msiexec.exe -ArgumentList '/i', $PathToMSI, '/qn/'`? – codewario Dec 20 '19 at 15:37
  • I added a write for scriptblock, and $Path1 was not resolved `PathToMSI= D:\Install\20191213.3\'X Y Z.msi' scriptblock= Start-Process msiexec.exe -Argumentlist "/i D:\Install\$path1\'$path2$path3'","/qn"` – Steve M Dec 20 '19 at 15:48
  • scriptblock= Start-Process msiexec.exe -Argumentlist "/i, $PathToMSI, ","/qn" – Steve M Dec 20 '19 at 15:55
  • Did you tried `Test-Path "$PathToMSI"`? An apostrophe is valid filename character so I doubt that your file name contains a trailing apostrophe. Try `$PathToMSI = "D:\Install\$path1\$path2$path3"` – JosefZ Dec 20 '19 at 16:14
  • Hang on, I see the problem. I'll post as an answer but the remote command doesn't know how to resolve your local variable. – codewario Dec 20 '19 at 16:14

1 Answers1

1

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.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • With the $using I get the following error:##[error]System.Management.Automation.RuntimeException: A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid only if the script block is invoked on a remote computer. – Steve M Dec 20 '19 at 16:32
  • Running with the Argument list the first entry gets the same results with it saying it ran, but it did not run the MSI package – Steve M Dec 20 '19 at 16:36
  • What if you eschew `Start-Process` entirely for the following, making sure to use the ArgumentList method above (I never use `Start-Process` for msiexec myself): `&msiexec /i $args[0] /qn`? – codewario Dec 20 '19 at 17:17
  • I had found another solution where someone was having an issue with Invoke: https://stackoverflow.com/questions/25407353/issues-with-powershell-invoke-command?rq=1; After changeing the MSI from 'X Y Z.msi' to 'XYZ.MSI' It worked as expected. – Steve M Dec 20 '19 at 17:20
  • If you use the call operator `&` to execute `msiexec` instead of `Start-Process` what happens? Have you tried outputting `args[0]` to see if `$PathToMSI` is there? Also, if you aren't running this remotely, why are you using `Invoke-Command` in the first place? – codewario Dec 20 '19 at 17:24
  • This is being run from a TFS Task to install the MSI. It looks like an issue with handling file names with spaces in them. With spaces in the name you normally put single quotes 'my name.msi', but for some reason I couldn't get that to resolve. myname.msi resolves just fine. – Steve M Dec 20 '19 at 17:42