2
votes

Hello I hope you guys can help me with a problem that is bugging me for a couple of days now. I cannot get the output right when I export the results of the script to csv file I get the following.

What I See What I See

What I would like to see
enter image description here

function Get-ScheduledTask
{
    [CmdletBinding()]

    param(
        [Parameter(
        Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [String[]]$ComputerName,

        [Parameter(Mandatory=$false)]
        [String[]]$RunAsUser,

        [Parameter(Mandatory=$false)]
        [String[]]$TaskName,

        [parameter(Mandatory=$false)]
        [alias("WS")]
        [switch]$WithSpace
     )

    Begin
    {

        $Script:Tasks = @()
    }

    Process
    {
        $schtask = schtasks.exe /query /s $ComputerName  /V /FO CSV | ConvertFrom-Csv
        Write-Verbose  "Getting scheduled Tasks from: $ComputerName"

        if ($schtask)
        {
            foreach ($sch in $schtask)
            {
                if ($sch."Run As User" -match "$($RunAsUser)" -and $sch.TaskName -match "$($TaskName)")
                {
                    Write-Verbose  "$Computername ($sch.TaskName).replace('\','') $sch.'Run As User'"
                    $sch  | Get-Member -MemberType Properties | ForEach -Begin {$hash=@{}} -Process {
                        If ($WithSpace)
                        {
                            ($hash.($_.Name)) = $sch.($_.Name)
                        }
                        Else
                        {
                            ($hash.($($_.Name).replace(" ",""))) = $sch.($_.Name)
                        }
                    } -End {
                        $script:Tasks += (New-Object -TypeName PSObject -Property $hash)
                    }
          }
            }
        }
    }

    End
    {
        $Script:Tasks
    }
}

$ComputerName = "SE94ABH02"
$ServiceAccounts = Get-Content "D:\Scripts\Test-Peter\Testing\ServiceAccounts.txt"

$obj = New-Object –TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name ServerName -Value $ComputerName
$obj | Add-Member -MemberType NoteProperty -Name TaskName -Value ([string]::Join(",",(@())))
$obj | Add-Member -MemberType NoteProperty -Name ScheduledTaskState -Value ([string]::Join(",",(@())))
$obj | Add-Member -MemberType NoteProperty -Name LogonMode -Value ([string]::Join(",",(@())))
$obj | Add-Member -MemberType NoteProperty -Name Author -Value ([string]::Join(",",(@())))
$obj | Add-Member -MemberType NoteProperty -Name RunAsUser -Value ([string]::Join(",",(@())))
$obj | Add-Member -MemberType NoteProperty -Name ServiceName -Value ([string]::Join(",",(@())))
$obj | Add-Member -MemberType NoteProperty -Name StartName -Value ([string]::Join(",",(@())))


$SCHTSk = Get-ScheduledTask $ComputerName | Where-Object {$_.RunAsUser -like "NLKVKF94*"} | Select TaskName,ScheduledTaskState,LogonMode,Author,RunAsUser

If ($SCHTSK) {

        $TEMP = @()

            foreach ($TskItem in $SCHTSK) {
                If ($TskItem -match "NLKVKF94") {
                    $TEMP += $TskItem

                    $info = @{
                        'TaskName'=$TEMP.TaskName;
                        'ScheduledTaskState'=$TEMP.ScheduledTaskState;
                        'LogonMode'=$TEMP.LogonMode;
                        'Author'=$TEMP.Author;
                        'RunAsUser'=$TEMP.RunAsUser
                    }
                }
            }

            $tskobj = New-Object -TypeName PSObject -Property $info 
            $obj.TaskName += $tskobj.TaskName
            $obj.ScheduledTaskState += $tskobj.ScheduledTaskState
            $obj.LogonMode += $tskobj.LogonMode
            $obj.Author += $tskobj.Author
            $obj.RunAsUser += $tskobj.RunAsUser
}




$WmiObjectResultaat = Get-WmiObject -Class win32_service -computer $ComputerName | select-object __SERVER,Name,StartName

If ($WmiObjectResultaat) {

    $TEMP = @()

            foreach ($item in $WmiObjectResultaat) { 
                if ($ServiceAccounts -contains $Item.StartName) { 
                    $TEMP += $Item

                    $info = @{
                        'Name'=$TEMP.Name;
                        'Startname'=$TEMP.Startname
                    }
                }
            }

            $Srvobj = New-Object -TypeName PSObject -Property $info
            $obj.ServiceName += $Srvobj.Name
            $obj.StartName += $Srvobj.Startname


}

$obj | Export-Csv -Path "D:\Scripts\Test-Peter\Testing\lalala.csv" -NoTypeInformation
1
If you're using Powershell, you may want to look at the ScheduledTasks module that ships with Windows.Bacon Bits

1 Answers

1
votes

You've created a single object $obj to hold all the task details, instead of a collection of individual tasks:

$obj = New-Object –TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name ServerName -Value $ComputerName
$obj | Add-Member -MemberType NoteProperty -Name TaskName -Value ([string]::Join(",",(@())))
# ... and so on

Again, instead of adding individual tasks to an array/collection, you add the value of each property to the same property of $obj:

$tskobj = New-Object -TypeName PSObject -Property $info 
$obj.TaskName += $tskobj.TaskName
$obj.ScheduledTaskState += $tskobj.ScheduledTaskState
$obj.LogonMode += $tskobj.LogonMode
$obj.Author += $tskobj.Author
$obj.RunAsUser += $tskobj.RunAsUser

All you need to do is change $obj to an empty collection and assign the tasks to it instead:

$obj = @()

foreach($Task in (Get-ScheduledTask)){
    $TaskProperties = @{
        'TaskName'=$Task.TaskName;
        'ScheduledTaskState'=$Task.ScheduledTaskState;
        'LogonMode'=$Task.LogonMode;
        'Author'=$Task.Author;
        'RunAsUser'=$Task.RunAsUser
    }
    $obj += New-Object psobject -Property $TaskProperties
}

$obj | Export-Csv "output.csv"