PowerShell Get -Aduser-使用自定义广告量
我需要做的是将特定的广告用户及其某些属性导出到CSV文件。我需要拥有的一些默认属性,例如name
,samacCountName
,enabled
和一些自定义的属性:businessCategory ,
extensionAttribute9
。
$name = Read-Host -Prompt "Please enter the name for output file."
$filename = $name+".csv"
$domain = @('DOMAIN1','DOMAIN2','DOMAIN3','DOMAIN4')
$result = foreach ($item in $domain) {
Get-ADUser -server $item -Properties businesscategory, extensionAttribute4,
extensionAttribute9, extensionAttribute13, employeenumber, Enabled -ResultPageSize 100 -Filter *
if (($null -ne $_.employeenumber) -and ($_.employeenumber -notlike '*svc*')) {
Select-Object Name,
SamAccountName,
UserPrincipalName,
@{n="businesscategory"; e={$_.businesscategory -join ", "}},
@{n="extensionAttribute4";e={$_.extensionAttribute4 -join ", "}},
@{n="extensionAttribute9";e={$_.extensionAttribute9 -join ", "}},
@{n="extensionAttribute13";e={$_.extensionAttribute13 -join ", "}},
DistinguishedName, employeenumber, Enabled
} else { (...)
等 如果,它应进入第一个。它这样做,但是它出口了所有帐户,无论雇员是否存在。 另一个问题是导出的CSV不包含从自定义属性创建的列,而是显示了我不要求的其他一些属性。
如果我使用where -object
而不是if -else
,则可以正常工作,并检查了以下值:
Where-Object {
($_.SamAccountName -notlike '*proprietary*') -and
($_.UserPrincipalName -notlike '*proprietary*') -and
($_.SamAccountName -notlike '*mailbox*') -and (...)
不幸的是,我需要使用if -else 进行更复杂的比较和选择,但无法弄清楚
What I need to do is to export specific AD users and some of their properties to a CSV file. What I need to have there is some of the default properties like Name
, SamAccountName
, Enabled
and some custom ones: businesscategory
, extensionAttribute9
etc.
I'm struggling with my if - else
statements, as they seem to not be comparing employeenumber to $null
$name = Read-Host -Prompt "Please enter the name for output file."
$filename = $name+".csv"
$domain = @('DOMAIN1','DOMAIN2','DOMAIN3','DOMAIN4')
$result = foreach ($item in $domain) {
Get-ADUser -server $item -Properties businesscategory, extensionAttribute4,
extensionAttribute9, extensionAttribute13, employeenumber, Enabled -ResultPageSize 100 -Filter *
if (($null -ne $_.employeenumber) -and ($_.employeenumber -notlike '*svc*')) {
Select-Object Name,
SamAccountName,
UserPrincipalName,
@{n="businesscategory"; e={$_.businesscategory -join ", "}},
@{n="extensionAttribute4";e={$_.extensionAttribute4 -join ", "}},
@{n="extensionAttribute9";e={$_.extensionAttribute9 -join ", "}},
@{n="extensionAttribute13";e={$_.extensionAttribute13 -join ", "}},
DistinguishedName, employeenumber, Enabled
} else { (...)
The above is part of my code where it should enter into first if
. It does that, but it exports all accounts, whether employeenumber is present or not.
Another issue is that the exported CSV doesn't contain columns created from custom attributes, instead it shows some other properties that I did not ask for.
This used to work fine if I used Where-Object
instead of if - else
and checked the values like below:
Where-Object {
($_.SamAccountName -notlike '*proprietary*') -and
($_.UserPrincipalName -notlike '*proprietary*') -and
($_.SamAccountName -notlike '*mailbox*') -and (...)
Unfortunately I need to use if - else
to make more complex comparisons and selections, but can't figure it out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在此行中:
然后在此行中:
由于
$ _
不存在,因此您正在比较类似的内容:它始终为
$ $ false
。还值得一提的是,这是aforeach
loop,不同于foreach-object
,自动变量
>)$ _
$ _在这里并不意味着什么。
下一个问题是在使用
select-object
作为语句的开头,没有对其进行管道的对象。在这种情况下,
如果
可以用一些 ldap滤波:代码看起来像这样:
The problem is in this line:
Then in this line:
Since
$_
doesn't exist, you are comparing something like:Which will always be
$false
. It's also worth mentioning that this is aforeach
loop, different fromForEach-Object
, the automatic variable$_
($PSItem
) doesn't mean anything here.The next problem comes when using
Select-Object
as the beginning of the statement, there is no object being piped to it.In this case, the
if
condition could be removed completely with some LDAP Filtering:The code would look like this: