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.
DBThomson76Asked:
Who is Participating?
 
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Sure.

$Servers = Get-Content servers.txt
$user = "UserA"
$domain = "domain"
$domusr = "$domain\$user"

$findings = foreach ($computername in $Servers){

    $schtask = schtasks.exe /query /s $computername /V /FO CSV | ConvertFrom-Csv | Where { $_."Run As User" -eq $user -or $_."Run As User" -eq $domusr} 

    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

Open in new window

0
 
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Haven't run this but see if this works:

$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} 

    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)
        $findings+=$object
    }
}

$findings | Export-CSV -path ServerSchedultTasks.csv

Open in new window

0
 
DBThomson76Author Commented:
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.
0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
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

Open in new window

Does that resolve your issue?
0
 
DBThomson76Author Commented:
That worked great.
Can it search for either "UserA" or "Domain\UserA"
0
 
DBThomson76Author Commented:
Awesome!! Thank you very much!
0
 
DBThomson76Author Commented:
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!!
0
 
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Glad to help. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.