Powershell - 获取上次登录

loading...


0

我有一个快速的脚本,我试图重新使用计算机lastlogon . 由于时间限制,我将其张贴在这里寻求帮助 . 我试图使用显示名称,甚至尝试使用山姆,但没有运气 .

$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 char:14 $ results = Get-Aduser -Filter $ i.sam -Properties * |选择姓名,Lastlogondat ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (:) [Get-ADUser],ADFilterParsingException FullyQualifiedErrorId:解析查询时出错:'kal001'错误消息:位于'1'的'语法错误',Microsoft.ActiveDirectory.Management.Commands.GetADUser

DISPLAYNAME错误:Get-Aduser:属性:在类型为'System.Management.Automation.PSCustomObject'的对象中找不到'displayname' . 在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 type of object:'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
2回答

  • 0

    尝试以下方法之一:

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

    samaccountname会更快,因为这是一个索引属性 .


  • 0
    # 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
    

loading...

评论

暂时没有评论!