We all suffer about the same.
Import-Module OperationsManager just takes too long - 13 seconds!
$(Get-Date)
Import-Module OperationsManager
$(Get-Date)
Tuesday, April 26, 2016 3:42:35 PM
Tuesday, April 26, 2016 3:42:48 PM
So, if you use powershell in your subscriptions in OpsMgr, to affect some customfileds with extra data, well ... it could be a problem if you have a lot of 'Impor-Modules' going on.So, i decided to put myself working on 'how to (not) use operations manager powershell module).
First things first.
Load your SDK. (DLL's)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager.Common") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.EnterpriseManagement.Core') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.EnterpriseManagement.OperationsManager') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.EnterpriseManagement.Runtime') | Out-Null
Now, let's open up a connection to your MS. $MGConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings($env:computername)
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSetting)
Now, inside this new object ($MG) you have it all, and i mean it, ALL!Let's say you want to list a specific list of alerts based on a specific criteria.
Well, untill now you used get-scomalert cmdlet ... now you use this :
$newTime = (Get-Date).AddHours(-24)
$Criteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringAlertCriteria("ResolutionState = 0 AND Severity >= 1 AND TimeRaised > `'$newTime`'")
$MG.GetMonitoringAlerts($Criteria)
Well, the difference ? 13 seconds!Old-way :
New-way :$(Get-Date) Import-Module OperationsManager $newTime = (Get-Date).AddHours(-24) $Criteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringAlertCriteria("ResolutionState = 0 AND Severity >= 1 AND TimeRaised > `'$newTime`'") $Alerts = Get-SCOMAlert -Criteria $Criteria $(Get-Date) Tuesday, April 26, 2016 3:54:06 PM Tuesday, April 26, 2016
3:54:19 PM
Other examples :$(Get-Date) $newTime = (Get-Date).AddHours(-24) $Criteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringAlertCriteria("ResolutionState = 0 AND Severity >= 1 AND TimeRaised > `'$newTime`'") $Alerts = $MG.GetMonitoringAlerts($Criteria) $(Get-Date) Tuesday, April 26, 2016 3:54:32 PM Tuesday, April 26, 2016
3:54:32 PM
Get-SCOMClassInstance
in our new way : $ClassCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.MonitoringClassCriteria("DisplayName = 'Test Class'")
$MonitoringClass = $MG.GetMonitoringClasses($ClassCriteria)
And related MonitoringObjects ? (Get-SCOMMonitoringObject) ? $MG.GetMonitoringObjects($MonitoringClass[0])