• Compare two different csv files using PowerShell

    Today I needed to compare two csv files with results from SQL queries from the OperationsManager database and the OperationsManagerDW database. I run the SQL queries on both OperationsManager databases and saved the result to a csv file. Now I needed to quickly compare the results from both csv files, and what better way than to use PowerShell.

    Here is an example how you can use PowerShell to quickly compare two csv files:

    First some example CSV files:

    Example CSVFile1.csv

    ManagementPackId,MPFriendlyName,MPName,mp.MPVersionDependentId,MPLastModified,MPKeyToken,ContentReadable
    3A7609F3-A5AB-F205-5001-010EE387DD28,Customer - Exchange 2007 MP overrides,Customer.Exchange.MP.overrides,5D49AADA-DFEE-40DC-9A32-2758FC71B426,2011-01-06 16:39:57.517,NULL,1
    49C911AC-337C-CD94-DD13-021E2CFDDAB0,Customer - SCOM MS,Customer.SCOM.MS,9FA54A57-B123-44AE-A9AB-ED1A1C4CDB35,2011-01-27 09:16:28.100,NULL,1
    C0A4183F-4318-CB0C-EF5A-054B32AE33B4,Windows Server 2000 Operating System Overrides,Windows.Server.2000.Operating.System.Overrides,6293AED6-DE8F-462E-AD54-1F83F2E33F82,2011-02-04 14:38:19.330,NULL,1
    3DFAC27F-8551-B71B-7DD2-30156A31CD92,Customer - Citrix Beheer - WIS Servers Events,Customer.Citrix.Beheer.WIS.Servers.Events,4496E575-C63C-4B45-BEF7-6216668264F7,2010-12-06 01:54:15.963,NULL,1
    3AFE86A8-827C-CDB6-9A84-32C67483C3D1,Windows cluster management monitoring - overrides,Windows.cluster.management.monitoring.overrides,243D4B09-9455-4737-9880-399CF2700CF4,2011-02-04 16:07:11.377,NULL,1
    6569E210-F188-EEF7-05CA-331859F4D8C6,Customer - Citrix Beheer,Customer.Citrix.Beheer,8109BB67-C923-4814-A1DB-F2A386B2389B,2011-01-17 13:58:08.690,NULL,1
    8BDC857A-7F32-40A7-E5C2-4583E263B290,Customer - ISA server 2006,Customer.ISA.server,68F24CBA-B6C7-474B-A09B-03B0D730609B,2011-01-07 10:56:51.290,NULL,1
    C39E8EF5-5E04-9CAE-F467-61D87ACD5E9E,Customer - Windows Server - SCCM,Customer.Windows.Server.SCCM,7BB1925D-007A-4621-BAD3-56F87B49C4C8,2011-01-17 13:02:28.880,NULL,1
    A2B5CA86-96F7-F4CA-2152-63274CEF3336,Customer - Sharepoint Server,Customer.Sharepoint.Server,26E07753-0740-450F-972C-6A16C2099C12,2011-01-06 07:26:36.533,NULL,1
    5BC992B2-96F6-E192-6E92-65FB5EC0CE5E,Customer - SQL Server (Monitoring),Customer.SQL.Server.Monitoring,2618C5C0-D89F-4E86-8259-FD7809D75E7C,2011-02-07 14:12:14.613,NULL,1
    E4056BA9-CFC7-2D04-FF9B-67084CC15E83,Customer - Print Servers,Customer.Print.Servers,C336C3FD-CF15-4319-A33E-00F4BF2A390D,2010-11-24 10:14:22.660,NULL,1
    3EECB872-CC4A-62BC-EA39-855EF594852D,Customer - Print Beheer Menu MP,Customer.Print.Beheer.Menu.MP,6F12D09D-0A55-40BA-A320-B571E9554ADE,2010-12-21 12:49:53.603,NULL,1
    A2D688EC-8AE1-C4AF-A485-8564C214D292,Customer - Arcserve MP,Customer.Arcserve.MP,7AECFE66-51F6-4174-B84C-E918140F37F2,2010-12-03 11:51:39.080,NULL,1
    A3542697-E0E7-E382-D40B-86ADEC512D79,Windows Server 2008 Operating System (Monitoring) Overrides,Windows.Server.2008.Operating.System.Monitoring.Overrides,20BCC6DC-F928-4108-B70F-873639A0448A,2010-12-16 12:16:40.800,NULL,1
    070A0205-56D6-FF84-688A-8A216A47A949,Customer - Fileservers Dienst,Customer.Fileservers.Dienst,7DDF2381-3B13-4971-9E91-6656075C2E60,2010-12-22 09:26:50.683,NULL,1
    98701ABB-AF93-7E5A-D121-9A18E0037CE8,Customer - DSA & PKI Gateways,Customer.DSA.PKI.Gateways,95B49F67-9514-4E80-991D-B3CF4EA2632F,2010-12-20 11:38:44.697,NULL,1
    FA4FDEAF-E107-8AA3-BD2C-9E29C58C0C53,Customer - PKI Management,Customer.PKI.Management,6B677888-07F1-4725-BA3F-E742B20FA9C4,2011-01-27 08:45:23.153,NULL,1
    51DA566A-1AC1-5DC9-3F66-A11FC439357C,nworks VMware Virtual Enterprise Monitoring MP,nworks.VMware.VEM,599A587E-3357-A127-50EF-198F04365ADA,2010-11-23 11:10:29.243,65c40f14a98ce59b,1
    512DB9E6-D21A-2E4F-E987-A28BDE8F940B,Customer - Exchange availability,Customer.Exchange.availability,1D0DDA6B-9ED2-48C0-BFEE-1538DA9AE16F,2010-12-27 10:10:30.707,NULL,1
    DD81FA7E-73BF-332B-C4B6-A767BB514BE8,Customer - Connect Direct,Customer.Connect.Direct,9C6BB3FD-6582-4F18-B18C-7BBE4DB9FCA6,2011-01-31 13:47:15.613,NULL,1
    155A8BBB-C6D2-C706-DD1C-B7EFCCCC0E81,Customer - AD Beheer,Customer.AD.Beheer,CF036BAA-47C0-4D09-A2F4-25B3DC1F78B9,2011-01-05 13:17:15.520,NULL,1
    E2296ED5-DDBC-EBC7-3881-C5288ADAA7CF,Microsoft Exchange Server 2010 Management Pack,Microsoft.Exchange.2010,AB06EB14-EAF1-0F0B-04B8-F1CDD33F4ACC,2011-02-04 09:02:29.607,31bf3856ad364e35,1
    9E138797-AE1C-AF6E-E61B-D110253B417C,Engyro Connector TEC,Engyro.Connector.TEC.MP,76306617-5F13-42EB-9F3B-B948F18DC4D8,2010-11-23 11:29:55.980,NULL,1
    A3C3E706-795F-BF80-DFC2-DBB4BDDFA919,Customer - ACS Management Pack,Customer.ACS.Management.Pack,D7C4F7A2-B18D-454F-8615-22CEC4EFDFCA,2011-01-04 12:30:01.593,NULL,1
    7C2A6181-B7B9-89C2-6FC3-EDB760FCEFC1,Microsoft Exchange Server 2007 CAS Monitoring - Override,Microsoft.Exchange.Server.CAS.Monitoring.Override,BA79A086-E992-4AB7-B4B6-933FB8AD1211,2011-01-07 12:39:36.870,NULL,1

     

    Example CSVFile2.csv

    ManagementPackId,MPFriendlyName,MPName,mp.MPVersionDependentId,MPLastModified,MPKeyToken,ContentReadable
    3A7609F3-A5AB-F205-5001-010EE387DD28,Customer - Exchange 2007 MP overrides,Customer.Exchange.MP.overrides,5D49AADA-DFEE-40DC-9A32-2758FC71B426,2011-01-06 16:39:57.517,NULL,1
    49C911AC-337C-CD94-DD13-021E2CFDDAB0,Customer - SCOM ,Customer.SCOM,9FA54A57-B123-44AE-A9AB-ED1A1C4CDB35,2011-01-27 09:16:28.100,NULL,1
    C0A4183F-4318-CB0C-EF5A-054B32AE33B4,Windows Server 2000 Operating System Overrides,Windows.Server.2000.Operating.System.Overrides,6293AED6-DE8F-462E-AD54-1F83F2E33F82,2011-02-04 14:38:19.330,NULL,1
    F37D0C95-F313-4EC9-5385-0F41BFCFE55D,Customer Custom Monitoring,Customer.Custom.Monitoring,44597F97-6FD4-4D2E-B01E-5CDB9B218E50,2011-01-27 08:52:20.377,NULL,1
    75E3375E-B1CD-4641-E86B-2664A7E7E7C2,Customer - Microsoft Exchange 2010 Override MP,Customer.Microsoft.Exchange.Override.MP,6DC9439A-BD4C-46C0-8D2F-080029042A6D,2011-02-04 11:12:40.857,NULL,1
    3DFAC27F-8551-B71B-7DD2-30156A31CD92,Customer - Citrix Beheer - WIS Servers Events,Customer.Citrix.Beheer.WIS.Servers.Events,4496E575-C63C-4B45-BEF7-6216668264F7,2010-12-06 01:54:15.963,NULL,1
    3AFE86A8-827C-CDB6-9A84-32C67483C3D1,Windows cluster management monitoring - overrides,Windows.cluster.management.monitoring.overrides,243D4B09-9455-4737-9880-399CF2700CF4,2011-02-04 16:07:11.377,NULL,1
    6569E210-F188-EEF7-05CA-331859F4D8C6,Customer - Citrix Beheer,Customer.Citrix.Beheer,8109BB67-C923-4814-A1DB-F2A386B2389B,2011-01-17 13:58:08.690,NULL,1
    B5FE879F-CD35-DD9A-7464-36401ABB9205,Microsoft Exchange 2010 Report Library,Microsoft.Exchange.2010.Reports,112EA102-A45A-9AA9-C84D-1ED9A56E8C61,2011-02-04 10:31:14.137,31bf3856ad364e35,1
    DA187E72-B9D7-9E16-D098-3B0A624DC38C,My Default Management Pack,Microsoft.SystemCenter.OperationsManager.DefaultUser,497CE20F-02AF-417E-97B1-363F311CF739,2011-02-03 16:37:16.093,NULL,1
    8BDC857A-7F32-40A7-E5C2-4583E263B290,Customer - ISA server 2006,Customer.ISA.server,68F24CBA-B6C7-474B-A09B-03B0D730609B,2011-01-07 10:56:51.290,NULL,1
    C39E8EF5-5E04-9CAE-F467-61D87ACD5E9E,Customer - Windows Server - SCCM,Customer.Windows.Server.SCCM,7BB1925D-007A-4621-BAD3-56F87B49C4C8,2011-01-17 13:02:28.880,NULL,1
    A2B5CA86-96F7-F4CA-2152-63274CEF3336,Customer - Sharepoint Server,Customer.Sharepoint.Server,26E07753-0740-450F-972C-6A16C2099C12,2011-01-06 07:26:36.533,NULL,1
    5BC992B2-96F6-E192-6E92-65FB5EC0CE5E,Customer - SQL Server (Monitoring),Customer.SQL.Server.Monitoring,2618C5C0-D89F-4E86-8259-FD7809D75E7C,2011-02-07 14:12:14.613,NULL,1
    E4056BA9-CFC7-2D04-FF9B-67084CC15E83,Customer - Print Servers,Customer.Print.Servers,C336C3FD-CF15-4319-A33E-00F4BF2A390D,2010-11-24 10:14:22.660,NULL,1
    336987CD-60EF-5014-F384-7D37DE784858,Nworks Overrides,Nworks.Overrides,5EF45858-0DC1-4E67-9AFD-F70A2446CCA9,2011-01-25 10:20:27.143,NULL,1
    3EECB872-CC4A-62BC-EA39-855EF594852D,Customer - Print Beheer Menu MP,Customer.Print.Beheer.Menu.MP,6F12D09D-0A55-40BA-A320-B571E9554ADE,2010-12-21 12:49:53.603,NULL,1
    A2D688EC-8AE1-C4AF-A485-8564C214D292,Customer - Arcserve MP,Customer.Arcserve.MP,7AECFE66-51F6-4174-B84C-E918140F37F2,2010-12-03 11:51:39.080,NULL,1
    A3542697-E0E7-E382-D40B-86ADEC512D79,Windows Server 2008 Operating System (Monitoring) Overrides,Windows.Server.2008.Operating.System.Monitoring.Overrides,20BCC6DC-F928-4108-B70F-873639A0448A,2010-12-16 12:16:40.800,NULL,1
    070A0205-56D6-FF84-688A-8A216A47A949,Customer - Fileservers Dienst,Customer.Fileservers.Dienst,7DDF2381-3B13-4971-9E91-6656075C2E60,2010-12-22 09:26:50.683,NULL,1
    98701ABB-AF93-7E5A-D121-9A18E0037CE8,Customer - DSA & PKI Gateways,Customer.DSA.PKI.Gateways,95B49F67-9514-4E80-991D-B3CF4EA2632F,2010-12-20 11:38:44.697,NULL,1
    FA4FDEAF-E107-8AA3-BD2C-9E29C58C0C53,Customer - PKI Management,Customer.PKI.Management,6B677888-07F1-4725-BA3F-E742B20FA9C4,2011-01-27 08:45:23.153,NULL,1
    51DA566A-1AC1-5DC9-3F66-A11FC439357C,nworks VMware Virtual Enterprise Monitoring MP,nworks.VMware.VEM,599A587E-3357-A127-50EF-198F04365ADA,2010-11-23 11:10:29.243,65c40f14a98ce59b,1
    512DB9E6-D21A-2E4F-E987-A28BDE8F940B,Customer - Exchange availability,Customer.Exchange.availability,1D0DDA6B-9ED2-48C0-BFEE-1538DA9AE16F,2010-12-27 10:10:30.707,NULL,1
    DD81FA7E-73BF-332B-C4B6-A767BB514BE8,Customer - Connect Direct,Customer.Connect.Direct,9C6BB3FD-6582-4F18-B18C-7BBE4DB9FCA6,2011-01-31 13:47:15.613,NULL,1
    155A8BBB-C6D2-C706-DD1C-B7EFCCCC0E81,Customer - AD Beheer,Customer.AD.Beheer,CF036BAA-47C0-4D09-A2F4-25B3DC1F78B9,2011-01-05 13:17:15.520,NULL,1
    E2296ED5-DDBC-EBC7-3881-C5288ADAA7CF,Microsoft Exchange Server 2010 Management Pack,Microsoft.Exchange.2010,AB06EB14-EAF1-0F0B-04B8-F1CDD33F4ACC,2011-02-04 09:02:29.607,31bf3856ad364e35,1
    9E138797-AE1C-AF6E-E61B-D110253B417C,Engyro Connector TEC,Engyro.Connector.TEC.MP,76306617-5F13-42EB-9F3B-B948F18DC4D8,2010-11-23 11:29:55.980,NULL,1
    A3C3E706-795F-BF80-DFC2-DBB4BDDFA919,Customer - ACS Management Pack,Customer.ACS.Management.Pack,D7C4F7A2-B18D-454F-8615-22CEC4EFDFCA,2011-01-04 12:30:01.593,NULL,1
    7C2A6181-B7B9-89C2-6FC3-EDB760FCEFC1,Microsoft Exchange Server 2007 CAS Monitoring - Override,Microsoft.Exchange.Server.CAS.Monitoring.Override,BA79A086-E992-4AB7-B4B6-933FB8AD1211,2011-01-07 12:39:36.870,NULL,1
    94DDD8EA-2A12-9E54-9545-F4A926DBACF0,System Center Core Monitoring Overrides,System.Center.Core.Monitoring.Overrides,BA527C6E-0E2C-440C-9358-6D073BBD4722,2010-12-07 07:25:25.517,NULL,1

     

    Now we can compare both files with the use of the compare-object cmdlet in PowerShell.

    $file1 = import-csv -Path "C:\temp\Test1.csv"
    $file2 = import-csv -Path "C:\temp\Test2.csv"
    Compare-Object $file1 $file2 -property MPFriendlyName -IncludeEqual

     

    image

    Have fun with PowerShell and the compare-object cmdlet.

     

    (stefan.stranger).gettype()

    IsPublic IsSerial Name BaseType

    -------- -------- ---- --------

    True False Object[] Crappy.Planner

  • Pulling OpsMgr Company Knowledge from Management Pack and send it to email

    Some weeks ago I got a question from Marnix Wolf asking if it’s possible to retrieve Company Knowledge for a monitor or rule and sent that as email. And after an evening playing around with PowerShell I created a PowerShell script that could do exactly that.

     

    Pre-requisites:

    • OpsMgr 2007 (R2)
    • PowerShell version 2.0 (I only tested the script in PowerShell version 2.0)
    • Company Knowledge stored in one Management Pack

     

    Background info:

    I hope you know you can add your own Company Knowledge for a rule or monitor.  Administrators can add their own knowledge to rules and monitors to expand the troubleshooting information and provide company-specific information for operators, which is known as company knowledge. Administrators can use company knowledge to document any overrides implemented for a monitor or rule, along with the explanation for the customization and any other information that might be useful.

    Operations Manager stores company knowledge in a management pack. Sealed management packs cannot be modified, so Operations Manager saves customizations such as company knowledge in a custom management pack. By default, Operations Manager saves all customizations to the Default Management Pack. As a best practice, you should instead create a separate management pack for each sealed management pack you want to customize. More info about adding Company Knowledge can be found here. For the pre-requisites you can take a look at this blogpost on the MOMTeam blogsite.

     

    Steps:

    1. Let’s first create a Management Pack to store the Company Knowledge.
      image

      I’ve created a Management Pack called Stefan – Company Knowledge Test MP.
    2. The next steps is to create Company Knowledge for some monitors and rules.
    1. Open in the OpsMgr Console a Monitor for which we want to add Company Knowledge. I choose the AD Domain Availability Health Rollup Monitor because there is an Alert for this monitor in my demo environment.
      image
    2. Open the Company Knowledge Tab and select Edit.
      image
    3. Word will be launched (in my case it’s Word 2010) and insert the Company Knowledge you want to Edit.
      image
    4. Once this is done, click File/Save. Do NOT close out the Word document. If you do you will lose any information that you had saved.
    5. image 
    6. Press the Save button  to save Company Knowledge and make sure you save it in the correct Management Pack!
    7. Let’s do the same for a rule. I selected the AD Agent Assignment: Rule Failure rule.
    8. Follow steps 1 to 7 again to add your Company Knowledge for this rule.
      image
    9. And again save the Company Knowledge in the same Management Pack.

     

    Now we saved the Company Knowledge for the monitor and rule we can extract this info from the Management Pack. It’s also somewhere in the database but extracting it from the Management Pack is the easiest way IMO.

     

    These are the high-level steps that need to be executed in the PowerShell script:

    1. Find Monitor or Rule for Alert.
    2. Export Management where Company Knowledge is saved.
    3. Parse Company Knowledge for Monitor from exported XML MP.
    4. Email Company Knowledge

     

    Step 1. Find Monitor or Rule for Alert.

    #Error Handling
    #This is because when there is no monitor found in the FindMonitorForAlert Function
    #the script needs to continue.
    $ErrorActionPreference="SilentlyContinue"
    $Error.psbase.clear()

           
    Function Initialize
    {
         ## Check for OpsMgr shell
        if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null)
        {
            Write-Host "Load OpsMgr Snapin if not loaded" -foregroundcolor red
            LoadSnapin("localhost")
        }
    }
         
    Function LoadSnapin($RMS)
    {
        add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
        set-location "OperationsManagerMonitoring::";
        new-managementGroupConnection -ConnectionString:$RMS;
        set-location $RMS;
    }
         
         
    Function FindMonitorOrRuleForAlert($AlertName)
    {
        #Find an alert with resolution state new
        $alert = get-alert | where {$_.Name -eq "$AlertName"}
       
        #Check if Alert is available
        if ($alert)
        {   
            #Find monitor for Alert.
            $monitorname = (get-monitor $alert.monitoringruleid).Name

            #Check if Monitor is found
            if ($monitorname)
            {
                Write-Host "Monitor Found"
                return $monitorname
            }
            else
            {
                #Call FindRuleForAlert
                FindRuleForAlert $AlertName           
            }
        }
        else
            {
            Write-Host "No Alert found"
            }
    }
       
    Function FindRuleForAlert($AlertName)
    {
        $alert = get-alert | where {$_.Name -eq "$AlertName"}
               
        #Find rule for Alert.
        $rulename = (get-rule $alert.monitoringruleid).Name
       
        Write-Host "Rule Found"       
        return $rulename
    }
       
       
    #Main
     
    Initialize
     
    FindMonitorOrRuleForAlert "Data Access Service - Windows Service"
     
    FindMonitorOrRuleForAlert "Root Management Server Unavailable." 

    When we run the above PowerShell script we find the Monitor or Rule name for the  alerts “Data Access Service – Windows Service” and “Root Manager Server Unavailable.

    image

     

    image

    We know have a way to find the Monitor or Rule name for an alert.

    Step 2. Export Management where Company Knowledge is saved.

    <#
        In this part of the script we are going to export the Management Packs
        and save it in the User's temp folder
    #>


    Function Initialize
    {
         ## Check for OpsMgr shell
        if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null)
        {
            Write-Host "Load OpsMgr Snapin if not loaded" -foregroundcolor red
            LoadSnapin("localhost")
        }
    }
         
    Function LoadSnapin($RMS)
    {
        add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
        set-location "OperationsManagerMonitoring::";
        new-managementGroupConnection -ConnectionString:$RMS;
        set-location $RMS;
    }


    Function ExportMP($MPname, $mpexportfolder)
        {
            #Get Management where Company Knowledge is saved. Must be know in advance.
            get-managementpack | where {$_.Name -eq $MPName} | export-managementpack -path $mpexportfolder
            Write-Host "MP exported to xml"
        }


    #Main

    Initialize

    ExportMP -MPname "Stefan.Company.Knowledge.Test.MP" -mpexportfolder ($env:TEMP)

    Result:

    image

     

    We now exported the Management Pack where we saved the Company Knowledge to the users temp directory.

    Step 3. Parse Company Knowledge for Monitor from exported XML MP.

    <#
        In this part of the script we are parse the Company Knowledge
        for the Monitor or Rule
    #>

    #Variables
    $monitorname = "Microsoft.Windows.Server.AD.Domain.AvailRollupMonitor"


    Function Initialize
    {
         ## Check for OpsMgr shell
        if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null)
        {
            Write-Host "Load OpsMgr Snapin if not loaded" -foregroundcolor red
            LoadSnapin("localhost")
        }
    }
         
    Function LoadSnapin($RMS)
    {
        add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
        set-location "OperationsManagerMonitoring::";
        new-managementGroupConnection -ConnectionString:$RMS;
        set-location $RMS;
    }

    Function ParseCK($monitor,$mpexportfolder,$MPName)
    {
        $xml =  [xml](Get-Content $mpexportfolder"\"$MPname".xml")
             
        #MonitorName
        $ElementID = "'"+ "$Monitor" + "'"
        $KnowledgeArticle = [xml](Select-Xml -xml $xml -xpath "//KnowledgeArticle[contains(@ElementID, $ElementID)]")
        $myarray = $KnowledgeArticle.MamlContent.section
        $myarray | Format-Table @{Label="Title";Expression={$_.title}}, @{Label="Text";Expression={$_.para}} -aut
        Write-Host "finished"
    }
         
    #Main
         
    Initialize

    ParseCK -monitor $monitorname -mpexportfolder ($env:TEMP) -MPName "Stefan.Company.Knowledge.Test.MP"

    Result:

    image

    Or for the Rule:

    image

     

    Step 4. Email Company Knowledge

    In PowerShell 2.0 there is a new cmdlet Send-MailMessage, but I’m going to use the Net.Mail.MailMessage .Net class because my smtp server needs authentication.

    <#
        In this part of the script we are emailing the Company Knowledge
    #>


    #Body
    $body = @"
        <h1>Knowledge Article</h1>

        <h2>Summary</h2>

        <h2>Configuration</h2>

        <h2>Causes</h2>

        <h2>Resolutions</h2>

        <h2>Additional Information</h2>

        <h2>External Knowledge Sources</h2>
    "@


    # Create mail and set priority
    $mail = new-object Net.Mail.MailMessage
    $mail.Priority = [System.Net.Mail.MailPriority]::High

    # Create from, to, and bcc
    $mail.From  = opsmgr@stranger.local
    $mail.To.Add("username@hotmail.com")
    $mail.CC.Add("username@microsoft.com")
    $mail.BCC.Add(username@stranger.local)

    # Create the message
    $mail.Subject = "OpsMgr email with Company Knowledge" + " (" + [System.DateTime]::Now.ToString("yyyy-MM-dd HH:mm") + ")"
    $mail.Body = $body
    $mail.IsBodyHtml = $true

    # Set SMTP Server and create SMTP Client
    $smtp = new-object Net.Mail.SmtpClient
    $smtp.Host = "mail.stranger.local"
    $smtp.Port = "25"
    $smtp.EnableSsl = $false
    $smtp.Credentials = New-Object System.Net.NetworkCredential(stefan@stranger.local, P@ssw0rd);

    # Send message
    try {
       $smtp.Send($mail)
    }
    catch {
      "Exception caught: {0}" -f $Error[0]
    }

    Result:

    image

     

    Ok now we only have to put all the separate parts together Winking smile

     

    <#
        Pulling OpsMgr Company Knowledge from Management Pack and send it to email.
        This script will find the monitor or rule for a selected alert and
        pull the Company Knowledge for this monitor or rule from the exported Management Pack.
        Tested on:
        - OpsMgr 2007 R2
        - PowerShell version 2.0
        Remark: All the Company Knowledge should be saved in ONE Management Pack
        Author: Stefan Stranger
        Date: 17-02-2011
    #>

    param($AlertName=(Read-Host "Please Enter Alert Name"),$emailto=(Read-Host "Please Enter email address"))

     

    #Variables:
    $global:mpname = ""
    $global:rmsname = "opsmgrrms.stranger.local"
    $global:mpname = "Stefan.Company.Knowledge.Test.MP"
    $currentpath = pwd

    #Error Handling
    #This is because when there is no monitor found in the FindMonitorForAlert Function
    #the script needs to continue.
    $ErrorActionPreference="SilentlyContinue"
    $Error.psbase.clear()

           
    Function Initialize
    {
         ## Check for OpsMgr shell
        if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null)
        {
            Write-Host "Load OpsMgr Snapin if not loaded" -foregroundcolor red
            LoadSnapin("$rmsname")
        }
    }
         
    Function LoadSnapin($rmsname)
    {
        add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
        set-location "OperationsManagerMonitoring::";
        new-managementGroupConnection -ConnectionString:$rmsname;
        set-location $rmsname;
    }

    Function FindRuleForAlert($AlertName)
    {
        $alert = get-alert | where {$_.Name -like "$AlertName"}
       
        $monitoringruleid = ($alert | select MonitoringRuleID -first 1).MonitoringRuleId.Guid
               
        #Find rule for Alert.
        $rulename = (get-rule $monitoringruleid).Name
       
        Write-Host "Rule Found"       
        return $global:monitororrulename = $rulename
    }
         
         
    Function FindMonitorOrRuleForAlert($AlertName)
    {
        #Find an alert with resolution state new
        $alert = get-alert | where {$_.Name -like "$AlertName"}
       
        #Check if Alert is available
        if ($alert)
        {   
           
            $monitoringruleid = ($alert | select MonitoringRuleID -first 1).MonitoringRuleId.Guid
            #Find monitor for Alert.
            $monitorname = (get-monitor $monitoringruleid).Name

            #Check if Monitor is found
            if ($monitorname)
            {
                Write-Host "Monitor Found"
                return $global:monitororrulename = $monitorname
            }
            else
            {
                #Call FindRuleForAlert
                FindRuleForAlert $AlertName           
            }
        }
        else
            {
            Write-Host "No Alert found"
            break
            }
    }
       

       
       
     
    #Step 2. Export Management where Company Knowledge is saved.
    <#
        In this part of the script we are going to export the Management Packs
        and save it in the User's temp folder
    #>


    Function ExportMP($MPname, $mpexportfolder)
    {
        #Get Management where Company Knowledge is saved. Must be know in advance.
        get-managementpack | where {$_.Name -eq $MPName} | export-managementpack -path $mpexportfolder
        Write-Host "MP exported to xml"
    }


    #Step 3. Parse Company Knowledge for Monitor from exported XML MP.
    <#
        In this part of the script we are parse the Company Knowledge
        for the Monitor or Rule
    #>

    Function ParseCK($monitororrulename,$mpexportfolder,$MPName)
    {
        $xml =  [xml](Get-Content $mpexportfolder"\"$MPname".xml")
             
        $ElementID = "'"+ "$monitororrulename" + "'"
        $KnowledgeArticle = [xml](Select-Xml -xml $xml -xpath "//KnowledgeArticle[contains(@ElementID, $ElementID)]")
        $global:myarray = $KnowledgeArticle.MamlContent.section
        #$myarray | Format-Table @{Label="Title";Expression={$_.title}}, @{Label="Text";Expression={$_.para}} -aut
        Write-Host "Parsed Company Knowledge"
    }

    #Step 4. Email Company Knowledge
    <#
        In this part of the script we are emailing the Company Knowledge
    #>

    $Summary = ($myarray | where {$_.Title -eq "Summary"}).para
    $Configuration = ($myarray | where {$_.Title -eq "Configuration"}).para
    $Causes = ($myarray | where {$_.Title -eq "Causes"}).para
    $Resolutions = ($myarray | where {$_.Title -eq "Resolutions"}).para
    $Additional = ($myarray | where {$_.Title -eq "Additional"}).para
    $External = ($myarray | where {$_.Title -eq "External"}).para


    #Body
    $body = @"
        <h1>Knowledge Article</h1>

        <h2>Summary</h2>
        <p>$Summary</P>
        <h2>Configuration</h2>
        <p>$Configuration</P>
        <h2>Causes</h2>
        <p>$Causes</P>
        <h2>Resolutions</h2>
        <p>$Resolutions</P>
        <h2>Additional Information</h2>
        <p></P>
        <h2>External Knowledge Sources</h2>
        <p>$External</P>
    "@

    Function EmailCK($emailto)
    {

        # Create mail and set priority
        $mail = new-object Net.Mail.MailMessage
        $mail.Priority = [System.Net.Mail.MailPriority]::High

        # Create from, to, and bcc
        $mail.From  = "opsmgr@stranger.local"
        $mail.To.Add($emailto)
        $mail.CC.Add("username@microsoft.com")
        $mail.BCC.Add(username@stranger.local)

        # Create the message
        $mail.Subject = "OpsMgr email with Company Knowledge" + " (" + [System.DateTime]::Now.ToString("yyyy-MM-dd HH:mm") + ")"
        $mail.Body = $body
        $mail.IsBodyHtml = $true

        # Set SMTP Server and create SMTP Client
        $smtp = new-object Net.Mail.SmtpClient
        $smtp.Host = "mail.stranger.local"
        $smtp.Port = "25"
        $smtp.EnableSsl = $false
        $smtp.Credentials = New-Object System.Net.NetworkCredential("username@stranger.nl", P@ssword);

        # Send message
        try {
           $smtp.Send($mail)
           Write-Host "Email is sent"
        }
        catch {
          "Exception caught: {0}" -f $Error[0]
        }
    }

    #Step 5. Clean up
    <#
        In this part of the script we are cleaning up.
        Remove the exported MP XML file.
    #>

    Function CleanUp($MPname)
    {
        remove-item ($env:TEMP+"\"+$MPname+".xml")
        Write-Host "Remove MP XML File"
        remove-pssnapin -name "Microsoft.EnterpriseManagement.OperationsManager.Client"
        cd $currentpath
        $AlertName = ""
       

    }


    #Main
     
    Initialize

    FindMonitorOrRuleForAlert $AlertName

    ExportMP -MPname $mpname -mpexportfolder ($env:TEMP)

    ParseCK -monitor $monitororrulename -mpexportfolder ($env:TEMP) -MPName $mpname

    EmailCK -emailto $emailto

    CleanUp -MPname $mpname

     

    Result:

    image

     

    image

     

    Have fun!F

  • Download VHD Test Drive – System Center Operations Manager 2007 R2 with PowerShell and Bits

    Just in case you were interested in test driving Operations Manager 2007 R2, we published a VHD several days ago, you can download it here.

    Jeremy Pavleck questioned why it’s only available as a 13 part RAR file, and doesn’t use the typical MS File Transfer Manager like we get on MSDN to download it one big bundle – or at least the option to get it that way. I don’t know why we offer it only as a 13 part RAR file but for his and yours convenience I created a PowerShell script which can download the complete rar files in one-go using Bits.

     

    Save script to DownloadVHD.ps1 and run from PowerShell.

    ###############################################################################
    # Download VHD Test Drive – System Center Operations Manager 2007 R2 from
    # http://www.microsoft.com/downloads/en/details.aspx?FamilyID=ef67ea4f-b8af-436f-80ae-33936f26f110
    # in one-go using PowerShell and Bits.
    # Remark: Use PowerShell 2.0 because it makes use of the BitsTransfer Module
    # Author: Stefan Stranger
    # v1.001 - 14/02/2011 - stefstr - initial release
    ###############################################################################

    $global:path = "c:\Temp\"

    Import-Module BitsTransfer #Loads the BitsTransfer Module
    Write-Host "BitsTransfer Module is loaded"


    $OpsMgrVHDs = @("http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/EULA.txt",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/README.rtf",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part01.exe",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part02.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part03.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part04.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part05.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part06.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part07.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part08.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part09.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part10.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part11.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part12.rar",
    "http://download.microsoft.com/download/4/A/F/4AFA721B-842E-4253-82E7-A53E09FC191E/VHD_OpsMgr2007R2.part13.rar")

    Foreach ($OpsMgrVHD in $OpsMgrVHDs) { Start-BitsTransfer $OpsMgrVHD $path}
    Write-Host "OpsMgr VHD's are downloaded to $path"

     

    Screenshot from script running in PowerShell ISE.

    image

     

    Have fun!

  • Sneak preview of sealed MPs using PowerShell

    Today I needed to check if a customer had installed the latest versions of available Management Packs. And there are many tools that can help you with that, but I wanted to know if the downloaded MP files had a newer version than the ones installed in the Management Group.

    I could have used  Boris Yanushpolsky’s  MPViewer to check which version the MP file had, but what better than use PowerShell to have a Sneak Preview of the Version of the downloaded MP file Winking smile

    Here is the script I created. Just save it to SneakPreviewSealedMPFile.ps1 and have fun.

    <#
        Use this script to have a sneak preview of the sealed MP file.
        You need to have the OpsMgr Console installed for this script to work.
        Example usage: .\SneakPreviewSealedMPFile.ps1 "C:\Install\OpsMgr\MPs\SCOMCrossPlatformCU3MP\Microsoft.Unix.LogFile.Library.mp"
    #>

    param($sealedmp)

    # Loads an assembly from the application directory or from the global assembly cache using a partial name
    $assembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager")| out-null

    # Create a new .Net Object   
    $mp = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPack($sealedmp)

    #  Format the result
    $mp | format-list

     

    Result:

    image