locked
PS: run script as job, then popup/mail when finished RRS feed

  • Question

  • Hi,

    Looking for a way to schedule scripts as jobs (in order to have the Powershell console free while my script is running).
    It is working fine, but would like to have a message/popup or mail when job is finished.
    Therefore I googled and found the $jobevent. Not sure what the jobevent is doing.
    Result is this: Job #27 (PSCriptJob) complete, but then PS ISE freezes.

    Also when I run a get-job, I get this (what is this c79dd ... job)?

    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
    --     ----            -------------   -----         -----------     --------             -------                  
    27     PSCriptJob      BackgroundJob   Completed     True            localhost            powershell.exe -file $...
    29     c79ddb62-98d...                 Running       True                                 ...                      

    Please advise.
    J.


    function Submit-PSSCriptJob
    {
      [Cmdletbinding()]
      Param (
        [Parameter()]
        [String]$Path
      )


      $Job = Start-Job -Name PSCriptJob -ScriptBlock {powershell.exe -file $using:Path}

      $jobEvent = Register-ObjectEvent $job StateChanged -Action {
        Write-Host ('Job #{0} ({1}) complete.' -f $sender.Id, $sender.Name)
        $jobEvent | Unregister-Event


      }

    }

              

    Jan Hoedt

    Thursday, November 2, 2017 9:24 AM

Answers

  • How to detect a jobs completion automatically:

    $j = start-job { Get-ChildItem; Start-Sleep 60 }
    $action = { Write-Host 'job state changed' -ForegroundColor green }
    Register-ObjectEvent -InputObject $j -EventName StateChanged -SourceIdentifier myJobTest -Action $action 

    You will see the message when the job completes or fails.


    \_(ツ)_/



    • Edited by jrv Thursday, November 2, 2017 4:04 PM
    • Marked as answer by janhoedt Monday, November 27, 2017 4:49 PM
    Thursday, November 2, 2017 4:03 PM

All replies

  • Hi,

    Try this piece of code available from the link to modify your accordingly. Upvote if it helps you in any regard.

    https://gallery.technet.microsoft.com/Enabling-the-Symantec-of-306d4d01?redir=0


    Naveen Basati


    • Edited by BASATI Thursday, November 2, 2017 11:30 AM added link
    Thursday, November 2, 2017 11:29 AM
  • if you want email you can use below at the end of script with appropriate values to schedule email once task is finished

    send-mailmessage


    Thanks & Regards Ramandeep Singh

    Thursday, November 2, 2017 12:42 PM
  • How to detect a jobs completion automatically:

    $j = start-job { Get-ChildItem; Start-Sleep 60 }
    $action = { Write-Host 'job state changed' -ForegroundColor green }
    Register-ObjectEvent -InputObject $j -EventName StateChanged -SourceIdentifier myJobTest -Action $action 

    You will see the message when the job completes or fails.


    \_(ツ)_/



    • Edited by jrv Thursday, November 2, 2017 4:04 PM
    • Marked as answer by janhoedt Monday, November 27, 2017 4:49 PM
    Thursday, November 2, 2017 4:03 PM
  • Thanks! Will look at it asap.
    Small change on the script: sometimes I'm using functions, so loading the script won't do anything. That's why I changed the script -with mail- to also load the function.
    However, it does not run the function.

    What am I overlooking here?

      function Submit-PSSCriptJob
    {
      <#
          .Synopsis
          Short description
          .DESCRIPTION
          Long description
          .EXAMPLE
          Example of how to use this cmdlet
          .EXAMPLE
          Another example of how to use this cmdlet
          .TODO
        
      #>
      
      [Cmdletbinding()]
      Param (
        [Parameter()]
        [String]$Path,
        [String]$Function
      )
      

        Start-Job -Name PSCriptJob -ScriptBlock { powershell.exe -file $using:Path
                                                  $using:function
                                                  Send-MailMessage -To 'mailaddress' -From 'noreply@mailaddress' -Subject "Job FINISED wich is using $using:path" -SmtpServer "ourmailserver"
        }


    Jan Hoedt

    Thursday, November 2, 2017 4:07 PM
  • I think you have to call the function to run it

    Naveen Basati

    Friday, November 3, 2017 4:10 AM