Search This Blog

Thursday 25 September 2014

Operations Manager 2012 – Useful PowerShell Commands and Scripts


As of 01/05/2017, this blog will not be updated or maintained

Here are some useful PowerShell Commands and Scripts that can help you in your environment.

Note : Change the values in red to the standards of your environment.

Install Prerequisites 2012/2012r2 Management Server and/or Console:

$dwnld = 'C:\File_Download_Location\SCOM2012R2Prereqs'
if (!(Test-Path -path $dwnld))
 {
 New-Item $dwnld -type directory
 }
$object = New-Object Net.WebClient
$RPTurl = 'http://download.microsoft.com/download/F/B/7/FB728406-A1EE-4AB5-9C56-74EB8BDDF2FF/ReportViewer.msi'
$object.DownloadFile($RPTurl, "$dwnld\ReportViewer.msi")
  
$RPTurl = 'http://go.microsoft.com/fwlink/?LinkID=239644&clcid=0x409'
$object.DownloadFile($RPTurl, "$dwnld\SQLSysClrTypes.msi")
  
Start-Process -FilePath "$dwnld\SQLSysClrTypes.msi" -ArgumentList '/q' -Wait
Start-Process -FilePath "$dwnld\ReportViewer.msi" -ArgumentList '/q' -Wait


Install Prerequisites 2012/2012R2 Web Console:
This script assumes the source files for the .NET installation are on S:\Software\Tools\sxs.

Import-Module ServerManager
Add-WindowsFeature NET-Framework-Core,Web-Static-Content,Web-Default-Doc,Web-Dir-Browsing,Web-Http-Errors,Web-Http-Logging,Web-Request-Monitor,Web-Filtering,Web-Stat-Compression,Web-Mgmt-Console,Web-Metabase,Web-Asp-Net,Web-Windows-Auth,Web-ASP,Web-CGI -source S:\Software\Tools\sxs -Restart
Add-WindowsFeature Web-Asp-Net45,AS-HTTP-Activation

Back-up Unsealed Management packs:

Import-Module Operationsmanager
Get-SCOMManagementPack | where {$_.Sealed -eq $false} | export-SCOMmanagementpack -path C:\Backup_Location\MPBackups

Display primairy and failover servers for all Gateway servers:

#Display Primary and Failover Management Servers for all Gateway Servers
$GWs = Get-SCOMManagementServer | where {$_.IsGateway -eq $true}
$GWs | sort | foreach {
Write-Host "";
"Gateway MS :: " + $_.Name;
"--Primary MS :: " + ($_.GetPrimaryManagementServer()).ComputerName;
$failoverServers = $_.getFailoverManagementServers();
foreach ($managementServer in $failoverServers) {
"--Failover MS :: " + ($managementServer.ComputerName);
}
}
Write-Host "";

Configure failover for Gateway Servers:
This script can be used for configuring primairy and failover management server for a gateway. The script is run on the management server.

$primaryMS = Get-SCOMManagementServer | where {$_.Name –eq '<primairy ms>'}
$failoverMS = Get-SCOMManagementServer | where {$_.Name –eq '<secundairy ms>'}
$gatewayMS = Get-SCOMManagementServer | where {$_.IsGateway -eq $true}
Set-SCOMParentManagementServer -GatewayServer: $gatewayMS -PrimaryServer: $primaryMS
Set-SCOMParentManagementServer -GatewayServer: $gatewayMS -FailoverServer: $failoverMS

Count all closed alerts created by monitors in the last X days:

$targetdate = (get-date).AddDays(-5)
(Get-SCOMAlert -criteria 'ResolutionState = "255" AND IsMonitorAlert = "True"'| Where-Object {$_.LastModified -gt $targetdate }).count

Disable a specific monitor:

$MP = Get-SCOMManagementPack -displayname "<override management pack>" | where {$_.Sealed -eq $False}
$Class = Get-SCOMClass -DisplayName "<class name>"
$Monitor = Get-SCOMMonitor -DisplayName "<monitor name>"
Disable-SCOMMonitor -Class $Class -ManagementPack $MP -Monitor $Monitor
$Monitor = Get-SCOMMonitor -DisplayName "<monitor name>"
Disable-SCOMMonitor -Class $Class -ManagementPack $MP -Monitor $Monitor

Enable a specific monitor:

$MP = Get-SCOMManagementPack -displayname "<override management pack>" | where {$_.Sealed -eq $False}
$Class = Get-SCOMClass -DisplayName "<class name>"
$Monitor = Get-SCOMMonitor -DisplayName "<monitor name>"
Enable-SCOMMonitor -Class $Class -ManagementPack $MP -Monitor $Monitor
$Monitor = Get-SCOMMonitor -DisplayName "<monitor name>"
Enable-SCOMMonitor -Class $Class -ManagementPack $MP -Monitor $Monitor

Hope that this post was helpful.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.