将pipe道导出到一个csv文件

我想弄清楚如何导出到一个CSV文件。 我没有问题导出到Excel,但是当我尝试将其导出到一个CSV文件我搞砸了。 我把广告拉了出来。

Foreach($result in $results){ $User = $result.GetDirectoryEntry() $DistinguishedName = $USER.DistinguishedName -replace "'", "''" $CN = $USER.CN -replace "'", "''" $name = $USER.name -replace "'", "''" $userAccountControl = $USER.userAccountControl $extensionAttribute7 = $User.extensionattribute7 -replace "'", "''" $extensionAttribute1 = $User.extensionattribute1 -replace "'", "''" $operatingSystem = $USER.operatingSystem -replace "'", "''" $pwdLastSet = [datetime]::FromFileTimeUtc((ConvertADSLargeInteger $USER.pwdLastSet[0])) if( ( [datetime]"1900-01-01" - $pwdLastSet ).Days -gt 0 ) { $pwdLastSet = [datetime]"1900-01-01" } 

填写一般表(1)的信息

 foreach ($objItem in $result){ $Sheet1.Cells.Item($intRow, 1) = $CN $Sheet1.Cells.Item($intRow, 2) = $operatingSystem $Sheet1.Cells.Item($intRow, 3) = $pwdLastSet $Sheet1.Cells.Item($intRow, 4) = $DistinguishedName $Sheet1.Cells.Item($intRow, 5) = $extensionAttribute7 $Sheet1.Cells.Item($intRow, 6) = $extensionAttribute1 } 

我将创build一个数组,并根据脚本中的对象填充对象,然后可以通过循环来填充电子表格,还可以使用它来导出为CSV。

 $Users = @() Foreach($result in $results){ $User = $result.GetDirectoryEntry() $pwdLastSet = [datetime]::FromFileTimeUtc((ConvertADSLargeInteger $USER.pwdLastSet[0])) if( ( [datetime]"1900-01-01" - $pwdLastSet ).Days -gt 0 ) { $pwdLastSet = [datetime]"1900-01-01" } $Users+=[PSCustomObject][Ordered]@{ User = $User DistinguishedName = $USER.DistinguishedName -replace "'", "''" CN = $USER.CN -replace "'", "''" name = $USER.name -replace "'", "''" userAccountControl = $USER.userAccountControl extensionAttribute7 = $User.extensionattribute7 -replace "'", "''" extensionAttribute1 = $User.extensionattribute1 -replace "'", "''" operatingSystem = $USER.operatingSystem -replace "'", "''" pwdLastSet = $pwdLastSet } } For($intRow=1;$intRow -le $Users.count;$intRow++){ $Sheet1.Cells.Item($intRow, 1) = $Users[$intRow-1].CN $Sheet1.Cells.Item($intRow, 2) = $Users[$intRow-1].operatingSystem $Sheet1.Cells.Item($intRow, 3) = $Users[$intRow-1].pwdLastSet $Sheet1.Cells.Item($intRow, 4) = $Users[$intRow-1].DistinguishedName $Sheet1.Cells.Item($intRow, 5) = $Users[$intRow-1].extensionAttribute7 $Sheet1.Cells.Item($intRow, 6) = $Users[$intRow-1].extensionAttribute1 } $Users | Export-Csv C:\Path\To\NewFile.csv -NoTypeInformation 

只需将$result导入ForEach-Object循环,并从对象中select相关属性并导出:

 $results | % { $_.GetDirectoryEntry() } | select CN, operatingSystem, pwdLastSet, DistinguishedName, extensionattribute7, extensionattribute1 | Export-Csv 'C:\output.csv' -NoType 

您可以使用计算的属性来replace具有其自身的修改版本的属性:

 $results | % { $_.GetDirectoryEntry() } | select @{n='CN';e={$_.CN -replace "'","''"}}, @{n='operatingSystem';e={$_.operatingSystem -replace "'","''"}}, @{n='pwdLastSet';e={ $d = [DateTime]::FromFileTimeUtc($_.pwdLastSet[0]) if ( $d.Date -lt [DateTime]"1900-01-01" ) { [DateTime]"1900-01-01" } else { $d } }}, @{n='DistinguishedName';e={$_.DistinguishedName -replace "'","''"}}, @{n='extensionattribute7';e={$_.extensionattribute7 -replace "'","''"}}, @{n='extensionattribute1';e={$_.extensionattribute1 -replace "'","''"}} | Export-Csv 'C:\output.csv' -NoType