1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| #Requires -Modules ActiveDirectory
Import-Module ActiveDirectory
# AD OU to search for computers
$OUDistinguishedName = "OU=Virtual,OU=Desktops,OU=MyDomain,DC=mydomain,DC=com"
Function Connect-TaskScheduler
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[String]$ComputerName
)
Begin
{
$objScheduledTask = New-Object -ComObject("Schedule.Service")
}
Process
{
Try
{
Write-Verbose "Connecting to $ComputerName"
$objScheduledTask.Connect("$ComputerName")
Write-Verbose "Connected: $($objScheduledTask.Connected)"
}
Catch
{
Throw "Failed to connect to $ComputerName"
}
}
End
{
Return $objScheduledTask
}
}
Function Get-AllScheduledTasks
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[ValidateScript({If ($_.GetType().BaseType.Name -eq "MarshalByRefObject" -and $_.Connected -eq $True){$True}Else{Throw "Not a valid Task Scheduler connection object"}})]
$Session,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullOrEmpty()]
[String[]]$Path = "\",
[Parameter()]
[Switch]$Recurse = $False
)
Begin
{
$Tasks = @()
$Paths = @()
}
Process
{
$Paths += Get-TaskSchedulerPaths -Session $Session -Path $Path -Recurse:$Recurse
$Tasks += $Paths | ForEach-Object {$_.Path | Get-TaskSchedulerTasks -Session $Session}
}
End
{
Return $Tasks
}
}
Function Get-TaskSchedulerPaths
{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$True)]
[ValidateNotNullOrEmpty()]
[String[]]$Path = "\",
[Parameter(Mandatory=$True)]
[ValidateScript({If ($_.GetType().BaseType.Name -eq "MarshalByRefObject" -and $_.Connected -eq $True){$True}Else{Throw "Not a valid Task Scheduler connection object"}})]
$Session,
[Parameter()]
[Switch]$Recurse = $False
)
Begin
{
$Paths = @()
}
Process
{
$BasePath = $Session.GetFolder("$Path")
$Paths += $BasePath
$Paths += $BasePath.GetFolders(1)
If ($Recurse)
{
ForEach ($P in $Paths)
{
If ($P -eq $BasePath)
{
Continue
}
Else
{
$Paths += Get-TaskSchedulerPaths -Session $Session -Path $P.Path -Recurse
}
}
}
}
End
{
Return $Paths
}
}
Function Get-TaskSchedulerTasks
{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$True)]
[ValidateNotNullOrEmpty()]
[String[]]$Path = "\",
[Parameter(Mandatory=$True)]
[ValidateScript({If ($_.GetType().BaseType.Name -eq "MarshalByRefObject" -and $_.Connected -eq $True){$True}Else{Throw "Not a valid Task Scheduler connection object"}})]
$Session
)
Begin
{
$Domain = (Get-WmiObject Win32_ComputerSystem -ComputerName $Session.TargetServer).Domain
$AllTasks = @()
}
Process
{
$Folder = $Session.GetFolder("$Path")
$FolderTasks = $Folder.GetTasks(0)
ForEach ($Task in $FolderTasks)
{
$RunAsID = $Task | Select-Object @{Name="RunAs";Expression={[xml]$xml = $_.xml ; $xml.Task.Principals.Principal.UserId}} | Select-Object -ExpandProperty RunAs -ErrorAction Stop
If ($RunAsID -match "S-\d-\d-\d\d.-\d*")
{
Try
{
$RunAs = Get-ADUser -Identity $RunAsID | Select-Object -ExpandProperty SamAccountName -ErrorAction Stop
}
Catch
{
Try
{
$RunAs = Get-ADGroup -Identity $RunAsID | Select-Object -ExpandProperty SamAccountName -ErrorAction Stop
}
Catch
{
$RunAs = $RunAsID
}
}
}
Else
{
$RunAs = $RunAsID
}
$HashTable = [Ordered]@{
ComputerName = $Session.TargetServer
Name = $Task.Name
Path = $Task.Path
RunAs = $RunAs
LastRunTime = $Task.LastRunTime
NextRunTime = $Task.NextRunTime
TaskEnabled = $Task.Enabled
}
$AllTasks += New-Object PSObject -Property $HashTable
}
}
End
{
Return $AllTasks
}
}
$Tasks = @()
$Computers = Get-ADComputer -Filter * -Properties OperatingSystem -SearchBase $OUDistinguishedName -SearchScope Subtree | Where-Object {$_.Enabled -eq $True -and $_.OperatingSystem -like "*Windows*"} | Select-Object -ExpandProperty Name | Sort-Object
Foreach ($Computer in $Computers)
{
$Connection = Connect-TaskScheduler -ComputerName $Computer -Verbose -ErrorAction Continue
$Tasks += Get-AllScheduledTasks -Session $Connection -Recurse
}
$Tasks | Export-Csv -LiteralPath "$PSScriptRoot\ScheduledTasks.csv" -Delimiter "," -NoTypeInformation |
Partager