As promised, here are all the samples that I am showing in my MMS session on Command Shell. There are also a several samples I'm planning on mentioning but won't have time to show. Hope it's helpful.
Given the length of the this, chances are pretty good that I have an error somewhere. Please let me know if there's anything that doesn't work, if there's anything you think I forgot, or if you have any improvements to any of these scenarios.
Management Groups
Add a management group connection:
> new-managementGroupConnection OpsMgr02
Change your default management group connection:
> new-defaultManagementGroupConnection OpsMgr02 $true
View all current management group connections:
> get-ManagementGroupConnection
or
> cd \> dir
Management Packs
Export a specific management pack (sealed or unsealed):
> get-managementPack -name Microsoft.SQLServer.2005.Monitoring | export-managementPack -path c:\mp
Export all management packs in a management group:
> get-managementPack | export-managementPack -path c:\mp
Install a management pack to multiple management groups:
> new-managementGroupConnection OpsMgr02> cd \> install-managementPack c:\mp\Contoso.MyManagementPack.mp
Export management pack from test environment, seal it, and import to two production management groups:
> get-managementPack -name bwren.MMS | export-managementPack -path c:\mp> MPSeal.exe Contoso.MyManagementPack.xml /I c:\mp /Keyfile c:\keys\contosoKeyPair.snk /Company "Contoso"> new-managementGroupConnection ggOpsMgr01.greenGargantua.com> cd \ggOpsMgr01.greenGargantua.com> install-managementPack c:\mp\bwren.mms.xml> cd \OpsMgr01.bwren.com> get-managementGroupConnection | where {$_.managementServerName -eq 'ggOpsMgr01.greenGargantua.com'} | remove-managementGroupConnection
Agents
Install an agent on a single computer:
> $ms = get-managementServer | where {$_.name -eq 'OpsMgr02.GreenGargantua.com'}> install-agentByName -name 'srv01' -managementServer $ms
Install agents to a list of computers from a text file:
> $ms = get-managementServer | where {$_.name -eq 'OpsMgr02.GreenGargantua.com'} > gc c:\scripts\agents.txt | foreach {install-agentByName -name $_ -managementServer $ms}
Schedule install of agents to a list of computers from a text file.
Script: InstallAgents.ps1
param($file,$managementServer,$rmsServerName)
Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";Set-Location "OperationsManagerMonitoring::";$mgConn = New-ManagementGroupConnection -connectionString:$rmsServerNameSet-Location $rmsServerName
$ms = get-managementServer | where {$_.name -eq $managementServer}gc $file | foreach {install-agentByName -managementServer $ms -name $_}
Run from Task Scheduler:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "c:\scripts\InstallAgents.ps1" -file:'c:\scripts\computers.txt' -managementServer:OpsMgr02 -rmsServerName:'OpsMgr01'
Discover all domain controllers and install an agent on each:
> $ms = get-managementServer | where {$_.name -eq 'OpsMgr02.GreenGargantua.com'}> $discoveryConfig = new-ldapQueryDiscoveryCriteria -domain contoso.com -ldapQuery "(primaryGroupID=516)"> $discoveryResult = start-discovery -managementServer $ms -windowsDiscoveryConfiguration $discoveryConfig> $installResult = install-agent -managementServer $ms -agentManagedComputer $discoveryResult.CustomMonitoringObjects> $installResult.MonitoringTaskResults
Display the proxy setting for all agents with a particular string in the computer name:
> get-agent | where {$_.computerName -match 'dc'} | ft name,proxyingEnabled
Enable the proxy setting for all agents with a particular string in the computer name:
> $agents = get-agent | where {$_.computerName -match 'dc'} > $agents | foreach {$_.ProxyingEnabled = $true}> $agents | foreach {$_.ApplyChanges()}
Move all agents on a subnet to a different management server:
> $ms = get-managementServer | where {$_.name -eq 'OpsMgr02.greenGargantua.com'}> $agents = get-agent | where {$_.IPAddress -match '10.2.*.*'} > $agents | set-managementServer -primaryManagementServer $ms
Security
Get list of roles a user belongs to:
> get-userRole | where {$_.users -match 'iggy'} | ft name
Add list of users from a text file to a user role:
Script: AddUsersFromFile.ps1
param($file)
$entries = import-csv -path $file
foreach ($entry in $entries) { $role = get-userRole | where {$_.name -eq $entry.role} add-userToUserRole -userRole $role -user $entry.user}Sample Text File: users.txt
role,userMarketing,contoso\iggyMarketing,contoso\johnnyFinance,contoso\sidIT,contoso\trent
Running Script:
> c:\scripts\AddUsersFromFile -file c:\scripts\users.txt
Monitoring Classes and Objects
List all monitoring classes:
> get-monitoringClass | ft name
List all SQL 2005 databases:
> (get-monitoringClass -name Microsoft.SQLServer.2005.Database) | get-monitoringObject | ft name
OR
> (get-monitoringClass | where {$_.displayName -eq 'SQL 2005 DB'} | get-monitoringObject | ft name
List all Windows computers and their IP addresses:
> get-monitoringClass -name Microsoft.Windows.Computer | get-monitoringObject | foreach {$_."[Microsoft.Windows.Computer].IPAddress"}
List all monitoring classes in the SQL Server 2005 Discovery management pack
> $mp = get-managementPack -name Microsoft.SQLServer.2005.Discovery> $mp | get-monitoringClass | ft name
Display the proxy settings for all agents holding a particular class:
> $mc = get-monitoringClass -name 'Microsoft.Windows.Server.AD.DomainControllerRole'> $mos = $mc | get-monitoringObject > $mo | foreach {get-agent | where {$_.computerName -eq $mo.name}} | ft name,proxyingEnabled
Enable the proxy settings for all agents holding a particular class:
> $mc = get-monitoringClass -name 'Microsoft.Windows.Server.AD.DomainControllerRole'> $mos = $mc | get-monitoringObject > foreach ($mo in $mos) { $agent = get-agent | where {$_.computerName -eq $mo.displayName} $agent.proxyingEnabled = $true $agent.applyChanges()}
Maintenance Mode
Set maintenance mode for a single object:
> $StartTime = (get-Date '4/1/2008 22:00').ToUniversalTime()> $EndTime = (get-Date '4/2/2008 2:00').ToUniversalTime()> $mc = get-monitoringClass -name Microsoft.SQLServer.2005.Database > $mo = get-monitoringObject -monitoringClass $mc | where {$_.name -eq 'MyDatabase'}> new-maintenanceWindow -monitoringObject $mo -startTime $StartTime -endTime $EndTime -reason PlannedOther -comment "Scheduled maintenance."
Set maintenance mode for an object and all of its contained objects:
> $StartTime = (get-Date '4/1/2008 22:00').ToUniversalTime()> $EndTime = (get-Date '4/2/2008 2:00').ToUniversalTime()> $mc = get-monitoringClass -name Microsoft.Windows.Computer > $mo = get-monitoringObject -monitoringClass $mc | where {$_.name -eq 'srv01'}> $mo.ScheduleMaintenanceMode($StartTime,$EndTime,PlannedOther,"Nightly reboot.","Recursive")
Schedule maintenance mode for all computers in a group (uses ):
> C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "c:\scripts\GroupMM.ps1" -groupName:'Nightly Reboots' -hours:2 -rmsServerName:'OpsMgr01' -startMM:$true
View maintenance mode history for an object:
> $mc = get-monitoringClass -name Microsoft.Windows.Computer> $mo = get-monitoringObject -monitoringClass $mc | where {$_.name -eq 'srv01'}> $mo | get-maintenanceWindow -history
Tasks
Run task to enable audit collection on all agents:
> $task = get-task | where {$_.name -eq 'Microsoft.SystemCenter.EnableAuditCollectionService'}> $mc = get-monitoringClass | where {$_.name -eq 'Microsoft.SystemCenter.HealthService'}> $mc | get-monitoringObject | foreach {start-task -task $task -targetMonitoringObject $_}
Data
View all unresolved alerts:
> get-alert -criteria "ResolutionState <> 255"
View all alerts grouped by severity and name
> get-alert -criteria "ResolutionState <> 255" | sort severity,name | group severity,name
View alerts from a particular rule group by managed object:
> get-alert -criteria "ResolutionState <> 255" | where {$_.name -match 'Script or Executable Failed to run'} | group monitoringObjectDisplayName
Change resolution state of alerts from a particular rule group by managed object:
> get-alert -criteria "ResolutionState <> 255" | where {$_.name -match 'Script or Executable Failed to run'} | group monitoringObjectDisplayName | foreach { >> $_.ResolutionState = 50>> $_.update("Comment")>>}>>
Resolve all alerts generated by rules as opposed to monitors:
> get-alert -criteria "ResolutionState <> 255 and IsMonitorAlert = 'False'" | resolve-Alert
Reset health for a monitor called "Manual monitor" on all objects of the class "Contoso.MyCustomClass" currently in an Error state.
> $mon = get-monitor | where {$_.displayName -eq 'Manual monitor'}> $mc = get-monitoringClass -name Contoso.MyCustomClass> $mc | get-monitoringObject | where {$_.HealthState -eq 'Error'} | foreach {$_.ResetMonitoringState($mon)}
Performance Data
Extract processor utilization data for all computers for the month of March 2008 to a comma delimited file.
> $startTime = get-date '3/1/2008'> $endTime = get-date '3/31/2008'> $pc = get-performanceCounter -criteria: "ObjectName='Processor' and CounterName='% Processor Time' and MonitoringObjectPath='web02.bwren.com'"> get-performanceCounterValue -startTime $startTime -endTime $endTime -performanceCounter $pc | export-csv c:\scripts\mom\perf.csv
Management Pack Elements
List all rules by management pack:
> get-rule | select @{name="MP";expression={foreach-Object {$_.GetManagementPack().DisplayName}}},DisplayName | sort mp,displayName
List all monitors in a specific management pack:
> (get-managementPack -name Microsoft.SQLServer.2005.Monitoring) | get-monitor | sort displayName | ft displayName
List all disabled discoveries:
> get-discovery | where {$_.enabled -eq 'false'} | ft displayName
View the display name, category, and enabled status of all performance collection rules in the SQL Server 2005 Monitoring management pack:
> get-rule -managementPack $mp | where {$_.category -eq 'PerformanceCollection'} | ft displayName,category,enabled