Affichage des articles dont le libellé est PowerShell. Afficher tous les articles
Affichage des articles dont le libellé est PowerShell. Afficher tous les articles

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

11 nov. 2011

OpsMgr2012 powershell cmdlets

With OM2012, we couple of new cmdlets has been introduced and other has been removed. You will probably have to rewrite some scripts...



70 cmdlets added


Get-SCClass 
Get-SCDiscovery 
Get-SCManagementGroupConnection 
Get-SCManagementPack 
Get-SCOMAccessLicense 
Get-SCOMAgent 
Get-SCOMAgentlessManagedComputer 
Get-SCOMAgentManagementServerSetting 
Get-SCOMAlert 
Get-SCOMAlertHistory 
Get-SCOMClass 
Get-SCOMClassInstance 
Get-SCOMCommand 
Get-SCOMConnector 
Get-SCOMDiagnostic 
Get-SCOMDiscovery 
Get-SCOMEvent 
Get-SCOMGatewayManagementServer 
Get-SCOMGroup 
Get-SCOMMaintenanceMode 
Get-SCOMManagementGroupConnection 
Get-SCOMManagementPack 
Get-SCOMManagementServer 
Get-SCOMMonitor 
Get-SCOMNotificationAction 
Get-SCOMNotificationEndpoint 
Get-SCOMNotificationRecipient 
Get-SCOMNotificationSubscription 
Get-SCOMOverride 
Get-SCOMOverrideResult 
Get-SCOMPendingManagement 
Get-SCOMRecovery 
Get-SCOMRelationship 
Get-SCOMRelationshipInstance 
Get-SCOMResourcePool 
Get-SCOMRMSEmulator 
Get-SCOMRule 
Get-SCOMRunAsAccount 
Get-SCOMRunAsProfile 
Get-SCOMTask 
Get-SCOMTaskResult 
Get-SCOMUserRole 
Get-SCRelationship 
Get-SCRelationshipInstance 
Import-SCManagementPack 
Import-SCOMManagementPack 
Install-SCOMAgent 
New-SCManagementGroupConnection 
New-SCOMManagementGroupConnection 
New-SCOMResourcePool 
Remove-SCManagementGroupConnection 
Remove-SCManagementPack 
Remove-SCOMAgentlessManagedComputer 
Remove-SCOMDisabledClassInstance 
Remove-SCOMManagementGroupConnection 
Remove-SCOMManagementPack 
Remove-SCOMResourcePool 
Remove-SCOMRMSEmulator 
Set-SCManagementGroupConnection 
Set-SCOMAgentlessManagedComputer 
Set-SCOMAgentManagementServerSetting 
Set-SCOMAlert 
Set-SCOMMaintenanceMode 
Set-SCOMManagementGroupConnection 
Set-SCOMResourcePool 
Set-SCOMRMSEmulator 
Set-SCOMRunAsProfile 
Set-SCOMUserRole 
Start-SCOMMaintenanceMode 
Start-SCOMTask 




List of cmdlets removed (replaced indeed)  :



Get-MonitoringObjectGroup 
Get-MonitoringObjectPath 
Get-MonitoringObjectProperty 
Get-NotificationAction 
Get-NotificationEndpoint 
Get-NotificationRecipient 
Get-NotificationSubscription 
Get-OperationsManagerCommand 
Get-Override 
Get-PerformanceCounter 
Get-PerformanceCounterValue 
Get-PrimaryManagementServer 
Get-Recovery 
Get-RelationshipClass 
Get-RelationshipObject 
Get-RemotelyManagedComputer 
Get-RemotelyManagedDevice 
Get-ResultantCategoryOverride 
Get-ResultantRuleOverride 
Get-ResultantUnitMonitorOverride 
Get-RootManagementServer 
Get-Rule 
Get-RunAsAccount 
Get-State 
Get-Task 
Get-TaskResult 
Get-Tier 
Get-UserRole 
Install-Agent 
Install-AgentByName 
Install-ManagementPack 
New-CustomMonitoringObject 
New-DeviceDiscoveryConfiguration 
New-LdapQueryDiscoveryCriteria 
New-MaintenanceWindow 
New-ManagementGroupConnection 
New-MonitoringPropertyValuePair 
New-Tier 
New-WindowsDiscoveryConfiguration 
Reject-AgentPendingAction 
Remove-ConnectorFromTier 
Remove-DisabledMonitoringObject 
Remove-ManagementGroupConnection 
Remove-RemotelyManagedComputer 
Remove-RemotelyManagedDevice 
Remove-Tier 
Resolve-Alert 
Set-AlertDestination 
Set-DefaultSetting 
Set-MaintenanceWindow 
Set-ManagementServer 
Set-ProxyAgent 
Start-Discovery 
Start-Task 
Uninstall-Agent 
Uninstall-ManagementPack 


1 sept. 2011

WSUS Administrator Powershell Module


I have just discovered a very useful module to manage and maintain WSUS.


Codeplex source : http://poshwsus.codeplex.com/


List of available commandlet : 

Add-WSUSClientToGroup
Approve-WSUSUpdate
Connect-WSUSServer
Convert-WSUSTargetGroup
Deny-WSUSUpdate
Disconnect-WSUSServer
Get-WSUSChildServers
Get-WSUSClient
Get-WSUSClientGroupMembership
Get-WSUSClients
Get-WSUSClientsInGroup
Get-WSUSCommands
Get-WSUSContentDownloadProgress
Get-WSUSDatabaseConfig
Get-WSUSDownstreamServers
Get-WSUSEmailConfig
Get-WSUSEvents
Get-WSUSGroup
Get-WSUSGroups
Get-WSUSServer
Get-WSUSStatus
Get-WSUSSubscription
Get-WSUSSyncHistory
Get-WSUSSyncProgress
Get-WSUSUpdate
Get-WSUSUpdateCategories
Get-WSUSUpdates
New-WSUSGroup
Remove-WSUSClient
Remove-WSUSClientFromGroup
Remove-WSUSGroup
Remove-WSUSUpdate
Resume-WSUSDownloads
Resume-WSUSUpdateDownload
Set-WSUSEmailConfig
Start-WSUSCleanup
Start-WSUSSync
Stop-WSUSDownloads
Stop-WSUSSync
Stop-WSUSUpdateDownload
New-WSUSInstallApprovalRule
Set-WSUSInstallApprovalRule
Get-WSUSInstallApprovalRules
Remove-WSUSInstallApprovalRule
Start-WSUSInstallApprovalRule
Connect-WSUSDatabaseServer
Test-WSUSDatabaseServerConnection
Get-WSUSUpdateClassifications


17 juil. 2011

Export the pinpoint MP's catalog into a single html page

If, in your company, people asks you all the time is there any management pack for monitoring this or this application and you give them all the time the link to the pinpoint catalog  (where you can find all provided MP by Microsoft or third party) but never take the time to click or search, than you are like me.... :)


You might need to publish this list on your sharepoint server (or any web server) or just send this list by mail.


That's why I have created this script to export the catalog in an HTML document to be shared in my organization.


The following script parse the pinpoint catalog and retreive the information in order to be exported in a simple HTML document.


Source script: here

###################################################################################
# Parsing and exporting the pinpoint systemcenter catalog to get it in one page #
# The purpose is to be able to distrib this catalog inside company's organization #
# #
# Author: Laurent De Berti #
# v1.0 - 17/7/2011 #
###################################################################################

# We need 2 Internet Explorer
# 1st one is to retreive the list of all MPs
# 2nd one is to retreive the description for each MP

$oIE=new-object -com internetexplorer.application
$oie2=new-object -com internetexplorer.application

#$count is the 1st page
$count = 1

$res=5
$baseurl="http://systemcenter.pinpoint.microsoft.com"
$mp=@()

# Looping for each page of Management Packs
while ($res -ne 1) {
"Page $count"

# Let's retreive all the page
$oIE.navigate($baseurl + "/en-US/applications/search/operations-manager-d11/management-packs-b200073?page="+$count+ "&sort=released&q=")

while ($oIE.busy) {
    sleep -milliseconds 250
}

# By using the HTML dl tag, we check how many MPs are return.

$res=0; $oIE.document.getElementsByTagName("dl") | % { $res+=1 }
$oIE.document.getElementsByTagName("dl") | % { 
$HTMLPart= $_.IHTMLElement_innerHTML
$title = $HTMLPart| select-string -Pattern 'title="(?<hid>[ 0-9a-zA-Z-]+)'| select -expand Matches | foreach {$_.groups["hid"].value}

# if title is set as "Best Match", we skip this line 'headline of the table
if ($title -ne "Best Match") { 

# start recording and parsing the web page for each MP
$record  = New-Object system.Object
$record | add-Member -memberType noteProperty -name ManagementPackName -Value $title
"Adding " + $title
#$record | add-Member -memberType noteProperty -name Description -Value ($HTMLPart| select-string -Pattern '<DD class=description><SPAN>(?<hid>[ 0-9a-zA-Z-:,.®]+)...'| select -expand Matches | foreach {$_.groups["hid"].value})
$record | add-Member -memberType noteProperty -name Company -Value ($HTMLPart| select-string -Pattern '<SPAN>Plug-in by: <SPAN class="fn org">(?<hid>[ 0-9a-zA-Z-:,.®]+)</SPAN>'| select -expand Matches | foreach {$_.groups["hid"].value})
$record | add-Member -memberType noteProperty -name ReleaseDate -Value ($HTMLPart| select-string -Pattern '<B>Release Date: </B>(?<hid>[ 0-9a-zA-Z-:,.®/]+) </DD>'| select -expand Matches | foreach {$_.groups["hid"].value})
$record | add-Member -memberType noteProperty -name Ver -Value ($HTMLPart| select-string -Pattern '<B>Version: </B>(?<hid>[ 0-9a-zA-Z-:,.®]+) </DD>'| select -expand Matches | foreach {$_.groups["hid"]value})
$downloadlink= $baseurl + ($HTMLPart | select-string -Pattern '="Application Details" href="(?<hid>[ 0-9a-zA-Z-:,./]+)"'| select -expand Matches | foreach {$_.groups["hid"].value})
$record | add-Member -memberType noteProperty -name DownloadLink -Value $downloadlink


# opening another IE to get the description from the link page
$oie2.navigate($downloadlink)
while ($oie2.busy) {
          sleep -milliseconds 250
}
$desc=($oie2.document.getElementsByTagName("p") | ? {$_.classname -eq "appDetailDescription"}).IHTMLElement_outerText

$record | add-Member -memberType noteProperty -name Description -Value $desc

# Adding the record to the new mp system.object
$mp+=$record
}
}


# Let's go the next page.
$count++

}

# $mp contains all the MP
# printing out the result
$mp

# exporting in HTML to get the catalog.
$head = '<style> BODY{font-family:Trebuchet; background-color:white;} H1{font-size:1.3em;} TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} TH{font-size:0.8em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#CEE3FF} TD{font-size:0.8em;border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#F7FFE8} </style>'

$mp | ConvertTo-HTML  -body "<P><H1>Here the full list of management pack in the pinpoint catalog</H1><P>" -head $head -title "pinpoint - list of managementpack"| out-file c:\mp.html

15 mars 2011

What management packs are applied to your SCOM agents

What management packs are applied to your agents ? It is something not really easy.
For each agent you retreive all objects discovered related to the agent, then you can retreive all the classes of thoses objects, so you are able to retreive the management applied to the Agent

$agent=Get-Agent
$max=$agent.count
$t=0
"Retreiving all Classes"
$AllMP = New-Object System.Collections.Hashtable

$servers=@()
$record = New-Object system.object
$record  | add-Member -memberType noteProperty -name "Server" -Value "NA"
Get-ManagementPack | where  {$_.sealed -eq $TRUE } | sort -Property Name |foreach {
    $mpname=$_.Name
    $record | add-Member -memberType noteProperty -name $mpname -Value "No"
    $mpname
    $_.GetClasses() |  foreach { $AllMP[$_.Name.tostring()]=$mpname }
  
    }
$servers+=$record

$agent |% {
    write-progress -activity "Dealing with"  -status "% Complete:" -percentcomplete ($t*100/$max)
    $record = New-Object system.object
    $record | add-Member -memberType noteProperty -name "Server" -Value $_.PrincipalName
    $_.HostComputer.GetRelatedMonitoringObjects() | % {
        $record | add-Member -memberType noteProperty -Force -name $AllMP[$_.GetLeastDerivedNonAbstractMonitoringClass().Name] -Value "Yes"
        }
    $servers+=$record
    $t++
}
$servers

Get all alerts from a management pack

For example, In SCOM if you want to retreive all alerts from a management packs, you can do something like this
$crit=""
$count=0
get-managementpack | ? { $_.Name -match "Citrix"} | % { $_.getclasses() | % {

    If ($count -eq 0) {$crit= "MonitoringClassId = '"+$_.id.tostring()+"'"; $count++}
    else { $crit= $crit + "OR MonitoringClassId = '"+$_.id.tostring()+"'" }
 }}
get-alert -criteria $crit

This powershell script retreive Citrix alerts...