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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

DBThomson76Author Commented:
That worked great.
Can it search for either "UserA" or "Domain\UserA"
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.