Home > Blockchain >  Powershell scrip get list of scheduled tasks
Powershell scrip get list of scheduled tasks

Time:01-29

I have this script (see below) and I want:

  1. To have the servername in the output file in column "PSComputerName"
  2. Two files, one the disabled tasks and one file with the enabled tasks
$ComputerName = Invoke-Expression -Command 'hostname'
$FilePathEnabled = "c\temp"   $ComputerName   "-EnabledTasksResult.csv"
$FilePathDisabled = "c\temp"   $ComputerName   "-DisabledTasksResult.csv"

(Get-ScheduledTask).where{!($_.TaskPath -like "\Microsoft\*") -and !($_.Status -eq "Disabled")} | Get-ScheduledTaskInfo | 
Export-Csv -NoTypeInformation -Path $FilePathEnabled 

(Get-ScheduledTask).where{!($_.TaskPath -like "\Microsoft\*") -and !($_.Status -ne "Disabled")} | Get-ScheduledTaskInfo | 
Export-Csv -NoTypeInformation -Path $FilePathDisabled 

Who could help me? Thank you

CodePudding user response:

Some remarks on your code first:

  • to create a file path, best use Join-Path to combine the elements or the .Net method [IO.Path]::Combine()
  • you forgot a colon after the drive letter "c\temp"
  • the easiest way to get your computername in a variable is to use the environment: $env:COMPUTERNAME instead of Invoke-Expression -Command 'hostname'
  • Status should be State (at least on my Win10 machine..)

Next, I would first collect all tasks, enabled or not and then filter out the enabled and disabled ones from that collection. This saves you from running the Get-ScheduledTask cmdlet twice.

$ComputerName     = $env:COMPUTERNAME
$FilePathEnabled  = Join-Path -Path 'c:\temp ' -ChildPath "$ComputerName-EnabledTasksResult.csv"
$FilePathDisabled = Join-Path -Path 'c:\temp ' -ChildPath "$ComputerName-DisabledTasksResult.csv"

$allTasks = (Get-ScheduledTask).Where{$_.TaskPath -notlike "\Microsoft\*"}
# filter and export the Enabled tasks
$allTasks.Where{$_.State -ne 'Disabled'} | Get-ScheduledTaskInfo | 
    Select-Object *, @{Name = 'PSComputerName'; Expression = {$ComputerName}} -ExcludeProperty PSComputerName |
    Export-Csv -Path $FilePathEnabled  -NoTypeInformation

# filter and export the Disabled tasks
$allTasks.Where{$_.State -eq 'Disabled'} | Get-ScheduledTaskInfo |
    Select-Object *, @{Name = 'PSComputerName'; Expression = {$ComputerName}} -ExcludeProperty PSComputerName |
    Export-Csv -Path $FilePathDisabled  -NoTypeInformation

CodePudding user response:

In order to include the computer name in the output, you could use the select-object to create a custom object with the added property as follow:


Get-ScheduledTaskInfo | Select-Object *,@{Name = 'PSComputerName'; Expression = {$ComputerName}}

So you code will be something like that

$ComputerName = Invoke-Expression -Command 'hostname'
$FilePathEnabled = "c:\temp\"   $ComputerName   "-EnabledTasksResult.csv"
$FilePathDisabled = "c:\temp\"   $ComputerName   "-DisabledTasksResult.csv"

(Get-ScheduledTask).where{!($_.TaskPath -like "\Microsoft\*") -and !($_.Status -eq "Disabled")} | Get-ScheduledTaskInfo | Select-Object *,@{Name = 'PSComputerName'; Expression = {$ComputerName}}
Export-Csv -NoTypeInformation -Path $FilePathEnabled 

(Get-ScheduledTask).where{!($_.TaskPath -like "\Microsoft\*") -and !($_.Status -ne "Disabled")} | Get-ScheduledTaskInfo | | Select-Object *,@{Name = 'PSComputerName'; Expression = {$ComputerName}}
Export-Csv -NoTypeInformation -Path $FilePathDisabled 

  •  Tags:  
  • Related