Wednesday, October 16, 2024
HomePowershellThe way to implement your PowerShell 7 Scripts in Process Scheduler –...

The way to implement your PowerShell 7 Scripts in Process Scheduler – SID-500.COM


PowerShell

So that you’re already working with the newest PowerShell model and questioning automate your PowerShell 7 scripts with the duty scheduler? Sure? Okay, then keep tuned I’ll present you two methods to incorporate a PS7 script within the job scheduler. Let’s dive in.

Choice 1

On this part I’ll wrap my PowerShell script in a PowerShell 5 Scheduled Job. Observe {that a} PowerShell 5 scheduled job shall be generated and your PS7 script shall be executed from this PS5 job.

Right here is an instance of work this out.

# Choice 1: PowerShell 7 wrapped in PowerShell 5 ScheduledJobs

Register-ScheduledJob -Identify Test1 -ScriptBlock {pwsh.exe $homescript.ps1} `
-Set off (New-JobTrigger -As soon as -At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes 15) `
-RepeatIndefinitely)

Choice 2 (beneficial)

On this possibility I’m going to indicate you the creation of a scheduled taks to your PowerShell 7 script. That is the beneficial possibility as a result of your script shall be executed extra straight with out utilizing PowerShell 5 within the background.

# Choice 2: Scheduled-Process

$Motion = New-ScheduledTaskAction -Execute `
"pwsh –Noprofile -WindowStyle Hidden -ExecutionPolicy Bypass -File $homescript.ps1"
$Set off = New-ScheduledTaskTrigger -As soon as -At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes 15)
$Principal = New-ScheduledTaskPrincipal -UserId pagradministrator
$Process = New-ScheduledTask -Motion $Motion -Set off $Set off -Principal $Principal
Register-ScheduledTask -TaskName "Test4" -InputObject $Process -Drive

Conclusion

Choice 1 and Choice 2 do the identical factor. Nonetheless, possibility 2 will begin the duty and the script the place it ought to begin, in Powershell 7.

Printed by Patrick Gruenauer

Microsoft MVP on PowerShell [2018-2023], IT-Coach, IT-Guide, MCSE: Cloud Platform and Infrastructure, Cisco Licensed Academy Teacher.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments