0
msiexec /i "example.msi" /q UserName="my username" password="my password"

The above command is executed perfectly in the command line but I need to execute in the powershell in a single line. So any one please suggest. I am new to the powershell.

I have tried below one:

Start-Process -FilePath msiexec -ArgumentList / /i, example.msi, "UserName='my username'","password='my password" -Wait

but it shows some issue:

Start-Process : A positional parameter cannot be found that accepts argument 'System.Object[]'.
At line:1 char:1
+ Start-Process -FilePath msiexec -ArgumentList / /i, "'example.msi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
karthik
  • 1
  • 3

3 Answers3

1

You've nearly got it, try this:

start msiexec -ArgumentList 
    "/i example.msi /q UserName=""my username"" password=""my password"""

If any of your arguments need quotes, you need to double them up.

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Hi @Balic C: It is working fine and also please suggest on how to run this msi to the client systems. – karthik Apr 05 '17 at 13:48
  • Cool, you can either use Joseph's method i.e. `Invoke-Command`, or you can use `psexec`, which I've had a lot of success with. – Bali C Apr 05 '17 at 14:10
  • I have used invoke-command but the output is showing nothing Invoke-Command -ComputerName $computer -ScriptBlock {msiexec /i "\\servername\pathToFile\example.msi" /q UserName="my username" password="my password"} – karthik Apr 06 '17 at 10:13
  • It won't unless there is an error, you are also using the quiet switch on msiexec. Try saving the Invoke command to a variable, then check that? – Bali C Apr 06 '17 at 10:24
0

Try using the "Invoke-Command" cmdlet. Should look something like this:

$computerlist = get-content c:\temp\ComputerNames.txt
foreach ($computer in $computerlist) {
    Invoke-Command -ComputerName $computer -ScriptBlock {msiexec /i "\\servername\pathToFile\example.msi" /q UserName="my username" password="my password"}
}

Try looking at the help for this command.

Joseph
  • 609
  • 2
  • 12
  • 26
  • Thank you it is working I have one more doubt I want run the msi from the server to execute in the clients systems. – karthik Apr 05 '17 at 13:43
  • @karthik Just provide exact UNC path to *.msi. – Vesper Apr 05 '17 at 13:44
  • But how the server find to which client have to run the *.msi – karthik Apr 05 '17 at 13:47
  • So if you are going to run this on multiple computers, you'll need a text file with the computer names in them and run my updated answer. – Joseph Apr 05 '17 at 14:10
  • Hi @Joseph : Your command is executed without any error but nothing showing in the client system. Even the log also not printed. – karthik Apr 06 '17 at 09:34
  • Invoke-Command -ComputerName karthik-r -ScriptBlock {msiexec /i "example.msi" /q UserName="my username" password="my password" /log C:\karthik.txt} – karthik Apr 06 '17 at 09:36
  • Can't say I'm 100% certain why. Might have something to do with the content inside the -ScriptBlock parameter. See if you can find anything on Google. – Joseph Apr 06 '17 at 09:47
  • Try looking at this question to see if it helps: http://stackoverflow.com/questions/8448808/using-invoke-command-scriptblock-on-a-function-with-arguments – Joseph Apr 06 '17 at 09:49
0

In general when you see parameters such as arguments or argumentslist always keep in mind that an array is expected. Same for the `& path args' format.

The cool thing about the array is that each item will be offered to the executable as if it was implicitly quoted, similar to you surrounding with quotes a parameter in command prompt that contains a space.

For this reason I always use structures like

$arguments=@($var1,$var2)

Like this I never have to worry about what is in the variables. When creating the array in multi line and depending on each case it also makes easier the change management because adding a parameter is a line change. It also helps with commenting one out when troubleshooting

For example

$arguments=@(
    $var1
    $injectedVar
    #$disabled
    $var2
 )
Alex Sarafian
  • 634
  • 6
  • 17