From 520c9be9f6c176c42a1cfea3f6d681238059130a Mon Sep 17 00:00:00 2001 From: P <51826628+patduckless@users.noreply.github.com> Date: Tue, 7 Dec 2021 16:36:24 +0000 Subject: [PATCH] Update Check_RunAsAccount.ps1 Added use of credentials for PSRemoting. Added ability to export all tasks with information. --- General/Scheduled task/Check_RunAsAccount.ps1 | 99 +++++++++++-------- 1 file changed, 58 insertions(+), 41 deletions(-) diff --git a/General/Scheduled task/Check_RunAsAccount.ps1 b/General/Scheduled task/Check_RunAsAccount.ps1 index 0685641..57bc5ee 100644 --- a/General/Scheduled task/Check_RunAsAccount.ps1 +++ b/General/Scheduled task/Check_RunAsAccount.ps1 @@ -1,55 +1,72 @@ <# .SYNOPSIS This Script is to check scheduled task that use the specified run as account or accounts on a list of devices specifed in a txt file. - .DESCRIPTION This Script is to check scheduled task that use the specified run as account or accountson a list of devices specifed in a txt file. To search for these scheduled task the script will call schtask.exe. - .EXAMPLE .\Check_RunAsAccount.ps1 -CompList D:\Scripts\Task_Scheduler\Complist.txt -RunAsAccount test1 -ExportLocation D:\Scripts\Task_Scheduler\ - .EXAMPLE .\Check_RunAsAccount.ps1 -CompList D:\Scripts\Task_Scheduler\Complist.txt -RunAsAccount test1,test2 -ExportLocation D:\Scripts\Task_Scheduler\ #> -## Set script parameter -param( -[parameter(Mandatory = $true)] -[String]$CompList, -[parameter(Mandatory = $true)] -[String[]]$RunAsAccount, -[parameter(Mandatory = $true)] -[String]$ExportLocation -) - -## 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 "Checkig $($comp)" -ForegroundColor Green - -## Check scheduled task for specified run as account -$schtask = schtasks.exe /query /V /S $comp /FO CSV | ConvertFrom-Csv | Select-Object HostName,TaskName,Status,"Next Run Time","Run As User" | -Where-Object {$_."Run As User" -contains $RunAsAccount} -if ($schtask){ - -## Export results -Write-Host "Task found exporting to results to $($ExportLocation)" -$schtask | Export-Csv "$ExportLocation\ScheduledTaskExport.csv" -NoTypeInformation -Append -} -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 -} +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-csv "$ExportLocation\ScheduledTaskExport.csv" -NoTypeInformation -Append + } + 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 + } + } }