How To Free Up PowerShell While Running Long Scripts

Опубликовано: 01 Ноябрь 2024
на канале: ITPro Today
2,124
33

Discover how to efficiently manage long-running PowerShell scripts with background jobs. Brien Posey shows you how to use the Start-Job, Receive-Job, and Stop-Job cmdlets to create, monitor, and retrieve results from background jobs, keeping your PowerShell session accessible. Learn how to integrate these jobs into your scripts for optimal performance.

The Jobs.ps1 script featured in this video:

$job =  Start-Job -ScriptBlock {1..10000000 | Measure-Object -Sum }

while ($job.state -eq ‘Running’) {
                  Write-Output “Job is still running…”
                  Start-Sleep -Seconds 5
                  $job = Get-Job -Id $job.Id
}

$result = Receive-Job -Job $job

$ Display the result
Write-Output “The sum is: $($result.Sum)

$ Remove the job
Remove-Job -Job $job

Brien Posey is a bestselling technology author, speaker, and 21x Microsoft MVP. In addition to his ongoing work in IT, Posey has trained as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

For daily news, analysis, opinions and how-to’s about the IT industry, visit us at ITPro Today: https://www.itprotoday.com/

Music by Coma-Media on Pixabay

#powershellscripting #powershell #powershellscript #powershelltraining