使用 headers - Get-aduser 将信息记录到 csv 文件

logging info to csv file with headers - Get-aduser

我需要将 samaccountname、主目录路径、日期记录到 csv 文件。 我在互联网上看到了很多东西,但似乎对我不起作用,我不知道为什么?也许 bcoz 它与 get-aduser 或我使用 powershellv2

有关

我写的就是这个(虽然不起作用!)

Get-aduser -filter * -properties homedirectory | foreach {
$a = $_.samaccountname
$b = $_.homedirectory

 $details = @{            
                samaccountname      = $a     
                homedirectory       = $b        
        } 

$results += New-Object PSObject -Property $details  
$path = "C:\log.csv"
$results | export-csv -Path $path -NoTypeInformation

}

需要帮助!

只需这样做:

Get-aduser -filter * -properties homedirectory | Select SamAccountName, HomeDirectory | Export-CSV -Path C:\Log.csv -NoTypeInformation

我不确定我是否完全理解您的情况,但这里有一个与您已经尝试过的方法类似的替代方法。也许会有帮助。

$results = @()
Get-aduser -filter * -properties homedirectory | foreach {
    $a = $_.samaccountname
    $b = $_.homedirectory

     $details = @{            
                samaccountname      = $a     
                homedirectory       = $b        
        } 

    $results += New-Object PSObject -Property $details  
}
$path = "C:\log.csv"
$results | export-csv -Path $path -NoTypeInformation