简体   繁体   English

将循环导出到CSV-PowerShell

[英]Export Loops to CSV - PowerShell

I have a script that takes properties of schedules from two directories and writes the output to the host. 我有一个脚本,该脚本从两个目录获取日程表的属性,并将输出写入主机。 I want to capture this output into a csv. 我想将此输出捕获到csv中。 So far the script writes all attributes to the host correctly, but the csv only has the headers, ie "Name" and "Path". 到目前为止,脚本已将所有属性正确写入主机,但是csv仅具有标头,即“名称”和“路径”。

Here is what I have: 这是我所拥有的:

$results = @()
$JobObjects = $SchedulerObject.Search("/TradeSupport/Objects/Schedules/","*",65535,"*",$true);
foreach($JobObject in $JobObjects){
$Schedules = $JobObject.getabatobject();
foreach ($Schedule in $Schedules){
write-host("Name :" + $Schedule.Name)
write-host("Path :" + $Schedule.Path)

    $details = @{
            Name = $ScheduleObjects.Name
            Path = $ScheduleObjects.FullPath
            }}}

$JobObjects2 = $SchedulerObject.Search("/Operations/Objects/Schedules/","*",65535,"*",$true);
foreach($JobObject2 in $JobObjects2){
$Schedules2 = $JobObject2.getabatobject();
foreach ($Schedule2 in $Schedules2){
write-host("Name :" + $Schedule2.Name)
write-host("Path :" + $Schedule2.Path)


$details = @{
            Name = $ScheduleObjects2.Name
            Path = $ScheduleObjects2.FullPath
        }}  
    }
    $results += New-Object PSObject -Property $details


$results | select-object -property Name,Path | export-csv -Path \\ch0-craab-01\c$\Support\AB_Reports\Objects_Report.csv -NoTypeInformation 

What am I doing wrong? 我究竟做错了什么?

Put the $Results inside the Foreach Loops not on the outside $ Results放在Foreach循环内而不在外面

The OP said this didn't work and i took a second look at the script. OP说这不起作用,我再次看了看脚本。

The variable the OP is using to save to $results is unique and holds no information. OP用于保存到$ results的变量是唯一的,不包含任何信息。

The correct variables are 正确的变量是

$Schedule.Name
$Schedule.Name

But you are using 但是你在用

$ScheduleObjects.Name
$ScheduleObjects.FullPath

The below script should now work 下面的脚本现在应该可以工作

$results = @()
$JobObjects = $SchedulerObject.Search("/TradeSupport/Objects/Schedules/","*",65535,"*",$true);
foreach($JobObject in $JobObjects){
    $Schedules = $JobObject.getabatobject();
    foreach ($Schedule in $Schedules){
        write-host("Name :" + $Schedule.Name)
        write-host("Path :" + $Schedule.Path)
        $details = @{
            Name = $Schedule.Name
            Path = $Schedule.Path
        }
        $results += New-Object PSObject -Property $details
    }
}

$JobObjects2 = $SchedulerObject.Search("/Operations/Objects/Schedules/","*",65535,"*",$true);
foreach($JobObject2 in $JobObjects2){
    $Schedules2 = $JobObject2.getabatobject();
    foreach ($Schedule2 in $Schedules2){
        write-host("Name :" + $Schedule2.Name)
        write-host("Path :" + $Schedule2.Path)
        $details = @{
            Name = $Schedule2.Name
            Path = $Schedule2.Path
        }
        $results += New-Object PSObject -Property $details
    }  
}    
$results | select-object -property Name,Path | export-csv -Path \\ch0-craab-01\c$\Support\AB_Reports\Objects_Report.csv -NoTypeInformation

It's easier to see where you go wrong when you indent the code properly to line up with your loops: 正确缩进代码以使其与循环对齐时,更容易发现出错的地方:

$results = @()
$JobObjects = $SchedulerObject.Search("/TradeSupport/Objects/Schedules/","*",65535,"*",$true);
foreach($JobObject in $JobObjects) {
    $Schedules = $JobObject.getabatobject();
    foreach ($Schedule in $Schedules){
        write-host("Name :" + $Schedule.Name)
        write-host("Path :" + $Schedule.Path)

        $details = @{
            Name = $ScheduleObjects.Name
            Path = $ScheduleObjects.FullPath
        }
    }
}

$JobObjects2 = $SchedulerObject.Search("/Operations/Objects/Schedules/","*",65535,"*",$true);
foreach($JobObject2 in $JobObjects2) {
    $Schedules2 = $JobObject2.getabatobject();
    foreach ($Schedule2 in $Schedules2) {
        write-host("Name :" + $Schedule2.Name)
        write-host("Path :" + $Schedule2.Path) 

        $details = @{
            Name = $ScheduleObjects2.Name
            Path = $ScheduleObjects2.FullPath
        }
    }  
}
$results += New-Object PSObject -Property $details


$results | select-object -property Name,Path | export-csv -Path \\ch0-craab-01\c$\Support\AB_Reports\Objects_Report.csv -NoTypeInformation

Now you can more easily see that you only ever use your $details variable outside of the loops. 现在,您可以更轻松地看到,您仅在循环使用$details变量。 You want to append that item on to your $results array on every iteration. 您希望在每次迭代时将该项目追加到$results数组上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM