DBThomson76
asked on
Powershell Script to get All Scheduled Tasks that Run as a specific user
I need help in fixing this script. When I run it, it gives me the HostName of the computer but the TaskName does not come out correct. This script is checking for any scheduled tasks that run under the "UserA" account
This is how I would like it to show in the CSV file
Example.
HostName, TaskName
Server100, Reboot
Server100, Shutdown
Server101, Reboot
$Servers = Get-Content servers.txt
$user = "UserA"
foreach ($computername in $Servers)
{
$schtask = schtasks.exe /query /s $computername /V /FO CSV | ConvertFrom-Csv | Where { $_."Run As User" -eq $user}
{
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name HostName –Value $computername
$object | Add-Member –MemberType NoteProperty –Name TaskName –Value $($schtask.TaskName)
$findings+=$object;
}
}
$findings | Export-CSV -path ServerSchedultTasks.csv
Your help is greatly appreciated.
This is how I would like it to show in the CSV file
Example.
HostName, TaskName
Server100, Reboot
Server100, Shutdown
Server101, Reboot
$Servers = Get-Content servers.txt
$user = "UserA"
foreach ($computername in $Servers)
{
$schtask = schtasks.exe /query /s $computername /V /FO CSV | ConvertFrom-Csv | Where { $_."Run As User" -eq $user}
{
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name HostName –Value $computername
$object | Add-Member –MemberType NoteProperty –Name TaskName –Value $($schtask.TaskName)
$findings+=$object;
}
}
$findings | Export-CSV -path ServerSchedultTasks.csv
Your help is greatly appreciated.
ASKER
I tried to run with the change you suggested and I go this error.
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
OK, just tested this:
$Servers = Get-Content servers.txt
$user = "UserA"
$findings = foreach ($computername in $Servers){
$schtask = schtasks.exe /query /s $computername /V /FO CSV | ConvertFrom-Csv | Where { $_."Run As User" -eq $user}
Foreach ($tsk in $schtask){
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name HostName –Value $computername
$object | Add-Member –MemberType NoteProperty –Name TaskName –Value $($tsk.TaskName)
$object
}
}
$findings | Export-CSV -path ServerSchedultTasks.csv
Does that resolve your issue?
ASKER
That worked great.
Can it search for either "UserA" or "Domain\UserA"
Can it search for either "UserA" or "Domain\UserA"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome!! Thank you very much!
ASKER
This did exactly what I needed it to do, get all scheduled tasks from various computers that run under specific user accounts.
This is great for auditing.
Thanks again!!
This is great for auditing.
Thanks again!!
Glad to help. :)
Open in new window