Powershell Export-Csv会产生不希望的结果

时间:2015-10-21 17:19:21

标签: csv powershell tostring

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

我所看到的 What I 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 个答案:

答案 0 :(得分:0)

您已创建单个对象$.ajax("login.php",{ type : "POST", data : {username:username,password:password},// only this line is changed. success : function(data){ if(data == "ok"){ $("#message").html(data); }else{ $("#fail").fadeIn(); } } }); 来保存所有任务详细信息,而不是单个任务的集合:

$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 的相同属性中:

$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