Home > database >  Powershell pass variable to ArgumentList
Powershell pass variable to ArgumentList

Time:09-16

I have some trouble passing a variable to an ArgumentList. I want to install Teamviewer via MSI with powershell:

Start-Process 'msiexec.exe' -ArgumentList $tvparams

i want to deploy the machine on a Teamviewer-Site and pass the variable $serialnumber as the alias:

$serialnumber = (get-ciminstance -classname win32_bios -property serialnumber).serialnumber


$tvparams = '/i','ASSIGNMENTOPTIONS="--alias  $serialnumber"'

The onboarding of the machine works but instead of the actual serialnumber as an alias the machine pops up as "$serialnumber" on the Teamviewer-Site as if it is a String instead of a variable.

Im fiddling with the quotes for too long so im asking now. I feel like there must be a simple solution

CodePudding user response:

the machine pops up as "$serialnumber" on the Teamviewer-Site as if it is a String instead of a variable.

That's because you're using ' single-quotes - variable expansion only works in " double-quoted strings.

You can get around it by switching to double-quotes and escaping the literal double-quotes:

$tvparams = '/i',"ASSIGNMENTOPTIONS=""--alias  $serialnumber"""

Or by using the -f string format operator:

$tvparams = '/i',$('ASSIGNMENTOPTIONS="--alias  {0}"' -f $serialnumber)
  • Related