我有一個快速的腳本,我試圖從獲得計算機lastlogon重用。由於時間限制,我在此張貼尋求幫助。我試圖使用顯示名稱,甚至嘗試使用山姆,但沒有運氣。PowerShell的 - 獲取上次登錄
$results = @()
$CompanyUsers = import-csv c:\bin\users.csv
foreach ($i in $CompanyUsers)
{
$results += Get-Aduser -Filter $i.sam -Properties * | select Name, Lastlogondate
#$results += Get-Aduser -Filter {displayname -eq $i.displayname} -Properties * | select Name, Lastlogondate
}
$results | export-csv c:\bin\Userslogon.csv
我得到語法錯誤。我可以手動輸入值,所以我認爲它與從數組中提取的數據類型有關。建議將不勝感激!
SAM錯誤: Get-Aduser:解析查詢時出錯:'xxx001'錯誤信息:'語法錯誤'在位置:'1'。 在C:\ BIN \ GET-UserLastLogon.ps1:19字符:14 + $結果+ = GET-ADUser便有-Filter $ i.sam -Properties * |選擇名稱,Lastlogondat ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:ParserError:(:) [Get-ADUser],ADFilterParsingException + FullyQualifiedErrorId:分析查詢時出錯:'kal001'錯誤消息:'語法錯誤'在posi tion:'1'。,Microsoft.ActiveDirectory.Management.Commands。 GetADUser
DISPLAYNAME錯誤: 獲取-ADUser便有:屬性: '顯示名稱' 中類型的對象未找到: 'System.Management.Automation.PSCustomObject'。 在C:\ bin \ Get-UserLastLogon.ps1:20 char:17 + $ results + = Get-Aduser -Filter {displayname -eq $ i.displayname} -Properties ... + ~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo:InvalidArgument:(:) [Get-ADUser],ArgumentException + FullyQualifiedErrorId:屬性:'displayname'在類型對象中找不到':System.Manage ment.Automation.PSCustomObject 」,Microsoft.ActiveDirectory.Management.Commands.GetADUser
感謝您的建議。我找到了一種方法來得到這個工作:
# Create array of users
$results = @()
$Users = Get-Content C:\bin\fullnames.txt
# Get last logon date
foreach($i in $Users)
{
$results += Get-ADUser -ldapfilter "(displayname=$i)" -Property * | Select-Object -Property name, samaccountname, lastlogondate
}
# Export results to csv file
$results | export-csv c:\bin\logonusers.csv
如果您發佈錯誤信息的話會更快,所以我們不必猜測它是什麼。 – mjolinor