Thursday, April 4, 2013

[OpsMgr 2007 R2] Powershell script to export all product knowledge for rule and monitor for a specific MP

I've been asked to provide knowledge article to application owner in order to help them to determine what instruction to give to support level one for alert raised by SCOM management pack.

With no access to SCOM console, I didn't find an easy way to do it and I also decided to proceed with a powershell script.



Here are the command line I've written to export all product knowledge for rules and monitors for a specific MP. Create a PS1 file named ExportKnowledgeArticleFromAMP.ps1 and copy paste the command line from this post and go down and click download to get the PS1 file.

Usage a this script needs 2 parameters : MP name and Path to create files.
Example:
.\ExportKnowledgeArticleFromAMP.ps1 "Microsoft.Windows.Server.PrintServer.2012" "C:\Temp"

  1. param([string]$MPName, [string]$Path)
  2. # Script to export all product knowledge for rule and monitor for a specified MP 
  3. # usage : .\ExportKnowledgeArticleFromAMP.ps1 "Microsoft.Windows.Server.PrintServer.2012" "C:\Temp"
  4. function MamlToHTML($MAMLText)
  5. {
  6.  $HTMLText = "";
  7.  $HTMLText = $MAMLText -replace('xmlns:maml="http://schemas.microsoft.com/maml/2004/10"');
  8.  $HTMLText = $HTMLText -replace("maml:para","p");
  9.  $HTMLText = $HTMLText -replace("maml:");
  10.  $HTMLText = $HTMLText -replace("</section>");
  11.  $HTMLText = $HTMLText -replace("<section>");
  12.  $HTMLText = $HTMLText -replace("<section >");
  13.  $HTMLText = $HTMLText -replace("<title>","<h2>");
  14.  $HTMLText = $HTMLText -replace("</title>","</h2>");
  15.  $HTMLText = $HTMLText -replace("<listitem>","<li>");
  16.  $HTMLText = $HTMLText -replace("</listitem>","</li>");
  17.  $HTMLText = "<html><body>" + $HTMLText + "</body></html>";
  18.  $HTMLText;
  19. }
  20. # Mainline
  21. # Clear the screen.
  22. cls;
  23. # Get US Culture information.
  24. $ciUS = [System.Globalization.CultureInfo]'en-US';
  25. # Retrieve the Management Pack.
  26. $mps = get-managementpack
  27. $mp = $mps | ? { $_.Name -eq $MPName}
  28. $FolderName = $MPName + " - " + $mp.version
  29. # Create Folder
  30. New-Item -ItemType directory -Path $Path\$FolderName
  31. # Retrieve the Management Pack rules and monitors.
  32. $rules = $mp.getrules()
  33. $monitors = $mp.getmonitors()
  34. cls
  35. # Retrieve the knowledge Article for rules.
  36.  $i = 1
  37.  $j = 1
  38. foreach ($rule in $rules) {
  39.  $article = $rule.GetKnowledgeArticle($ciUS);
  40.  if ($article -ne $Null)
  41.  {
  42.   if ($article.MamlContent -ne $Null)
  43.   {
  44.     $article_text = $article.MamlContent;
  45.     $article_text = MamlToHTML($article_text);
  46.   }
  47.   write-host "Outputing HTML...";
  48.   if ($rule.DisplayName -ne "")
  49.     {
  50.     $RuleName = "Rule - " + [string]$i + " - " + [string]$rule.DisplayName
  51.     }
  52.   else
  53.     {
  54.     $RuleName = "Rule - " + [string]$i + " - " + [string]$rule.Name
  55.     }
  56.   # < > : " / \ | ? * removal
  57.    $RuleName
  58.   $RuleName = $RuleName.replace('<','lower than')
  59.   $RuleName = $RuleName.replace('>','Greater than')
  60.   $RuleName = $RuleName.replace('/','')
  61.   $RuleName = $RuleName.replace('|','')
  62.   $RuleName = $RuleName.replace('\','')
  63.   $RuleName = $RuleName.replace('!','')
  64.   $RuleName = $RuleName.replace('?','')
  65.   $RuleName = $RuleName.replace('*','')
  66.   $RuleName = $RuleName.replace(':','')
  67.   $RuleName = $RuleName.replace(';','')
  68.   $ExportFile = $Path + "\" + $FolderName + "\" + $RuleName + ".htm";
  69.   $article_text.tostring() | out-file $ExportFile
  70.   $i = $i + 1
  71.  }
  72. }
  73. # Retrieve the knowledge Article for monitors.
  74. foreach ($monitor in $monitors) {
  75.  $article = $monitor.GetKnowledgeArticle($ciUS);
  76.  if ($article -ne $Null)
  77.  {
  78.   if ($article.MamlContent -ne $Null)
  79.   {
  80.     $article_text = $article.MamlContent;
  81.     $article_text = MamlToHTML($article_text);
  82.   }
  83.   write-host "Outputing HTML...";
  84.    if ($monitor.DisplayName -ne "")
  85.     {
  86.     $MonitorName = "Monitor - " + [string]$j + " - " + [string]$monitor.DisplayName
  87.     }
  88.    else
  89.      {
  90.     $MonitorName = "Monitor - " + [string]$j + " - " + [string]$monitor.Name
  91.     }
  92.   # < > : " / \ | ? * removal
  93.    $MonitorName
  94.   $MonitorName = $MonitorName.replace('<','lower than')
  95.   $MonitorName = $MonitorName.replace('>','Greater than')
  96.   $MonitorName = $MonitorName.replace('/','')
  97.   $MonitorName = $MonitorName.replace('|','')
  98.   $MonitorName = $MonitorName.replace('\','')
  99.   $MonitorName = $MonitorName.replace('!','')
  100.   $MonitorName = $MonitorName.replace('?','')
  101.   $MonitorName = $MonitorName.replace('*','')
  102.   $MonitorName = $MonitorName.replace(':','')
  103.   $MonitorName = $MonitorName.replace(';','')
  104.   $ExportFile = $Path + "\" + $FolderName + "\" + $MonitorName + ".htm";
  105.   $ExportFile
  106.   $article_text.tostring() | out-file $ExportFile
  107.    $j = $j + 1
  108.  }
Executing the script will create a folder Microsoft.Windows.Server.PrintServer.2012 - 6.0.7004.0 that contains an HTML file for each monitor/rules.




Note :
  • The script sometimes return errors on some monitors or rules - I guess the conversion to HTML is failing - but in that case, the impact is that the HTML file is not created for that rule/monitor.
  • The script can be easily adapted of courses to run on OpsMgr 2012.


Here is a link where you can download my script : ExportKnowledgeArticleFromAMP.ps1



I'll publish a page on my blog >>>> HERE <<<< with all the product knowledge for all the provided MP I've in my environment.


This posting is provided "AS IS" with no warranties.

No comments:

Post a Comment