Powershell Export-Csv 给出了不希望的结果

Powershell Export-Csv gives undesired result

你好,我希望你们能帮助我解决困扰我几天的问题。 当我将脚本结果导出到 csv 文件时无法正确输出我得到以下内容。

我看到的

我想看什么

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

您创建了一个对象 $obj 来保存所有任务详细信息,而不是单个任务的集合:

$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

同样,不是将单个任务添加到 array/collection,而是将每个 属性 的值添加到 $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

您需要做的就是将 $obj 更改为空集合并将任务分配给它:

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