我是Powershell的新手,所以请注意:)。
我正在尝试从未分配经理的特定OU中导出AD用户,并从结果中排除多个子OU。最后将最终结果导出为CSV。我的代码:
param(
[Parameter(Mandatory=$True)]
[String]$OUPath,
[Parameter(Mandatory=$True)]
# The OUs that you want to exclude from the query
[String[]]$OUPathExclude,
[Parameter(Mandatory=$True)]
# Output Path of the CSV
[String]$FilePath,
[Parameter(Mandatory=$True)]
# Name Of the File
[String]$FileName
)
Get-ADUser -Properties manager -filter * -SearchBase $OUPath |
select Name,
@{label='User Name';Expression='SamAccountName'},
@{Label='Manager Name';Expression={(Get-ADUser -Identity $_.manager | Select-Object -ExpandProperty name)}},
DistinguishedName |
Where-Object {($_.DistinguishedName -notlike '*,'+$OUPathExclude) -AND ($_.'Manager Name' -eq $null)} |
sort -Property name -CaseSensitive
Export-Csv -Path $FilePath\$FileName.csv -NoTypeInformation -Encoding UTF8
因此,当在[String[]]$OUPathExclude参数中删除'[]‘时,它接受我正在排除的一个OU,并且它工作得很好,但是当放回'[]’时,它不接受where-object CmdLet中的多个OU,尽管当输入变量时,您可以看到2个OU被输入到variable中。此外,我尝试了foreach的两个版本,但似乎没有让它工作。
这个工具是为一个技术含量不高的团队设计的,所以我不想通过函数和命令集绑定将它转换成CmdLet,我也不想愚蠢地在脚本中放入多个变量,让它们每次都改变他。
您的帮助将不胜感激!
发布于 2020-10-11 17:37:17
您可以使用正则表达式,方法是构建排除模式,其中每个值由交替(或)操作符|
分隔,并将-notlike
更改为-notmatch
应采用以下格式
'OUDN1|OUDN2|OUDN3'
只需使用-join
语句构建它。只有当有多个值时,它才会添加字符。
$OUPathExclude = $OUPathExclude -join '|'
和
$_.DistinguishedName -notmatch $OUPathExclude
发布于 2020-10-11 20:22:58
如果您过滤掉没有直接填充manager
属性的任何用户,代码可能会更有效率。当然,有更多的方法可以做到这一点。下面的代码使用-LDAPFilter "(manager=*)"
然后,要排除某些OU路径中的用户,您可以构建正则表达式字符串进行检查:
param(
[Parameter(Mandatory=$True)]
[String]$OUPath,
[Parameter(Mandatory=$True)]
# The OUs that you want to exclude from the query
[String[]]$OUPathExclude,
[Parameter(Mandatory=$True)]
# Output Path of the CSV
[String]$FilePath,
[Parameter(Mandatory=$True)]
# Name Of the File
[String]$FileName
)
# build a regex string to match the OU's to exclude
# use [regex]::Escape() to escape all characters that have special meaning in regex
# join the results of that with the regex OR character '|'
# the dollar sign anchors the match to the end of the OU string
$excludeThese = '({0})$' -f (($OUPathExclude | ForEach-Object {[regex]::Escape($_)}) -join '|')
$result = Get-ADUser -Properties manager -LDAPFilter "(manager=*)" -SearchBase $OUPath |
Where-Object { $_.DistinguishedName -notmatch $excludeThese } |
Select-Object Name,
@{label='User Name';Expression= $_.SamAccountName },
@{Label='Manager Name';Expression={(Get-ADUser -Identity $_.manager).Name}},
DistinguishedName |
Sort-Object -Property Name -CaseSensitive
# output on screen
$result | Format-Table -AutoSize
# write to CSV
$result | Export-Csv -Path (Join-Path -Path $FilePath -ChildPath ('{0}.csv' -f $FileName)) -NoTypeInformation -Encoding UTF8
发布于 2020-10-11 20:28:16
因此,我的一个队友帮助了我,它似乎工作得很好。下面是供参考的代码:
param(
[Parameter(Mandatory=$True)]
[String]$OUPath,
[Parameter(Mandatory=$True)]
[String[]]$OUPathExclude,
[Parameter(Mandatory=$True)]
[String]$FilePath,
[Parameter(Mandatory=$True)]
[String]$FileName
)
$script = "
Get-ADUser -Properties manager -filter * -SearchBase '" + [string]$OUPath +"' | select Name,
@{label='User Name';Expression='SamAccountName'},
@{Label='Manager Name';Expression={(Get-ADUser -Identity $_.manager | Select-Object -ExpandProperty name)}},
DistinguishedName |
Where-Object {"
foreach ($ou in $OUPathExclude)
{
$script= $script + ' ($_.DistinguishedName -notlike ''*,''+'''+$ou+''') -AND'
}
$script =$script + ' ($_.''Manager Name'' -eq $null)} |
sort -Property name -CaseSensitive '
$script
Invoke-Expression $script| Export-Csv -Path $FilePath\$FileName.csv -NoTypeInformation -Encoding UTF8
https://stackoverflow.com/questions/64302347
复制相似问题