PowerShell导出csv / Strange输出

时间:2016-01-08 12:46:07

标签: powershell export-to-csv

我是PowerShell的新手,并不是处理代码和脚本但是尝试学习的最伟大的人。现在来问题了!

我写了一个简短的脚本,列出了具有某些属性的特定OU的所有用户。

Get-ADUser -Filter * -SearchBase “OU=XY,OU=XY,OU=XY,OU=XY,DC=XY,DC=XY” -Properties DisplayName, SAMAccountName, EmailAddress, msDS-UserPasswordExpiryTimeComputed, AccountExpirationDate, Company, StreetAddress, L, PostalCode, co | 
    select DisplayName, SAMAccountName, EmailAddress, @{ Name = "ExpiryDate"; Expression = { [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed") } }, AccountExpirationDate, Company, StreetAddress, L, PostalCode, co | 
    Sort-Object -Property Company | 
    Format-Table -Autosize -Property Company, DisplayName, SAMAccountName, EmailAddress, AccountExpirationDate, ExpiryDate, StreetAddress, L, Postalcode, co

这很好用,在运行PowerShell中的脚本时,一切都正常显示,但是当我尝试将信息导出到csv文件时:

Get-ADUser -Filter * -SearchBase “OU=XY,OU=XYs,OU=XY,OU=XY,DC=XY,DC=XY” -Properties DisplayName, SAMAccountName, EmailAddress, msDS-UserPasswordExpiryTimeComputed, AccountExpirationDate, Company, StreetAddress, L, PostalCode, co | 
    select DisplayName, SAMAccountName, EmailAddress, @{ Name = "ExpiryDate"; Expression = { [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed") } }, AccountExpirationDate, Company, StreetAddress, L, PostalCode, co | 
    Sort-Object -Property Company | 
    Format-Table -Autosize -Property Company, DisplayName, SAMAccountName, EmailAddress, AccountExpirationDate, ExpiryDate, StreetAddress, L, Postalcode, co | 
    Export-Csv -Path c:\xy.csv

我在文件中得到了一些奇怪的字符:

27c87ef9bbda4f709f6b4002fa4af63c,,,,,

但这只发生在为表选择了一定数量的属性/列之后。我只选择4个属性,它工作正常。我错过了什么?

欢迎任何帮助!

2 个答案:

答案 0 :(得分:0)

Format-* cmdlet通常应该是最后一个。

如果您移除代码段中的Format-Table位,则应获得预期结果。

此外,使用-NoTypeInformation时,Export-CSV是您最好的朋友:-) 信用额Matt

答案 1 :(得分:-2)

你能试试out-file命令吗?

Get-ADUser -Filter * -SearchBase “OU=XY,OU=XYs,OU=XY,OU=XY,DC=XY,DC=XY” -Properties DisplayName, SAMAccountName, EmailAddress, msDS-UserPasswordExpiryTimeComputed, AccountExpirationDate, Company, StreetAddress, L, PostalCode, co | select DisplayName, SAMAccountName, EmailAddress, @{ Name = "ExpiryDate"; Expression = { [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed") } }, AccountExpirationDate, Company, StreetAddress, L, PostalCode, co | Sort-Object -Property Company | Format-Table -Autosize -Property Company, DisplayName, SAMAccountName, EmailAddress, AccountExpirationDate, ExpiryDate, StreetAddress, L, Postalcode, co | out-file -filepath c:\xy.csv