6

Trying to get a variable into Start-Process msiexec, however, when I run the script, it returns:

Start-Process : A positional parameter cannot be found that accepts argument

'{BCF4CF24-88AB-45E1-A6E6-40C8278A70C5}'.

Powershell script:

$properties = "identifyingnumber","name"
$filter = "Name LIKE '%Some software%'"

$getWMI = Get-WmiObject -Class win32_product  -Filter $filter | Select-Object -Property $properties | Sort-Object Name
if ($getWMI -and $getWMI.Length -eq 2) {
        $id0 = $getWMI[0].identifyingnumber
        $id1 = $getWMI[1].identifyingnumber
        (Start-Process msiexec.exe -ArgumentList '"/X' $id1 '/QN KLPASSWD=randompass'"" -Wait).ExitCode  
}
else { exit }
Slait
  • 167
  • 1
  • 1
  • 8
  • 6
    You're passing the arguments improperly. `Start-Process msiexec -ArgumentList @('/X',$id1,'/QN','KLPASSWD=randompass')` If your pass has spaces, use double quotes inside the string literal i.e. `'KLPASSWD="random pass"'`. The literal array (`@()`) isn't necessary, but I like to be literal and concise where I can be – Maximilian Burszley Oct 20 '17 at 20:52
  • Possible duplicate of [Positional Parameter error in powershell script](https://stackoverflow.com/questions/39407004/positional-parameter-error-in-powershell-script) – Jim G. May 17 '18 at 16:14
  • 1
    Please consider writing an answer to your own question if you feel the comments has helped you out... – Dennis Aug 24 '21 at 17:51
  • How about `cmd /c msiexec /x $id1 /qn KLPASSWD=randompass; $LASTEXITCODE` That will wait. – js2010 Oct 13 '22 at 12:15

2 Answers2

0

I usually put the Argumentlist into a variable to get rid of all issues with spaces, quotes and double quotes.

Since I don't have the actual software, you're trying to manipulate with msiexec, I can't give you a verified answer.

$properties = "identifyingnumber","name"
$filter = "Name LIKE '%Some software%'"

$getWMI = Get-WmiObject -Class win32_product -Filter $filter | 
  Select-Object -Property $properties | Sort-Object Name

if ($getWMI -and $getWMI.Length -eq 2) {
  $id0 = $getWMI[0].identifyingnumber
  $id1 = $getWMI[1].identifyingnumber~
  $arg = '"/X' $id1 '/QN KLPASSWD=randompass'"" 
  (Start-Process msiexec.exe -ArgumentList $arg -Wait).ExitCode  
} else {
  exit 
}

But your quotation is way of and will produce errors...

$arg = '"/X' $id1 '/QN KLPASSWD=randompass'""
ParserError:
Line |
   1 |  $arg = '"/X' $id1 '/QN KLPASSWD=randompass'""
     |               ~~~~
     | Unexpected token '$id1' in expression or statement.

The line below will be a valid string, but I don't know if that was what you had in mind...

$arg = '"/X' + $id1 + '/QN KLPASSWD=randompass' + ""

Dennis
  • 871
  • 9
  • 29
-1

Encase the Argument List in quotations to expand out your variable and then escape out the quotation with backticks to pass special characters

"/X $id1 /QN KLPASSWD=`"r4nd0mp@$$``""

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 21 '22 at 03:47