PowerShell Get -Aduser-使用自定义广告量

发布于 2025-01-23 12:20:04 字数 1657 浏览 1 评论 0原文

我需要做的是将特定的广告用户及其某些属性导出到CSV文件。我需要拥有的一些默认属性,例如namesamacCountNameenabled 和一些自定义的属性: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 技术交流群。

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

发布评论

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

评论(1

夜夜流光相皎洁 2025-01-30 12:20:04

问题在此行中:

$result = foreach ($item in $domain) {
    Get-ADUser -server $item -Properties ... # => not assigned to any variable

然后在此行中:

if (($null -ne $_.employeenumber) -and ($_.employeenumber -notlike '*svc*')) {

由于$ _不存在,因此您正在比较类似的内容:

$null -ne $null -and $null -notlike '*svc*'

它始终为$ $ false。还值得一提的是,这是a foreach loop,不同于 foreach-object ,自动变量 $ _ $ _ >)在这里并不意味着什么。

下一个问题是在使用 select-object 作为语句的开头,没有对其进行管道的对象。

Select-Object Name, SamAccountName, UserPrincipalName, ...

在这种情况下,如果可以用一些 ldap滤波

# employee number is not `$null` AND employee number is not like `*svc*`
-LDAPFilter "(&(employeenumber=*)(!employeenumber=*svc*))"

代码看起来像这样:

$name = Read-Host -Prompt "Please enter the name for output file."
$filename = $name + ".csv" # Consider using `$HOME` here, or an absolute Path
$param = @{
    LDAPFilter = "(&(employeenumber=*)(!employeenumber=*svc*))"
    ResultPageSize = 100
    Properties = @(
        'businesscategory'
        'extensionAttribute4'
        'extensionAttribute9'
        'extensionAttribute13'
        'employeenumber'
    )
}
'DOMAIN1','DOMAIN2','DOMAIN3','DOMAIN4' | ForEach-Object {
    $param['Server'] = $_
    foreach($user in Get-ADUser @param) {
        [pscustomobject]@{
            Name                 = $user.Name
            SamAccountName       = $user.SamAccountName
            UserPrincipalName    = $user.UserPrincipalName
            BusinessCategory     = $user.businesscategory  -join ", "
            extensionAttribute4  = $user.extensionAttribute4 -join ", "
            extensionAttribute9  = $user.extensionAttribute9 -join ", "
            extensionAttribute13 = $user.extensionAttribute13 -join ", "
            DistinguishedName    = $user.DistinguishedName
            employeenumber       = $user.employeenumber
            Enabled              = $user.Enabled
            Domain               = $_ # Adding the Domain of this user here
        }
    }
} | Export-Csv $filename -NoTypeInformation

The problem is in this line:

$result = foreach ($item in $domain) {
    Get-ADUser -server $item -Properties ... # => not assigned to any variable

Then in this line:

if (($null -ne $_.employeenumber) -and ($_.employeenumber -notlike '*svc*')) {

Since $_ doesn't exist, you are comparing something like:

$null -ne $null -and $null -notlike '*svc*'

Which will always be $false. It's also worth mentioning that this is a foreach loop, different from ForEach-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.

Select-Object Name, SamAccountName, UserPrincipalName, ...

In this case, the if condition could be removed completely with some LDAP Filtering:

# employee number is not `$null` AND employee number is not like `*svc*`
-LDAPFilter "(&(employeenumber=*)(!employeenumber=*svc*))"

The code would look like this:

$name = Read-Host -Prompt "Please enter the name for output file."
$filename = $name + ".csv" # Consider using `$HOME` here, or an absolute Path
$param = @{
    LDAPFilter = "(&(employeenumber=*)(!employeenumber=*svc*))"
    ResultPageSize = 100
    Properties = @(
        'businesscategory'
        'extensionAttribute4'
        'extensionAttribute9'
        'extensionAttribute13'
        'employeenumber'
    )
}
'DOMAIN1','DOMAIN2','DOMAIN3','DOMAIN4' | ForEach-Object {
    $param['Server'] = $_
    foreach($user in Get-ADUser @param) {
        [pscustomobject]@{
            Name                 = $user.Name
            SamAccountName       = $user.SamAccountName
            UserPrincipalName    = $user.UserPrincipalName
            BusinessCategory     = $user.businesscategory  -join ", "
            extensionAttribute4  = $user.extensionAttribute4 -join ", "
            extensionAttribute9  = $user.extensionAttribute9 -join ", "
            extensionAttribute13 = $user.extensionAttribute13 -join ", "
            DistinguishedName    = $user.DistinguishedName
            employeenumber       = $user.employeenumber
            Enabled              = $user.Enabled
            Domain               = $_ # Adding the Domain of this user here
        }
    }
} | Export-Csv $filename -NoTypeInformation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文