简体   繁体   中英

Powershell - Get Last Logon

I have a quick script I am trying to reuse from getting computers lastlogon. Due to time constraints I am posting it here for assistance. I am trying to use the display name and even tried using the sam, but no luck.

$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

I get syntax errors. I can manually put in the values so I am thinking it has to do with data types extracted from the array. Suggestions would be appreciated!

SAM ERROR: Get-Aduser : Error parsing query: 'xxx001' Error Message: 'syntax error' at position: '1'. At C:\\bin\\Get-UserLastLogon.ps1:19 char:14 + $results += Get-Aduser -Filter $i.sam -Properties * | select Name, Lastlogondat ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException + FullyQualifiedErrorId : Error parsing query: 'kal001' Error Message: 'syntax error' at posi tion: '1'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

DISPLAYNAME ERROR: Get-Aduser : Property: 'displayname' not found in object of type: 'System.Management.Automation.PSCustomObject'. At C:\\bin\\Get-UserLastLogon.ps1:20 char:17 + $results += Get-Aduser -Filter {displayname -eq $i.displayname} -Properties ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException + FullyQualifiedErrorId : Property: 'displayname' not found in object of type: 'System.Manage ment.Automation.PSCustomObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Thanks for the suggestions. I found a way to get this to work:

# 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

Try one of these:

Get-Aduser -Filter "samaccountname -eq '$i.sam'" -Properties * 

Get-Aduser -Filter "displayname -eq '$i.displayname'" -Properties * 

The samaccountname will be faster, since that's an indexed property.

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM