我有一个 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 : <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aadecfd9dec3c4cdcbc9c5dfc4deeac9c5c7dacbc4d384c6c5c9cbc6" rel="noreferrer noopener nofollow">[email protected]</a>
如何解决这样的问题并使其仅返回我所声明的属性,或者甚至可能在删除不需要的位后对其进行过滤。
最佳答案
只需将 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
关于PHP | Powershell脚本输出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25552716/