PowerShell is the most powerful scripting language designed for windows system administrators. In Windows world, anything can be accomplished using Powershell since this language can access low level .net components. PowerShell was introduced a few years ago with Windows Server 2008 and has been greatly improved in Windows Server 2012. Version 3.0 of PowerShell has a lot of new features that will be covered in this article.
Running Jobs in the Background
PowerShell allows for jobs to be run asynchronously in the background. This allows script to move on to the next line in the script without waiting for the current command to complete. Following command will get list files on the c:\ as a job
Start-Job –Name getAllFiles –ScriptBlock {Get-ChildItem –Resurse | Export-CSV –path c:\files.csv}
Get-Job command can be used to retrieve the status of the job.
New-JobTrigger can be used to schedule background jobs. This command is used as a parameter to the Register-ScheduledJob command. For example, if you want to schedule to run above command to run at 9am, use following command
$triggerJob = New-JobTrigger –Once –At 9:00AM
Register-ScheduledJob –Name GetAllFilesAt9am –Trigger $triggerJob –ScriptBlock {Get-ChildItem –Resurse | Export-CSV –path c:\files.csv}
Get-ScheduledJob to retrieve list of scheduled jobs. Note that this scheduled job will show up under Task Scheduler Library under "\Microsoft\Windows\PowerShell\ScheduledJobs"
No comments:
Post a Comment