【问题标题】:Powershell custom append object to csv filePowershell自定义附加对象到csv文件
【发布时间】:2018-05-11 18:16:28
【问题描述】:

我正在尝试将自定义对象输出到 csv 格式的文本文件,因为我循环遍历每个对象。每行一个对象。

但是没有写入文件。

是要转换类型的东西吗?

$rechten = Get-ADGroupMember -Identity $v -Recursive -ERRORACTION silentlycontinue | Get-ADUser -Property DisplayName -ERRORACTION silentlycontinue | Select-Object Name

Write-Host -ForegroundColor Yellow "ADgroup $v wordt uitgevlooid."

foreach ($rechtenhouder in $rechten) {
    $objResults = New-Object PSObject
    $objResults | Add-Member -MemberType NoteProperty -Name DirectoryPath -Value $objPath
    $objResults | Add-Member -MemberType NoteProperty -Name Identity -Value    $rechtenhouder.name
    $objResults | Add-Member -MemberType NoteProperty -Name Systemrights -Value $accessRight.FileSystemRights
    $objResults | Add-Member -MemberType NoteProperty -Name systemrightstype -Value $accessRight.accesscontroltype
    $objResults | Add-Member -MemberType NoteProperty -Name isinherited -Value $accessRight.isinherited
    $objResults | Add-Member -MemberType NoteProperty -Name inheritanceflags -Value $accessRight.inheritanceflags
    $objResults | Add-Member -MemberType NoteProperty -Name rulesprotected -Value $objACL.areaccessrulesprotected
    $objResults | Add-Member -MemberType NoteProperty -Name Adtype -Value "User"

    $arrResults += $objResults
    Add-Content $exportpathtxtappend $objresults
}

【问题讨论】:

  • 您无需编写数百行添加注释属性,只需使用$objResult = New-Object psobject @{DirectoryPath = $objPath; Identity = "and so on...."} 创建对象
  • 不要使用Add-ContentExport-CSV 是你的朋友。
  • 我想使用 ac,因为如果脚本因可能的原因而中断,我会在文件中已经有一些输出。

标签: powershell csv object append


【解决方案1】:

对于您的特定用途,一次或分批导出所有对象将是最有效的,但有时一次将一条记录导出到 CSV 文件是有意义的,这就是我提出这个问题的原因,所以我想发布我的解决方案。

使用Export-CSV -Append 不断添加到 csv 文件的末尾。

foreach ($rechtenhouder in $rechten) {
    $objResults = New-Object PSObject -Property @{
        DirectoryPath    = $objPath;
        Identity         = $rechtenhouder.name;
        Systemrights     = $accessRight.FileSystemRights;
        systemrightstype = $accessRight.accesscontroltype;
        isinherited      = $accessRight.isinherited;
        inheritanceflags = $accessRight.inheritanceflags;
        rulesprotected   = $objACL.areaccessrulesprotected;
        Adtype           = "User";
    }

    $objResults | Export-CSV $csvPath -Append -NoTypeInformation
}

如果您以设定的时间间隔不断轮询,这很有用,但如果您正在迭代对象集合,则只需一次将它们全部导出即可。例如,我会使用这种导出脚本的方法,如下所示:

while($true){
    $procs = Get-Process | Select-Object Name,CPU
    $procs | Add-Member -type NoteProperty -Name "Timestamp" -Value $(Get-Date)

    $procs | Export-CSV $csvPath -Append -NoTypeInformation

    sleep -Seconds 60
}

【讨论】:

    【解决方案2】:

    首先,我建议您以一种更聪明的方式创建您的对象:

    foreach ($rechtenhouder in $rechten) {
        $objResults = New-Object PSObject -Property @{
            DirectoryPath    = $objPath;
            Identity         = $rechtenhouder.name;
            Systemrights     = $accessRight.FileSystemRights;
            systemrightstype = $accessRight.accesscontroltype;
            isinherited      = $accessRight.isinherited;
            inheritanceflags = $accessRight.inheritanceflags;
            rulesprotected   = $objACL.areaccessrulesprotected;
            Adtype           = "User";
        }
        $arrResults += $objResults
    }
    

    完成此操作后,您的 $arrResults 现在包含您的对象。这可以使用内置的 PowerShell 轻松导出到 CSV 文件 Export-CSV:

    $arrResults | Export-Csv -Path "C:/temp/text.csv"
    

    在每次循环迭代中使用Add-Content 恕我直言对性能无效。如果您的脚本运行了很长时间并且您想间隔保存当前状态,您可以例如开始一个异步工作 - 假设每 10 次迭代 - 导出你当前的数组:

    $i = 0
    foreach ($rechtenhouder in $rechten) {
        $objResults = New-Object PSObject -Property @{
            DirectoryPath    = $objPath;
            Identity         = $rechtenhouder.name;
            Systemrights     = $accessRight.FileSystemRights;
            systemrightstype = $accessRight.accesscontroltype;
            isinherited      = $accessRight.isinherited;
            inheritanceflags = $accessRight.inheritanceflags;
            rulesprotected   = $objACL.areaccessrulesprotected;
            Adtype           = "User";
        }
        $arrResults += $objResults
        if ($i % 10 -eq 0) {
            Start-Job -ScriptBlock {
                param($T, $Path)
                $T | Export-Csv -Path $Path
            } -ArgumentList @($arrTest, "Path/to/script")
        }
        $i++
    }
    

    【讨论】:

    • 喜欢那个异步工作的想法!我从来没有和start-job一起工作过。 text.csv 中只添加了一行。我必须在脚本块参数中弄乱/丢失。 $arr结果 | out-file $exportPathtxt 确实填写为 expeted 另一个文本文件。
    • 很高兴听到!请记住 - 如果循环比我预期的要快,您可能必须将每个 10th 作业调整为每个 20th 作业左右。如果没有,您将遇到并发问题。另外,不要忘记在最后一次循环之后导出。如果没有,你会错过一些条目。希望对您有所帮助。
    • 我在脚本块中添加了一个路径参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2016-10-08
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多