PowerShell:将结果附加到单个csv中

时间:2017-09-27 17:46:27

标签: powershell append export-to-csv

我有一个针对我们的调度程序运行的脚本,该脚本将获取文件夹中每个作业的作业名称和位置路径。为了让我的脚本导出目录中的所有作业,我在foreach语句中包含了“$ results + = New-Object PSObject -Property $ details”,它迭代文件夹中的所有作业,而不执行此操作只会返回上一个作业和路径,因为这是最后定义。

我的问题是当我添加另一个目录时。第一个目录将针对第二个目录进行迭代,因此csv将为第一个目录中的每个作业打印第二个目录中的所有作业。为了解决这个问题,我想独立于彼此运行每个目录,将结果附加到单个csv文件中。请告知如何做到这一点。

$results = @()
#Create new Job Scheduler Object and connect to Job Scheduler
$SchedulerObject = New-Object -ComObject "ActiveBatch.AbatJobScheduler"; 
$SchedulerObject.Connect("JOBSERVER");


#Trade Support Schedules
$JobObjects1 = $SchedulerObject.Search("/TradeSupport/Objects/Schedules/","*",65535,"*",$true);
foreach($JobObject1 in $JobObjects1){

    $Jobs1 = $JobObject1.getAbatObject();
    foreach($Job1 in $Jobs1){
        write-host("Name :" + $Job1.Name)
        write-host("Path :" + $Job1.Path)
    }
    #Operations Schedules
    $JobObjects2 = $SchedulerObject.Search("/Operations/Objects/Schedules/","*",65535,"*",$true);
    foreach($JobObject2 in $JobObjects2){
        $Jobs2 = $JobObject2.getAbatObject();
        foreach($Job2 in $Jobs2){
            write-host("Name :" + $Job2.Name)
            write-host("Path :" + $Job2.Path)
        }
        $details = @{
                TradeSupport_Schedule_Name = $Job1.Name
                TradeSupport_Schedule_Path = $Job1.FullPath
                Operations_Schedule_Name = $Job2.Name
                Operations_Schedule_Path = $Job2.FullPath
                BackOffice_Schedule_Name = $BOSchedName
                BackOffice_Schedule_Path =$BOSchedPath
        }
        $results += New-Object PSObject -Property $details

     write-host("************************************************************")
    }
}
$results | select-object -property TradeSupport_Schedule_Name,TradeSupport_Schedule_Path,Operations_Schedule_Name,Operations_Schedule_Path | export-csv -Path \\computer\c$\Support\AB_Reports\Objects_Report.csv -NoTypeInformation 
$SchedulerObject.Disconnect();

0 个答案:

没有答案