[英]Return manager's samacountname for users
I've got a requirement to create a CSV file of all active users from AD including the line manager attribute, however I need the line managers sAMAccountName
, not the cn
.我需要为来自 AD 的所有活动用户创建一个 CSV 文件,包括直线经理属性,但是我需要直线经理
sAMAccountName
,而不是cn
。 Here is what I have so far:这是我到目前为止所拥有的:
Get-ADUser -server server_ip -Filter { mail -like "*" -and ObjectClass -eq "user" } `
-SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" `
-Properties objectGUID,displayName,office,division,department,employeeNumber,
employeeID,mobilePhone,officePhone,ipphone,title,givenName,surname,mail,
manager,sAMAccountName |
Export-CSV "EU_AD_Properties.csv"
This returns all the data I want, but gives me the line manager's cn
, not the samacountname
.这将返回我想要的所有数据,但给我的是直线经理的
cn
,而不是samacountname
。
Any ideas?有什么想法吗?
I've tried this:我试过这个:
Get-ADUser -server server_ip -Filter { mail -like "*" -and ObjectClass -eq "user" } `
-SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" `
-Properties objectGUID,displayName,office,division,department,employeeNumber,
employeeID,mobilePhone,officePhone,ipphone,title,givenName,surname,mail,
@{Label="Manager";Expression={(Get-aduser -filter {sAMAccountName -eq $_.Manager}.sAMAaccountName)}},
sAMAccountName |
Export-CSV "EU_AD_Properties.csv"
However this errors out.但是,这会出错。
在这里,$user 是您要查询经理信息的用户
(get-aduser (get-aduser $user -Properties manager).manager).samaccountName
You can't create custom properties in arguments to the -Properties
parameter, because the current object variable $_
doesn't contain a value at that point (or at least not the value you want).您不能在
-Properties
参数的参数中创建自定义属性,因为当前对象变量$_
在该点不包含值(或至少不是您想要的值)。 You need to do that in a select
statement later in the pipeline, when $_
actually holds the value you need to process.您需要在管道稍后的
select
语句中执行此操作,此时$_
实际上保存了您需要处理的值。 The way you try to create the custom property won't work either:您尝试创建自定义属性的方式也不起作用:
@{Label="Manager";Expression={(Get-aduser -filter {sAMAccountName -eq $_.Manager}.sAMAaccountName)}}
The filter scriptblock doesn't have an attribute sAMAccountName
.过滤器脚本块没有属性
sAMAccountName
。 What you actually want to do is get the user object for the manager CN and retrieve its sAMAccountName
attribute:您真正想要做的是获取管理器 CN 的用户对象并检索其
sAMAccountName
属性:
@{Label='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}}
Also, you don't need the filter ObjectClass -eq "user"
, because Get-ADUser
will return only user objects anyway.此外,您不需要过滤器
ObjectClass -eq "user"
,因为Get-ADUser
无论如何只会返回用户对象。
So your pipeline should probably look like this:所以你的管道应该是这样的:
Get-ADUser -Server IP -Filter {mail -like "*"} -Properties * `
-SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" |
select objectGUID, displayName, office, division, department, employeeNumber,
employeeID, mobilePhone, officePhone, ipphone, title, givenName, surname,
mail, @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}},
sAMAccountName |
Export-CSV "EU_AD_Properties.csv"
Get-ADUser -Filter * -Properties Name,SamAccountName,AccountExpirationDate,Manager | select Name,SamAccountName,AccountExpirationDate,@{N='Manager';E={(Get-ADUser $_.Manager).sAMAccountName}} | Export-Csv "Userdata.csv"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.