Fun with PowerShell: Installing the OpsMgr 2007 Agent

I ran into an interesting dilemma yesterday.  While converting my two OpsMgr 2007 Agent install scripts from batch file to PowerShell I hit a snag with the most important part of the script, the actual installation of the OpsMgr Agent.

To manually install the OpsMgr Agent with customizations you have to use msiexec.exe with a ton of flags.  Doing a cut & paste of the install line from the batch file was a resounding failure.  So was using an invoke-expression and several other methods I tried.  Eventually I ran into this forum post which gave me the solution I needed.

http://www.vistax64.com/powershell/101954-executing-cmd-exe-powershell.html

I'm not going to bother with the entire script, but my installOMAgent function ended up looking like this:



Made with the Online Syntax Highlighter

  1. function installOMAgent {
  2.  # WMI call to get OS info
  3.  $os = get-wmiobject -class "Win32_OperatingSystem" -namespace "root\CIMV2"
  4.  # check OS architecture version, store result in $arch
  5.  if ($os.OSArchitecture -eq "64-bit" -or $os.Caption -match "x64") {
  6.   $arch = 64
  7.  } else {
  8.   $arch = 32
  9.  }
  10.  # set install dir
  11.  $INSTALLDIR = 'D:\Program Files\System Center Operations Manager 2007'
  12.  # set installer arguments
  13.  $argue = 'USE_SETTINGS_FROM_AD=0 MANAGEMENT_GROUP=MgtGrp MANAGEMENT_SERVER_DNS=server.domain.top SECURE_PORT=999999 ACTIONS_USE_COMPUTER_ACCOUNT=1'
  14.  switch ($arch) {
  15.   32 {
  16.    # generates the install string for the x86 client
  17.    $call = "msiexec`.exe `/i V:`\x86`\MOMAgent`.msi `/qn `/promptrestart INSTALLDIR=`"$INSTALLDIR`" $argue"
  18.   }
  19.   64 {
  20.    # generates the install string for the x64 client
  21.    $call = "msiexec`.exe `/i V:`\AMD64`\MOMAgent`.msi `/qn `/promptrestart INSTALLDIR=`"$INSTALLDIR`" $argue"
  22.   }
  23.    default {"Unsupported operating system architecture.";exit}
  24.  }
  25.  # installs the agent
  26.  $p = [diagnostics.process]::Start("cmd.exe","/c start /wait $call")
  27.  $p.WaitForExit()
  28.  # if 2008 then add the firewall rule
  29.  if ($os.version.chars(0) -eq "6") {
  30.   "Adding 2008 firewall rule..."
  31.   netsh advfirewall firewall add rule name="OpsMgr Agent" action=allow protocol=TCP dir=in localport=999999
  32.  }
  33. }


The code is fairly easy to follow.  First, I pull the OS information using a WMI call, then determine if the OS in question is 32- or 64-bit.  The $arch variable is then created to determine the MSI path for the appropriate agent version.  I then break down the agent install line into variables.  $INSTALLDIR sets the install location, and $argue sets the rest of the custom installation arguments I chose for the installation.

From there I run a switch statement to finish the installation command-line based on what operating system architecture is present and then store that finished string in variable named $call.  The installation then runs, pausing the script during so it does not continue until the application is present.  Lastly I run one last check to see if the OS is Windows Server 2008. If it is I add the firewall rule needed for the health service to work in the prescribed bi-directional manner.

That's it!  Piece of cake.  As with all PowerShell functions, this little snippet of code can easily be modified for any number of .msi packages.  Just tweak the contents of a few variables, dump the code into a different installation script and viola!  New installer script in record time.

#James Kehr
Get-Member $OW | ?{$_.title -eq "System Administrator"`
-and $_.certification -contains 'MCSE 2000, MCDST, Network+, A+'}

New-Variable -name company -value 'ORCS Web, Inc.' -description www.orcsweb.com | 1.888.313.9421’

blog comments powered by Disqus