Powershell variable export-CSV cache?

柔情痞子 提交于 2019-12-02 08:23:11

The Export-Csv cmdlet does not export strings. It exports the properties of objects as fields of the output CSV file. In case of string objects, the exported property is the length of the string. You need to transform the hashtable into a list of custom objects to make the export work as you expect:

$arrUsers.Keys | % {
  New-Object -Type PSObject -Property @{
    'sAMAccountName' = $_
    'lastLogon'      = $arrUsers[$_]
  }
} | Export-Csv 'C:\test.csv' -NoType

Also the manual date calculation could be replaced with the FromFileTime() method:

$lastLogon = [DateTime]::FromFileTime($Result.Properties.Item("lastLogon"))

With that said, why are you doing this "by hand" in the first place? The ActiveDirectory PowerShell module has a cmdlet Get-ADUser that already does all the work for you:

Import-Module ActiveDirectory

Get-ADUser -Filter * -Properties * | ? {
  $_.LastLogonDate -lt (Get-Date).AddDays(-60)
} | select SamAccountName, LastLogonDate | Export-Csv 'C:\test.csv' -NoType

Why move all the items from $arrUsers to $inactiveusers? You can achieve desired result by adding this at the very end:

$arrUsers.getEnumerator() | Select-Object Key,Value | 
   Export-Csv c:\test.csv  -NoTypeInformation

Find out more about Export-CSV: http://technet.microsoft.com/en-us/library/hh849932.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!