Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-ScheduledTasks
- {
- [CmdletBinding()]
- Param
- (
- [Parameter(Mandatory=$true,
- Position=0)]
- [ValidateNotNullOrEmpty()]
- [String[]]$ComputerName,
- [Parameter(Mandatory=$false,
- Position=1)]
- [ValidateNotNullOrEmpty()]
- [switch]$HTML
- )
- #Define the script block we'll run on each machine.
- Write-Verbose "Defining script block for each machine"
- $script = {
- $cName = $env:computerName
- #Create schedule COM object and connect to it.
- $taskObj = new-object -com Schedule.Service
- $taskObj.connect($cName)
- #Get the assigned tasks and store them in a custom object
- $taskRoot = $taskObj.getfolder("\")
- $tasks = $taskRoot.GetTasks(0)
- $taskData = $tasks | select Name,
- Enabled,
- @{Name="RunAs";Expression={[xml]$xml = $_.xml ; $xml.Task.Principals.principal.userID}},
- @{Name="Author";Expression={[xml]$xml = $_.xml ; $xml.Task.RegistrationInfo.Author}}
- #Return any detected tasks.
- return $taskData
- }
- try
- {
- #Run the script block against all the servers returned (32 at a time)
- Write-Verbose "Invoking script block on each machine"
- $data = Invoke-Command -ComputerName $ComputerName -ScriptBlock $script -ErrorAction Stop
- }
- catch
- {
- Throw $("There was an error invoking command: {0}" -f $_.exception.message)
- }
- if($HTML)
- {
- #Define the HTML header for the report.
- $style = @'
- <style>
- body { background-color:#EEEEEE; }
- body,table,td,th { font-family:Tahoma; color:Black; Font-Size:10pt }
- th { font-weight:bold; background-color:#AAAAAA; }
- td { background-color:white; }
- </style>
- '@
- #Output the server report, sort it, and convert it to HTML and then store it as a string.
- Write-Verbose "Building e-mail report"
- $htmlbody = $data | where {$_.runas -cnotmatch '^S-|^System$'} |
- sort PSComputerName | ConvertTo-Html PSComputerName,Name,Enabled,RunAs,Author -Head $style -PostContent "<br>" | Out-String
- $htmlBody | Out-File .\TaskReport.html -Force
- Invoke-Expression .\TaskReport.html
- }
- else
- {
- Write-Output $data
- }
- } #END Get-ScheduledTasks
Advertisement
Add Comment
Please, Sign In to add comment