installing msi on remote machines with parameters

rated by 0 users
This post has 4 Replies | 2 Followers

Top 500 Contributor
Posts 8
ismailahmed Posted: 11-27-2010 1:12 AM

Hi, Below is the script i am using for installing the msi on remote computers, the msi is already on those computers, the script does not work when i substitute the values with variables  but when i pass directly the values to the msi exec command the script works

$name=buildname

$num=buildnumber

$installdir="C:\data"

$msiname=$name-$num.msi
$srcpath ="C:\temp\$msiname"

$script={
                                                        #NOT WORKING#

##Invoke-Expression "msiexec /i $srcPath /qn ALLUSERS=1 REBOOT=ReallySuppress INSTALLDIR=$installDir SERVICEACCOUNT=$env:userdomain\$svcAcctName SERVICEPASSWORD=$svcAcctPwd /L* $installDir\$buildNum\$buildNum.log"

                                     #WORKING#

Invoke-Expression "msiexec.exe /i e:\temp\stage\NSA-Encryption-20101124.004.msi /qn ALLUSERS=1 REBOOT=ReallySuppress INSTALLDIR=e:\temp\data\AAA\website1 SERVICEACCOUNT=ABCD SERVICEPASSWORD=efgh"


}
$serverEnvList= "C27"
 $allsession=""
foreach ($server in $serverEnvList)
{
Write-Output($server)
 
}
$allsession = New-PSSession -Name $server -ComputerName $serverEnvList
Write-Host($script)
Invoke-Command -ScriptBlock $script -Session $allsession

 

Please help me in this regard

 

Top 10 Contributor
Posts 638
Microsoft MVP
Top Contributor

You are submitting your command to Invoke-Command as script block (inside curly braces). So the entire code is sent to the remote machine BEFORE it is ever processed by PowerShell.

Your variables exist on the calling machine. When the code is executed, it is already on the REMOTE machine, and there the variables do not exist.

What you would have to do is create the whole script block including all input parameters on your CLIENT before you send it over to the TARGET. You can create a script block by first constructing it as a string:

$code = "somecommand $somevariable"

Then you convert the string to script block:

$scriptblock = [Scriptblock]::Create($code)

Now you can send the scriptblock to the remote machine using Invoke-Command.

Hope that helps,

Tobias

Top 10 Contributor
Posts 638
Microsoft MVP
Top Contributor

Another approach would be using WMI to remotely install MSIs. This works when you have access permissions and the Win32_Product WMI class is present on the target machine. It does not require PS remoting at all.

Here is how:

PS> $msi = [wmiclass]'\\demo5\root\cimv2:Win32_Product'
PS> $msi.Install


MemberType          : Method
OverloadDefinitions : {System.Management.ManagementBaseObject Install(System.St
                      ring PackageLocation, System.String Options, System.Boole
                      an AllUsers)}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : System.Management.ManagementBaseObject Install(System.Str
                      ing PackageLocation, System.String Options, System.Boolea
                      n AllUsers)
Name                : Install
IsInstance          : True

 

PS> $msi.Install('c:\pathtomsi.msi', 0, $true)

In the first line, replace "demo5" with the remote machine name.

 

 

Not Ranked
Posts 1

Tobias:

Another approach would be using WMI to remotely install MSIs. This works when you have access permissions and the Win32_Product WMI class is present on the target machine. It does not require PS remoting at all.

Here is how:

PS> $msi = [wmiclass]'\\demo5\root\cimv2:Win32_Product'

This is a simpler approach, but I don't believe it works on Server 2003.

Also, Win32_Product is bad:

http://sdmsoftware.com/wmi/why-win32_product-is-bad-news/

 

Top 100 Contributor
Posts 38

Make your script block correctly then you can do:

$Script = {

$stuff = "Location\Location\Location"

$command = "cmd $stuff /foo"

}

$Computers = @("Comp1","Comp2","Comp3")

Invoke-Command -ComputerName $Computers -ScriptBlock $script -ThrottleLimit 50

 

http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/23/an-introduction-to-powershell-remoting-part-one.aspx

Page 1 of 1 (5 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.