2016-01-23 50 views
0

我使用这个代码:导出表到两次文件,因为不正确的格式

Function Main 
{ 
    $domain = "LDAP://www.example.com" 
    $outfile = 'C:\Scripts\Tests\usersDump.csv' 
    $properties = "SamAccountName", "lastLogonTimestamp", "AccountExpires", "FirstName", "LastName", "distinguishedName", "employeeNumber", "employeeID", "description", "extensionattribute8", "userAccountControl" 


    Write-Host "Searching AD..." 
    $dn = New-Object System.DirectoryServices.DirectoryEntry($domain) 
    $ds = New-Object System.DirectoryServices.DirectorySearcher($dn) 
    $ds.Filter = '(&(objectCategory=User)(samAccountType:1.2.840.113556.1.4.803:=805306368))' 
    $ds.PageSize=1000 
    $ds.PropertiesToLoad.AddRange($properties) 
    $list = $ds.FindAll() 
    Write-Host "Complete" 


    # The AD results are converted to an array of hashtables. 
    Write-Host "Exporting User Attributes to table..." 
    $table = @() 
    $garbageCounter = 0 
    foreach($item in $list) { 
     $hash = @{} 
     foreach($name in $properties){ 
      if ($item.Properties[$name]) { 
       $hash.$name = $item.Properties[$name][0] 
      } else { 
       $hash.$name = $null 
      } 
     } 
     $table += New-Object PSObject -Property $hash 
     $garbageCounter++ 
     if ($garbageCounter -eq 1000) 
     { 
      [System.GC]::Collect() 
      $garbageCounter = 0 
     } 
    } 
    [System.GC]::Collect() 
    Write-Host "Complete." 

    $listOfBadDateValues = '9223372036854775807', '9223372036854770000', '0' 
    $maxDateValue = '12/31/1600 5:00 PM' 

    Write-Host "fixing table values for `"lastLogonTimestamp`" and `"AccountExpires`"" 
    $tableFixedValues = $table | % { 
     if ($_.lastLogonTimestamp) { 
      $_.lastLogonTimestamp = ([datetime]::FromFileTime($_.lastLogonTimestamp)).ToString('g') 
     }; if (($_.AccountExpires) -and ($listOfBadDateValues -contains $_.AccountExpires)) { 
      $_.AccountExpires = $null 
     } else { 
      if (([datetime]::FromFileTime($_.AccountExpires)).ToString('g') -eq $maxDateValue) { 
       $_.AccountExpires = $null 
      } Else { 
       $_.AccountExpires = ([datetime]::FromFileTime($_.AccountExpires)).ToString('g') 
      } 
     };$_} 
    Write-Host "Complete" 

    Write-Host "Exporting table to csv file $outfile" 
    $tableFixedValues | Export-Csv $outfile –encoding "unicode" -NoTypeInformation -Force 
    Write-Host "Complete" 

    Write-Host "Done." 
} 

Main 

的问题是该文件是这么写的一切都在1列。为了在自己的专栏中,我用这个代码,以使属性...

Write-Host "Converting $outfile to csv file $OutputResultsFile" 
$outFileFixed = 'C:\Scripts\Tests\usersDumpFixed.csv' 
Import-Csv $outfile | Export-Csv $outFileFixed -NoTypeInformation -Force 
Write-Host "Complete" 

有没有一种方法,我可以做到这一点,而不必打开关闭一遍?

回答

1

从Export-csv中删除-Encoding Unicode参数,那么它将不在一列