0
以下代碼將查詢我的環境中的所有控制器,以確保獲得最新的登錄時間戳。它非常適合爲我工作的奇怪用戶獲取數據。但是,我發現需要評估AD中的800多個用戶。 它在完成之前會吃掉1.5gb,並使內存不足錯誤導致腳本崩潰。在函數完成後有沒有辦法回收內存,或者強制垃圾收集的方法?我曾嘗試刪除變量和其他變體。我試圖強制偶爾的系統垃圾收集無濟於事。獲取多個用戶的上次登錄時間戳
我的猜測是所有的AD查詢,只是不知道如何處理它們,一旦我完成。
對這個函數的調用來自一個foreach循環,這個foreach循環從我在函數調用之前拉出的列表中發送每個用戶。預先感謝我的任何幫助。
的$域變量,只是假裝它是「example.contoso.ca」
function getDateTimeStamp
{
param
(
[string]$userID,
[string]$domain,
[string]$attribute #this is an AD attribute in this case i used "lastlogon"
)
$domainSuffix = '*.'+$domain
$myForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$domaincontrollers = $myForest.Sites | % { $_.Servers } | Select-Object Name | Where-Object Name -like $domainSuffix
$realUserDate = $null
foreach ($domainController in $domainControllers)
{
$userDateTime = Get-ADUser -Identity $userID -Properties $attribute -Server $domainController.Name
$userLastDateTime = $userDateTime.$attribute
if($userLastDateTime -eq $null){$userLastDateTime = 0}
elseif($userLastDateTime -eq 9223372036854775807){$userLastDateTime = 0}
if($userLastDateTime -is 'DateTime'){$tempUserDate = get-date -Date ($userLastDateTime)}
else{$tempUserDate = [DateTime]::FromFileTime([Int64]::Parse($userLastDateTime))}
if ($realUserDate -le $tempUserDate){$realUserDate = $tempUserDate}
}
return $realUserDate
}
見我的答案在這裏,但嘗試的foreach對象 http://superuser.com/questions/675484/why-does-foreach-object-behave-differently-when-called-as-foreach –
我嘗試foreach($物品$ x) ,我試過$ items | foreach-object {} 既沒有在列表中一直工作。 – icomeinpieces
我沒有看到你定義'$ attribute'的地方。如果使用LastLogonDate作爲'$ attribute',則不必將'[Int64]'轉換爲'[DateTime]'。另外,你需要確切的LastLogon,還是可以使用LastLogonTimestamp?這是重複的,並且在我相信的11天內準確無誤。 – TheMadTechnician