I have this script (see below) and I want:
- To have the servername in the output file in column "PSComputerName"
- 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-Pathto 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:COMPUTERNAMEinstead ofInvoke-Expression -Command 'hostname' Statusshould beState(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
