【问题标题】:Powershell Filter for mathing AD Account to Folder用于计算 AD 帐户到文件夹的 Powershell 过滤器
【发布时间】:2021-08-17 19:08:01
【问题描述】:

我正在尝试将 samaccountname 与当前文件夹补丁匹配,如果文件夹补丁是“d:\profile\username”但它的状态不是用户名,它会正常工作 username_S-1-5-21*... .

我使用下面的代码,但是否可以过滤 _S-1.... 后面的所有内容,以便将用户名与 samaccountname 匹配?

我使用下面的代码,任何帮助将不胜感激

[编辑:添加完整脚本]

下面是完整的脚本,所以问题是我在我们的 FXLogic 配置文件文件夹的文件夹中的用户名后面有几个带有 _S-1-5-21* 的用户文件夹,并且需要将 samaccountname 与文件夹(用户名- _S-1-5-21* )

我希望这个解释更清楚,是的,它是 SID 而不是 GUID 总是让他们混淆。

param(
[Parameter(Mandatory=$true)]
$FXLogicFolderPath,
$MoveFolderPath,
$SearchBase,
[string[]]$ExcludePath,
[switch]$FolderSize,
[switch]$MoveDisabled,
[switch]$DisplayAll,
[switch]$UseRobocopy,
[switch]$RegExExclude,
[switch]$CheckFXLogicDirectory)

检查是否找到 FXLogicFolderPath,如果路径不正确则退出并显示警告消息

if (!(Test-Path -LiteralPath $FXLogicFolderPath)){
Write-Warning "FXLogicFolderPath not found: $FXLogicFolderPath"

检查是否找到 MoveFolderPath,如果路径不正确则退出并显示警告消息

if ($MoveFolderPath) {
if (!(Test-Path -LiteralPath $MoveFolderPath)){
    Write-Warning "MoveFolderPath not found: $MoveFolderPath"
    exit
}}
exit

主循环,对于在 FXLogic 文件夹路径下找到的每个文件夹,都会查询 AD 以找到匹配的 samaccountname

$ListOfFolders = Get-ChildItem -LiteralPath "$FXLogicFolderPath" -Force | Where-Object {$_.PSIsContainer}

如果给出了 ExcludePath 参数,则排除文件夹

if ($ExcludePath) {
$ExcludePath | ForEach-Object {
    $CurrentExcludePath = $_
    if ($RegExExclude) {
        $ListOfFolders = $ListOfFolders | Where-Object {$_.FullName -notmatch $CurrentExcludePath}
    } else {
        $ListOfFolders = $ListOfFolders | Where-Object {$_.FullName -ne $CurrentExcludePath}
    }
}}

$ListOfFolders | ForEach-Object {
$CurrentPath = Split-Path -Path $_ -Leaf

构造AD Searcher,如果指定SearchBase参数,则添加SearchRoot属性

    $ADSearcher = New-Object DirectoryServices.DirectorySearcher -Property @{
    Filter = "(samaccountname=$CurrentPath)"
}
if ($SearchBase) {
    $ADSearcher.SearchRoot = [adsi]$SearchBase
}

使用 FullName 路径查找 FXLogicdirectory 属性并将反斜杠替换为 \5C LDAP 转义字符

    if ($CheckFXLogicDirectory) {
    $ADSearcher.Filter = "(FXLogicdirectory=$($_.FullName -replace '\\','\5C')*)"
}

执行AD查询并存储在$ADResult中

$ADResult = $ADSearcher.Findone()

如果找不到匹配的 samaccountname,则执行并显示此代码

    if (!($ADResult)) {
    $HashProps = @{
        'Error' = 'Account does not exist and has a FXLogic folder'
        'FullPath' = $_.FullName
    }
    if ($FolderSize) {
        $HashProps.SizeinBytes = [long](Get-ChildItem -LiteralPath $_.Fullname -Recurse -Force -ErrorAction SilentlyContinue |
            Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -Exp Sum)
        $HashProps.SizeinMegaBytes = "{0:n2}" -f ($HashProps.SizeinBytes/1MB)
    }
    
    if ($MoveFolderPath) {
        $HashProps.DestinationFullPath = Join-Path -Path $MoveFolderPath -ChildPath (Split-Path -Path $_.FullName -Leaf)
        if ($UseRobocopy) {
            robocopy $($HashProps.FullPath) $($HashProps.DestinationFullPath) /E /MOVE /R:2 /W:1 /XJD /XJF | Out-Null
        } else {
            Move-Item -LiteralPath $HashProps.FullPath -Destination $HashProps.DestinationFullPath -Force
        }
    }

输出对象

New-Object -TypeName PSCustomObject -Property $HashProps

如果找到 samaccountname 但帐户被禁用,则会显示此信息

} elseif (([boolean]((-join $ADResult.Properties.useraccountcontrol) -band 2))) {
    $HashProps = @{
        'Error' = 'Account is disabled and has a FXLogic folder'
        'FullPath' = $_.FullName
    }
    if ($FolderSize) {
        $HashProps.SizeinBytes = [long](Get-ChildItem -LiteralPath $_.Fullname -Recurse -Force -ErrorAction SilentlyContinue |
            Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -Exp Sum)
        $HashProps.SizeinMegaBytes = "{0:n2}" -f ($HashProps.SizeinBytes/1MB)
    }

    if ($MoveFolderPath -and $MoveDisabled) {
        $HashProps.DestinationFullPath = Join-Path -Path $MoveFolderPath -ChildPath (Split-Path -Path $_.FullName -Leaf)
        Move-Item -LiteralPath $HashProps.FullPath -Destination $HashProps.DestinationFullPath -Force
    }

输出对象

New-Object -TypeName PSCustomObject -Property $HashProps

如果设置了 -DisplayAll 开关,则会显示具有活动用户帐户的文件夹

} elseif ($ADResult -and $DisplayAll) {
    $HashProps = @{
        'Error' = $null
        'FullPath' = $_.FullName
    }
    if ($FolderSize) {
        $HashProps.SizeinBytes = [long](Get-ChildItem -LiteralPath $_.Fullname -Recurse -Force -ErrorAction SilentlyContinue |
            Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -Exp Sum)
        $HashProps.SizeinMegaBytes = "{0:n2}" -f ($HashProps.SizeinBytes/1MB)
    }

输出对象

    New-Object -TypeName PSCustomObject -Property $HashProps
}}







# Construct AD Searcher, add SearchRoot attribute if SearchBase parameter is specified

$ADSearcher = New-Object DirectoryServices.DirectorySearcher -Property @{
    Filter = "(samaccountname=$CurrentPath)"
}
if ($SearchBase) {
    $ADSearcher.SearchRoot = [adsi]$SearchBase

【问题讨论】:

    标签: powershell


    【解决方案1】:

    S-1-5-21... 部分不是 GUID,它是 SID(主体的S安全性Id实体)。

    您可以使用-replace 运算符删除文件夹名称的那部分:

    $folderName = 'username_S-1-5-21-2855571654-3033049851-1520320983-9328'
    $userName = $folderName -replace '_S-1-5.*$'
    

    之后您可以构建所需的 LDAP 查询过滤器:

    $ADSearcher = New-Object DirectoryServices.DirectorySearcher -Property @{
        Filter = "(samaccountname=$userName)"
    }
    

    【讨论】:

      【解决方案2】:

      不确定您的用户名的来源,但您可以通过在 $CurrentPath 上的正则表达式替换来做一些简单的事情

      $currentPath = 'username_S-1-5-21-2855571654-3033049851-1520320983-9328'
      $currentPath = $currentPath -replace '_.+'
      
      # AD Searcher here
      

      这将替换下划线之后的所有内容,但所有用户名必须采用一致的格式,否则您必须考虑可能遇到的所有可能格式。

      【讨论】:

        猜你喜欢
        • 2017-12-15
        • 2020-06-16
        • 2012-10-07
        • 1970-01-01
        • 1970-01-01
        • 2022-10-24
        • 2021-12-02
        • 2013-01-20
        • 2012-05-08
        相关资源
        最近更新 更多