-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction Get-ScheduledTaskUsers.ps1
More file actions
53 lines (53 loc) · 2.19 KB
/
function Get-ScheduledTaskUsers.ps1
File metadata and controls
53 lines (53 loc) · 2.19 KB
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
function Get-ScheduledTaskUsers
{
## Set script parameter
param(
[parameter(Mandatory = $true)]
[String]$CompList,
[parameter(Mandatory = $false)]
[String[]]$RunAsAccount,
[parameter(Mandatory = $true)]
[String]$ExportLocation,
[Parameter(Mandatory = $true)]
[PSCredential]$Credential
)
## Get list of device to check
$comps = Get-Content $CompList
## Loop through each device
foreach ($comp in $comps)
{
Write-Host "Testing connection to $($comp)" -ForegroundColor DarkGreen
$TC = Test-Connection $comp -Count 1 -ErrorAction SilentlyContinue
if ($TC)
{
Write-Host "Checking $($comp)" -ForegroundColor Green
## Check scheduled task for specified run as account
if ($null -ne $RunAsAccount)
{
$schtask = Invoke-Command -Credential $Credential -ComputerName $comp -ArgumentList $RunAsAccount -ScriptBlock {
schtasks.exe /query /V /FO CSV | ConvertFrom-Csv | Select-Object HostName, TaskName, Status, "Next Run Time", "Run As User" |
Where-Object { $_."Run As User" -contains $RunAsAccount } }
}
else
{
$schtask = Invoke-Command -Credential $Credential -ComputerName $comp -ArgumentList $RunAsAccount -ScriptBlock {
schtasks.exe /query /V /FO CSV | ConvertFrom-Csv | Select-Object HostName, TaskName, Status, "Next Run Time", "Run As User" }
}
if ($schtask)
{
## Export results
Write-Host "Task found exporting to results to $($ExportLocation)"
$schtask | Export-Excel "$ExportLocation\ScheduledTaskExport.xlsx"
}
else
{
Write-Host "No task found with run as account"
}
}
else
{
Write-Host "$($comp) not responding Exporting failures to log file located in $($ExportLocation)" -ForegroundColor Yellow
$comp | Out-File "$ExportLocation\FailureReport.log" -NoTypeInformation -Append
}
}
}