如何使用 PowerShell 检索 AD 组中每个人的电子邮件别名和 CN

发布于 2024-11-05 06:43:14 字数 894 浏览 0 评论 0原文

我将 PowerShell 与 Quest AD cmdlet 结合使用。

我可以使用 Get-QADGroupMember cmdlet 来获取给定组中每个人的列表。到目前为止一切顺利,但我也想得到他们的电子邮件别名。目前返回的所有内容都是这样的:

Name      Type  DN
----      ----  --
Jane Doe  User  CN=Jane Doe,OU=Employee,DC=companyname,DC=com
Job Blow  User  CN=Joe Blow,OU=Employee,DC=companyname,DC=com

我尝试使用 get-qaduser 和 -includeallproperties 标志,但我仍然只返回上述字段,并且我不知道如何获取返回的数据,文档说这些数据缓存在电脑。

任何帮助将不胜感激。

更新

我最终使用了类似于下面的“select”:

$everyone = Get-QADGroupMember "All employees" | select firstname, lastname, email

这将我需要的所有内容都放入了哈希表数组中。那时,通过使用如下代码迭代每个人,可以很容易地完成所需的任何操作:

for ($i=0; $i -le $everyone .length-1; $i++)
{
    write-host $everyone[$i].email
}

花了我很长时间才找到“。”用于从哈希表中提取特定值的符号。我进行了文本解析,效果很好,但我知道这不是正确的方法,并最终找到了有关点表示法的文档。我希望在这里记录下来可以节省其他人的时间!

I am using PowerShell with the Quest AD cmdlets.

I can use the Get-QADGroupMember cmdlet to get a list of everyone in a given group. So far so good but I would like to get their email alias as well. All that is returned currently is something like:

Name      Type  DN
----      ----  --
Jane Doe  User  CN=Jane Doe,OU=Employee,DC=companyname,DC=com
Job Blow  User  CN=Joe Blow,OU=Employee,DC=companyname,DC=com

I tried using get-qaduser with the -includeallproperties flag but I still only get the above fields returned and I don't know how to get at the returned data which the documentation says is cached on the computer.

Any help would be appreciated.

UPDATE

I ended up using "select" similar to below:

$everyone = Get-QADGroupMember "All employees" | select firstname, lastname, email

And that got everything I needed into an array of hashtables. At that point it is easy to do whatever is needed by iterating through everyone with code like:

for ($i=0; $i -le $everyone .length-1; $i++)
{
    write-host $everyone[$i].email
}

Took me forever to find the "." notation for pulling specific values out of the hashtable. I did text parsing and that worked but I knew that couldn't be the right way of doing it and eventually found documentation on the dot notation. I hope documenting that here saves someone else some time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-11-12 06:43:14

您确定它真的不返回该信息吗?您是否尝试过将命令通过管道传输到 Get-MemberFormat-List -Force * 中? PowerShell 可以配置为仅显示项目的一些属性,而不是此处可能出现的所有情况。

您可以使用 Select-Object 选择属性,或者如果您知道它们在那里,则只需 select 选择属性,即使 PowerShell 默认情况下不显示它们:

Some-Command | select Name, Type, DN, SomeOtherProperty

例如,您可以使用Get-ChildItem 也是如此:

PS Home:\> gci *.ps1


    Directory: C:\Users\Joey


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2011-04-27     18:50        169 format.ps1
-a---        2011-04-26     18:36       1064 Untitled1.ps1
-a---        2011-04-27     18:41         69 x.ps1
-a---        2011-04-23     19:58         91 y.ps1

正常调用仅产生四个属性:ModeLastWriteTimeLength名称。然而,正如 Get-Member 所示,还有更多。

PS Home:\> gci *.ps1|gm -MemberType Property


   TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   System.DateTime CreationTime {get;set;}
CreationTimeUtc   Property   System.DateTime CreationTimeUtc {get;set;}
Directory         Property   System.IO.DirectoryInfo Directory {get;}
DirectoryName     Property   System.String DirectoryName {get;}
Exists            Property   System.Boolean Exists {get;}
Extension         Property   System.String Extension {get;}
FullName          Property   System.String FullName {get;}
IsReadOnly        Property   System.Boolean IsReadOnly {get;set;}
LastAccessTime    Property   System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc Property   System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   System.DateTime LastWriteTimeUtc {get;set;}
Length            Property   System.Int64 Length {get;}
Name              Property   System.String Name {get;}

Are you sure it really doesn't return that information? Have you tried piping the command into Get-Member or Format-List -Force *? PowerShell can be configured to only show a few properties of items and not all which might be the case here.

You can select properties using Select-Object or just select if you konw they are there, even though PowerShell doesn't display them by default:

Some-Command | select Name, Type, DN, SomeOtherProperty

You can see this for example with Get-ChildItem too:

PS Home:\> gci *.ps1


    Directory: C:\Users\Joey


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2011-04-27     18:50        169 format.ps1
-a---        2011-04-26     18:36       1064 Untitled1.ps1
-a---        2011-04-27     18:41         69 x.ps1
-a---        2011-04-23     19:58         91 y.ps1

The normal invocation only yields four properties: Mode, LastWriteTime, Length and Name. However, there are plenty more, as Get-Member shows.

PS Home:\> gci *.ps1|gm -MemberType Property


   TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   System.DateTime CreationTime {get;set;}
CreationTimeUtc   Property   System.DateTime CreationTimeUtc {get;set;}
Directory         Property   System.IO.DirectoryInfo Directory {get;}
DirectoryName     Property   System.String DirectoryName {get;}
Exists            Property   System.Boolean Exists {get;}
Extension         Property   System.String Extension {get;}
FullName          Property   System.String FullName {get;}
IsReadOnly        Property   System.Boolean IsReadOnly {get;set;}
LastAccessTime    Property   System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc Property   System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   System.DateTime LastWriteTimeUtc {get;set;}
Length            Property   System.Int64 Length {get;}
Name              Property   System.String Name {get;}
美人迟暮 2024-11-12 06:43:14

请记住,选择对象会剥离对象并生成新对象。

因此,在此示例中:

$test = get-qaduser atestuser | select-object name

$test 将是仅包含名称的 PSCustomObject (System.Object)。

您想用这些数据做什么?输出到控制台...到文件?

我会做这样的事情:

get-qadgroupmember "domain users" | format-table name, displayname, email

或者

get-qadgroupmember "domain users" | select-object name, displayname, email | Export-Csv c:\acsvfile.csv

Rememember that select-object strips down the object and generates new ones.

So in this example:

$test = get-qaduser atestuser | select-object name

$test will be a PSCustomObject (System.Object) containing only the name.

What do you want do do with the data? Output to the console...to a file?

I would do something like this:

get-qadgroupmember "domain users" | format-table name, displayname, email

Or

get-qadgroupmember "domain users" | select-object name, displayname, email | Export-Csv c:\acsvfile.csv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文