none
How can i repeat a script every 5 minutes RRS feed

  • Question

  • Hello, Guys

    I wrote a script to get some alerts from (SCOM) and send it using SMS over some API integration.

    Once i ran it from PowerShell ISE it self it's working without any issues, but once i created a schedule task it's not working i think that if i can repeat the script from PowerShell itself using Sleep function or any other function (timing) it's will be better 

    next is my code can anyone help

    Import-Module "OperationsManager"
    New-SCOMManagementGroupConnection EGSCOM1
    $now = (Get-Date).AddMinutes(-1000)
    $alertnames = Get-SCOMAlert | Where {$_.Name -eq 'Logical Disk Free Space in MBytes is low' -and $_.TimeRaised -gt $now -and $_.Severity -eq "Error"}
    $datenow = get-date -Format g
    foreach ($alertname in $alertnames)
    {
    $Problem = $alertname.Name
    $time1 = $alertname.TimeRaised
    $time2 = $alertname.LastModified
    $systemname = $alertname.MonitoringObjectPath
    $smstext = "Problem :" + $alertname + " - " + "On: " + $systemname + " Started on: " + $time1 + " Finished on :" + $time2
    $logtext = $smstext + " Has been sent to Zaghloul on " + $datenow
    Add-Content "P:\SMS\Scripts\Disk Space\Logs\DiskSpaceLog.txt" $logtext
    cd P:\SMS
    .\SMSAPI.exe 01009498901 $smstext
    
    $wshell = New-Object -ComObject wscript.shell;
    Sleep 1
    $wshell.SendKeys('~')
    $nownew = get-date -Format g
    $logbody = $Problem + " Has been sent to AbdelAzziz on " + $nownew
    $wshell = New-Object -ComObject wscript.shell;
    Sleep 1
    $wshell.SendKeys('~')
    }
    $chk = "Script Done on : " + $datenow
    Add-Content "P:\SMS\Scripts\Disk Space\Logs\DiskSpaceLog.txt" $chk

    • Moved by Bill_Stewart Monday, April 30, 2018 9:15 PM This is not "teach me basic sysadmin stuff" forum
    Tuesday, February 20, 2018 8:13 AM

All replies

  • You cannot use SendKeys from the scheduler or from any background task.  SendKeys is very unreliable.

    You can get COM alerts using the SC APIs.

    Post is SCOM forum for help scripting and receiving alerts.

    Sleep can sleep for 5 minutes.

    Sleep (60 * 5)


    \_(ツ)_/

    Tuesday, February 20, 2018 8:27 AM
  • Forget about send keys i will remove it

    the question is how can repeat the script every 5 minutes (any script)

    Tuesday, February 20, 2018 9:19 AM
  • Use a scheduled job.

    help Register-ScheduledJob -full


    \_(ツ)_/

    Tuesday, February 20, 2018 9:21 AM