System Center Operations Manager 2007: Powershell Scripts


Jump to: navigation, search


Powershell Simple Commands

Get all Powershell Commands:         Get-Command
Get all Operation Manager Commands:  Get-OperationsManagerCommand
Get all Rules:   		      Get-Rule
Get all Monitors:		      Get-Monitor
Get all Management Packs	      Get-ManagementPack

List all machines with Health Service Issues

# Get a list of all machines with have problems with the helth service.                 

$filename = "c:\temp\output\computer_list.txt"
$computer_list = get-alert | where {$_.MonitoringRuleId -eq "b59f78ce-c42a-8995-f099-e705dbb34fd4" -and `
                 $_.ResolutionState –eq 0} | Select-Object MonitoringObjectDisplayName;
$computer_list | out-file $filename;

Register Operations Manager 2007 Snapin

Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client
New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root: \ 
cd Monitoring:\
New-ManagementGroupConnection localhost 
cd localhost

Export All Management Packs

$all_mps = get-managementpack;
mkdir "C:\temp\mpexport";
foreach($mp in $all_mps)
  export-managementpack -managementpack $mp -path "C:\temp\mpexport";

Export All Management Packs (Sealed)

$mps = get-managementpack | where {$_.Sealed -eq $true}
mkdir "C:\temp\mpexport";
foreach($mp in $mps)
  export-managementpack -managementpack $mp -path "C:\temp\mpexport"

List All Management Packs Installed (Different versions)

Get-ManagementPack | Format-Table Name,FriendlyName,Version,Sealed -AutoSize
Get-ManagementPack | Format-Table -AutoSize
Get-ManagementPack | Format-Table Name,FriendlyName,Version,Sealed -AutoSize | Out-File "c:\mplist.txt"
Get-ManagementPack | Format-Table Name,Version -AutoSize | Out-File "c:\mplist.txt"
Get-ManagementPack | Sort -property FriendlyName | Format-Table FriendlyName,Version,Sealed -AutoSize | Out-File "c:\mplist_FriendlyName.txt"
Get-ManagementPack | Sort -property Name | Format-Table Name,Version,Sealed -AutoSize | Out-File "c:\mplist_Name.txt"

Change to a differenet management server

# Change to the current provider to server: Server1
New-DefaultManagementGroupConnection Server1 $False $True;

# Change to the current provider to server: Server2
New-DefaultManagementGroupConnection Server2 $False $True;

# Change to the current provider to server: Server3
New-DefaultManagementGroupConnection Server3 $False $True;

List all overrides and export to CSV

get-managementpack | get-override | Select-object *,@{Name = 'Management Pack (Friendly Name)'; `
    expression ={$_.GetManagementPack().FriendlyName}},@{Name = 'Management Pack (Name)'; `
    expression ={$_.GetManagementPack().Name}},@{Name = 'Management Pack (ID)'; `
    expression ={$_.GetManagementPack().ID}} | export-csv c:\temp\all_overrides.csv -noTypeInformation;

List all rules and export to CSV

get-rule | Select-object *,@{Name = 'Management Pack (Friendly Name)'; `
    expression ={$_.GetManagementPack().FriendlyName}},@{Name = 'Management Pack (Name)'; `
    expression ={$_.GetManagementPack().Name}},@{Name = 'Management Pack (ID)'; `
    expression ={$_.GetManagementPack().ID}} | export-csv c:\temp\all_rules.csv -noTypeInformation;

List all monitors and export to CSV

get-monitor | Select-object *,@{Name = 'Management Pack (Friendly Name)'; `
    expression ={$_.GetManagementPack().FriendlyName}},@{Name = 'Management Pack (Name)'; `
    expression ={$_.GetManagementPack().Name}},@{Name = 'Management Pack (ID)'; `
    expression ={$_.GetManagementPack().ID}} | export-csv c:\temp\all_monitors.csv -noTypeInformation;

Export all monitoring objects and classes to text and CSV files

Get-MonitoringObject | out-file c:\temp\all-monitoring-objects.txt;
Get-MonitoringObject | export-csv c:\temp\all-monitoring-objects.csv -noTypeInformation;
Get-MonitoringClass | out-file c:\temp\all-monitoring-classes.txt;
Get-MonitoringClass | export-csv c:\temp\all-monitoring-classes.csv -noTypeInformation;

Convert a OpsMgr 2007 knowledge article to HTML

# kb.ps1
# Very basic needs more work..

function fnMamlToHTML($MAMLText)
 $HTMLText = "";
 $HTMLText = $MAMLText -replace('xmlns:maml=""');
 $HTMLText = $HTMLText -replace("maml:para","p");
 $HTMLText = $HTMLText -replace("maml:");
 $HTMLText = $HTMLText -replace("</section>");
 $HTMLText = $HTMLText -replace("<section>");
 $HTMLText = $HTMLText -replace("<section >");
 $HTMLText = $HTMLText -replace("<title>","<h2>");
 $HTMLText = $HTMLText -replace("</title>","</h2>");
 $HTMLText = $HTMLText -replace("<listitem>","<li>");
 $HTMLText = $HTMLText -replace("</listitem>","</li>");
 $HTMLText = "<html><body>" + $HTMLText + "</body></html>";

# Mainline 

# Clear the screen.

# Get US Cultrure information.
$ciUS = [System.Globalization.CultureInfo]'en-US';

# Read in a rule.
write-host "Reading Rule...";

# Retrieve the "Alert on Failed Batch Responses" rule.
# This would work as well..
# $rule = get-rule | where {$_.DisplayName -eq 'Root Management Server Unavailable'};

$rule = get-rule | where {$_.Id -eq 'a1297c9f-b5bc-e817-ec77-cc246008d2af'};

# Retrieve the knowledge Atricle.

$article = $rule.GetKnowledgeArticle($ciUS);

if ($article -ne $Null)
 if ($article.MamlContent -ne $Null)
   $article_text = $article.MamlContent;
   $article_text = fnMAMLtoHTML($article_text);
 write-host "Outputing HTML...";
 $article_text.tostring() | out-file "c:\temp\output\article.htm";
 # Load up Internet Explorer and display the result.
 invoke-item "c:\temp\output\article.htm";

Export all management packs to XML

function fnCreateTemporaryDirectory
	# Generate Random file name.
	# $random_filename = [System.IO.Path]::GetRandomFileName();
	$sub_directory = "mpexport";
	# Get the local Temporary Path
	$temp_file_path = [System.IO.Path]::GetTempPath();
	$file_path = $temp_file_path + $sub_directory;
	$erroractionpreference = "SilentlyContinue";
	mkdir $file_path | out-null;
	$erroractionpreference = "Continue";

function fnDeleteDirectory($path)
	$erroractionpreference = "SilentlyContinue";
	rd $path;
	$erroractionpreference = "Continue";

function fnExportManagementPack($management_pack,$path)
	write-host "Exporting Management Pack: " -nonewline -foregroundcolor yellow;
        write-host $management_pack.FriendlyName -foregroundcolor green;
	export-managementpack -managementpack $management_pack -path $path;

#$file_path = fnCreateTemporaryDirectory;
#write-host "Creating Directory: " $file_path;
$file_path = "C:\Temp\mpexport";
write-host "Retrieving Managent Packs, please wait...." -foregroundcolor green;
$all_mps = get-managementpack;

foreach($mp in $all_mps)
  fnExportManagementPack $mp $file_path;

Site Changes

Site Navigation

Personal tools