locked
Why won't this code run? RRS feed

  • Question

  • Why does this fail when I run it from a scheduled task?

    Whats the best way to run this code? I know I'm missing something stupid here.

    http://systemcentercentral.com/BlogDetails/tabid/143/IndexID/19345/Default.aspx

     

    $targetdate = (get-date).AddDays(-1)

    (Get-Alert -criteria 'ResolutionState = ''255'' AND IsMonitorAlert = ''True'''| Where-Object {$_.LastModified -gt $targetdate }).count

     # script

    # targetdate or cut off date which is the past 1 day or 24 hours
    $targetdate = (get-date).AddDays(-1)
    # get closed alerts created by a monitor whose lastmodified is greater than the targetdate
    $alerts = Get-Alert -criteria 'ResolutionState = ''255'' AND IsMonitorAlert = ''True'''| Where-Object {$_.LastModified -gt $targetdate }

    ForEach($alert in $alerts) {
    $mon = get-monitor -criteria "Id = '$($alert.MonitoringRuleId)'"
    $mc = get-monitoringClass -id $alert.MonitoringClassId
    $mc | get-monitoringObject -criteria "Id = '$($alert.MonitoringObjectId)'" | foreach {$_.ResetMonitoringState($mon)}
    }

     

    Returns:

     

     


    Saturday, April 23, 2011 4:11 PM

Answers

  • Try to add this to your code (before anything else):

     

    #-------------------------------------------------------------------------------
    # fqdn of the root management server.
    #-------------------------------------------------------------------------------
    $rootMS="<FQDN TO YOUR RMS SERVER>"

    #-------------------------------------------------------------------------------
    # Add operations manager snapin and connect to the root management server.
    #-------------------------------------------------------------------------------
    add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
    set-location "OperationsManagerMonitoring::";
    new-managementGroupConnection -ConnectionString:$rootMS;

     


    Regards,
    Marc Klaver
    http://jama00.wordpress.com/
    • Marked as answer by Hola IT GUY Thursday, April 28, 2011 2:09 AM
    Tuesday, April 26, 2011 12:44 PM

All replies

  • FYI: http://technet.microsoft.com/en-ca/magazine/2008.08.scom.aspx

    I discuss how you might be able to accomplish this.  You likely just need to load the OpsMgr snapin in your code.  Now, keep in mind, you need to make sure the account you use to run these task has the proper OpsMgr permissions...
    • Proposed as answer by Marco Shaw Saturday, April 23, 2011 11:16 PM
    Saturday, April 23, 2011 11:16 PM
  • Try to add this to your code (before anything else):

     

    #-------------------------------------------------------------------------------
    # fqdn of the root management server.
    #-------------------------------------------------------------------------------
    $rootMS="<FQDN TO YOUR RMS SERVER>"

    #-------------------------------------------------------------------------------
    # Add operations manager snapin and connect to the root management server.
    #-------------------------------------------------------------------------------
    add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client";
    set-location "OperationsManagerMonitoring::";
    new-managementGroupConnection -ConnectionString:$rootMS;

     


    Regards,
    Marc Klaver
    http://jama00.wordpress.com/
    • Marked as answer by Hola IT GUY Thursday, April 28, 2011 2:09 AM
    Tuesday, April 26, 2011 12:44 PM
  • Thanks guys, I knew it was something 'easy'...

     

    :P

    Thursday, April 28, 2011 2:10 AM