25 nov. 2011

Compare Management Packs version on 2 management groups

The Following powershell script help you to compare management packs from 2 environments (like prod and preprod env).


In argument of the script:
  1. RMS Name of the 1st Management Group
  2. RMS Name of the 2nd Management Group
  3. Name of the csv file
Here is the script:
param([string]$ref, [string]$diff, [String]$csvfile)

function Connect-OpsMgr ([string] $rootMS ) {

#Initializing the Ops Mgr 2007 Powershell provider
 if ( (Get-pssnapin | where {$_.Name -eq "Microsoft.EnterpriseManagement.OperationsManager.Client" }).Name -ne "Microsoft.EnterpriseManagement.OperationsManager.Client" ) {
  add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin; }
 Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin;
 $CS = Gwmi Win32_ComputerSystem -Comp "."
 
 #$cred=get-credential $CS.Username
 
 $mg=New-ManagementGroupConnection -ConnectionString:$rootMS -Credential:$cred -ErrorVariable errSnapin;
 Set-Location $rootMS -ErrorVariable errSnapin;
 
 #Checks to see if it failed or succedded in loading the provider
 if ($errSnapin.count -ne 0)
  {   
   Write-Output "OpsMgr 2007 PSSnapin failed initialize!"
   Write-Output "Please verify you are running this script on a computer that have the console installed" 
   Write-Output "Instance Maintenance Mode failed due:`nPowershell was not able to connect to OpsMgr platform.`nImpossible to put in mainteance mode the object [$Computer]::$object.`nContact SP Windows to reinstall the powershell and the OpsMgr console" "MaintenanceMode" "Error" 1000
  
  }
}

#Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin;
$cred = get-credential 
"Getting Management Group information for "+$ref
Connect-OpsMgr $ref
"Retreiving Management Packs from " + $ref
$mp1=get-managementpack

#$mg1=(Get-ChildItem . |where {$_.managementservername -eq $ref }).PathName
#"Path" + $mg1
"Getting Management Group information for "+$diff
Connect-OpsMgr $diff
# $mg2=(dir |where {$_.managementservername -eq $diff }).PathName
"Retreiving Management Packs from " + $diff
$mp2=get-managementpack
$MPREF = New-Object System.Collections.Hashtable
$mp1 | % { $MPREF[$_.Name.Tostring()] = $_.Version.tostring() }
$MPDIFF = New-Object System.Collections.Hashtable
$mp2 | % { $MPDIFF[$_.Name.Tostring()] = $_.Version.Tostring() }
$CompareMP = @()

foreach ($mp in $mp1) {
 $count=0
 $mp.Name
 $MPDIFF[$mp.Name]
 
 $record = New-Object System.Object
 if ($MPDIFF[$mp.Name] -eq $null) {
 
  $record | add-Member -memberType noteProperty -name Name -Value $mp.Name
  $record | add-Member -memberType noteProperty -name $ref -Value $mp.Version
  $record | add-Member -memberType noteProperty -name $diff -Value 'Missing'
  $record | add-Member -memberType noteProperty -name Sealed -Value $mp.Sealed
  $record | add-Member -memberType noteProperty -name "Status" -Value "Need to be installed"
  $CompareMP+=$record
 } elseif ($MPDIFF[$mp.Name.tostring()] -ne $MPREF[$mp.Name]) {
    
    $record | add-Member -memberType noteProperty -name Name -Value $mp.Name
    $record | add-Member -memberType noteProperty -name $ref -Value $MPREF[$mp.Name]
    $record | add-Member -memberType noteProperty -name $diff -Value $MPDIFF[$mp.Name] 
    $record | add-Member -memberType noteProperty -name Sealed -Value $mp.Sealed
    
    $record | add-Member -memberType noteProperty -name Status -Value "Diff"
    $CompareMP+=$record
    }
}
foreach ($mpd in $mp2) {
 if ($MPREF[$mpd.Name.tostring()] -eq $null) {
  $record = New-Object System.Object
  $record | add-Member -memberType noteProperty -name Name -Value $mpd.Name
  $record | add-Member -memberType noteProperty -name $ref -Value 'Missing'
  $record | add-Member -memberType noteProperty -name $diff -Value $mpd.Version
  $record | add-Member -memberType noteProperty -name Sealed -Value $mpd.Sealed
  $record | add-Member -memberType noteProperty -name "Status" -Value "Need to be installed"
  $CompareMP+=$record
 }

}


"Exporting results to " +  $csvfile
$CompareMP | sort -property Name | Export-Csv  -noTypeInformation -Path $csvfile

Aucun commentaire: