0

I am working on a script to install software to remote PCs, and I ran into an issue I have never experienced before. I am trying to pass in the file name to Start-Process as a variable, but for some reason it does not like this, and hangs on the install. It will just sit indefinitely. The log file shows that this creates shows that the process terminated unexpectedly.

Invoke-Command -ComputerName $ComputerName -ScriptBlock { Start-Process msiexec "/i c:\$FileName /lvx* c:\PackageManagement.log" -wait  }

$FileName is test.msi which exists on the target computers C: drive as I copied it there.

I have tried using Powershell joins, I have tried using concat, I have tried setting the Start_Process command to it's own variable and using that. It just hangs. The computer is on, connected to the internet. When I replace $FileName in the code above with test.msi directly, it installs just fine.

Owen S
  • 1
  • You have arguments which must be a variable like following example. : ["/i c:\$FileName /lvx* c:\PackageManagement.log"] https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.3#example-8-run-a-command-as-an-administrator-using-alternate-credentials – jdweng Dec 13 '22 at 20:31
  • In short: Because the script block executes _remotely_, it knows nothing about the caller's _local_ variables. The simplest way to reference the caller's variable values is with the `$using:` scope. See the linked duplicate for details. – mklement0 Dec 13 '22 at 20:42

1 Answers1

0

If you use $using:FileName in place of just $FileName, that will give you what you are looking for on Powershell 3.0 or later.

Owen S
  • 1