PS: function to schedule a script as job, popup once finished + remove job?

Hi,

I have a  PS function which schedules a script as a job by path which works fine.
However, I'd like it to popup with a message once done and remove the job.
Please advise.

This is the function so far:

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

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

}
janhoedtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
You could do something like this:

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

    Start-Job -Name PSCriptJob -ScriptBlock {powershell.exe -file $using:Path} | Wait-Job
    "Job Complete"
    Get-Job PSCriptjob | Remove-Job

}

Open in new window

0
janhoedtAuthor Commented:
If you wait for the job, the function is useless.
The main reason for this function is to not occupy the powershell console while a script is running.
0
janhoedtAuthor Commented:
Googled a bit and came around with this:

 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


  }
   
}

Not sure what the jobevent is doing.
Result is this: Job #27 (PSCriptJob) complete, but then PS ISE freezes.
J.
0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Ah, I see. There's definitely a more elegant way of doing this but here's something quick I whipped up:
function Submit-PSSCriptJob {
    [Cmdletbinding()]
    Param (
        [Parameter()]
        [String]$Path
    )
  
    $command = @"
        . "$Path"
        Read-Host "Job complete.`nPress enter to close the window."
"@

    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    Start-Process powershell -ArgumentList "-EncodedCommand $encodedCommand" 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Viable solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.