2014-06-18 50 views
0

我有一个快速的脚本,我试图从获得计算机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 
+0

如果您发布错误信息的话会更快,所以我们不必猜测它是什么。 – mjolinor

回答

0

尝试其中之一:

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

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

的SAM帐户会更快,因为这是一个索引属性。

+0

错误消失了,但我没有收到任何导出到我的csv文件的数据。思考? – user2565554

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 
相关问题