【发布时间】: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