我有一个powershell脚本,它在活动目录中搜索给定的用户名并返回该用户的属性,下面是脚本:
Param([string]$username)
Import-Module ActiveDirectory
Get-ADUser $username -Properties GivenName, Surname, DisplayName, Enabled, PasswordExpired, Created, LastLogonDate
正如你所看到的,我已经给了它一个我想要返回的属性列表,它成功地返回了它。问题是它还返回了我不想要的其他东西,比如"DistinguishedName"。看看下面我返回的内容:
Created : 30/07/2014 11:55:39
DisplayName : Testing Acount
DistinguishedName : CN=Testing Acount,OU=Disscuss,OU=Users,OU=Company,DC=Company,DC=local
Enabled : True
GivenName : Testing
LastLogonDate : 18/08/2014 12:27:40
Name : Testing Acount
ObjectClass : user
ObjectGUID : d135516b-4c10-41f1-9fa5-4f48bcacc891
PasswordExpired : False
SamAccountName : testingacount
SID : S-1-5-21-1161181520-173477490-3285844792-2188
Surname : Acount
UserPrincipalName : testingacount@company.local
我该如何处理这样的问题,并使它只返回我所声明的属性,或者甚至在它之后过滤并删除不需要的部分。
只需通过管道将Get-ADUser
命令发送到Select-Object
:
Get-ADUser $username | Select-Object GivenName, Surname, DisplayName, Enabled, PasswordExpired, Created, LastLogonDate
这应该只会给你想要的元素。
编辑:Get-ADUser
命令不会返回所需的所有属性。您需要使用-Properties
参数,然后通过管道将其发送到Select-Object
Get-ADUser $username -Properties GivenName, Surname, DisplayName, Enabled, PasswordExpired, Created, LastLogonDate | Select-Object GivenName, Surname, DisplayName, Enabled, PasswordExpired, Created, LastLogonDate
以下是Get-ADUser
函数的technet页面:http://technet.microsoft.com/en-us/library/ee617241.aspx