将管道导出到csv文件

时间:2014-08-13 17:49:33

标签: excel powershell csv powershell-v3.0 powershell-v1.0

我想弄清楚如何导出到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
    }

2 个答案:

答案 0 :(得分:2)

只需将$result传递到ForEach-Object循环中,然后从对象中选择相关属性并导出:

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

您可以使用计算属性将属性替换为自身的修改版本:

$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

答案 1 :(得分:1)

我会创建一个数组,根据您脚本中的内容填充对象,然后您可以循环填写电子表格,也可以使用它导出到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