Powershell使用.net System.DirectoryServices.DirectorySearcher获取lastLogonTimestamp值

时间:2016-01-22 20:29:09

标签: powershell powershell-v3.0

在我使用Get-QADUser cmdlet之前。然而,当试图超过20万用户时,存在内存泄漏,并且花了2天才能获得所有用户。我在这里找到了一个更快的方法:https://blog.schmijos.ch/2013/09/27/ad-export-with-get-qaduser-is-too-slow/

这是使用System.DirectoryServices.DirectorySearcher .net类。我遇到的问题是,Get-QADUser正在提取它们的值中没有提取几个时间戳属性。

例如......

用户属性lastLogonTimestamp Get-QADUser = 10/15/2014 7:27:03 AM System.DirectoryServices.DirectorySearcher = 130578568233067000

用户财产AccountExpires Get-QADUser = 3/29/2008 9:00:00 PM System.DirectoryServices.DirectorySearcher = 128513232000000000

使用System.DirectoryServices.DirectorySearcher,无论如何,我可以将结果设为date而不是some really big number

Get-QADUser代码:

Get-QADUser -sizelimit 0 -LdapFilter "(&(objectCategory=User)(samAccountType:1.2.840.113556.1.4.803:=805306368))" -DontUseDefaultIncludedProperties -IncludedProperties SamAccountName,lastLogonTimestamp,AccountExpires,FirstName,LastName,distinguishedName,employeeNumber,employeeID,description,extensionattribute8,userAccountControl | 
select SamAccountName,lastLogonTimestamp,AccountExpires,FirstName,LastName,distinguishedName,employeeNumber,employeeID,description,extensionattribute8,userAccountControl |
Export-Csv $csvFileWithPath -NoTypeInformation

System.DirectoryServices.DirectorySearcher代码

$domain = "LDAP://www.example.com"
$outfile = 'C:\Scripts\Tests\test1.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()

# The AD results are converted to an array of hashtables.
Write-Host "Exporting" $list.Count "Records..."
$table = @()
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
}
$table | Export-Csv $outfile –encoding "unicode" -NoTypeInformation

$inputResultsFile = $outfile
$OutputResultsFile = 'C:\Scripts\Tests\testResults.csv'

Import-Csv $outfile | Export-Csv $OutputResultsFile -NoTypeInformation

Write-Host "Done."

2 个答案:

答案 0 :(得分:0)

我能够使用以下内容(在两种情况下)转换上述两者之间的日期:

(get-date 130578568233067000).AddHours(14025305)

因此,如果你这样做 - 采用.NET方法为你提供的数字,让Get-Date将其转换为数字,然后添加应该工作的小时数。

答案 1 :(得分:0)

使用DateTimeFromFileTime静态方法转换lastLogonTimestamp; e.g:

Get-ADUser username -properties lastLogonTimestamp |
  Select-Object DistinguishedName,
  @{Name="lastLogonTimestamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}