Use LDAP lookup for fixture groups
This commit is contained in:
@@ -369,6 +369,47 @@ function Ensure-SmbShare {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ConvertTo-LdapFilterEscapedValue {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$Value
|
||||||
|
)
|
||||||
|
|
||||||
|
$escapedValue = $Value.Replace('\', '\5c')
|
||||||
|
$escapedValue = $escapedValue.Replace('*', '\2a')
|
||||||
|
$escapedValue = $escapedValue.Replace('(', '\28')
|
||||||
|
$escapedValue = $escapedValue.Replace(')', '\29')
|
||||||
|
$escapedValue = $escapedValue.Replace([string][char]0, '\00')
|
||||||
|
|
||||||
|
return $escapedValue
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-AdGroupBySamAccountName {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$SamAccountName,
|
||||||
|
|
||||||
|
[string[]]$Properties = @()
|
||||||
|
)
|
||||||
|
|
||||||
|
$escapedSamAccountName = ConvertTo-LdapFilterEscapedValue -Value $SamAccountName
|
||||||
|
$parameters = @{
|
||||||
|
LDAPFilter = "(sAMAccountName=$escapedSamAccountName)"
|
||||||
|
ErrorAction = 'Stop'
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Properties.Count -gt 0) {
|
||||||
|
$parameters['Properties'] = $Properties
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return Get-ADGroup @parameters | Select-Object -First 1
|
||||||
|
}
|
||||||
|
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function Ensure-AdGroup {
|
function Ensure-AdGroup {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
@@ -382,7 +423,7 @@ function Ensure-AdGroup {
|
|||||||
[string]$Description
|
[string]$Description
|
||||||
)
|
)
|
||||||
|
|
||||||
$existingGroup = Get-ADGroup -Identity $Name -ErrorAction SilentlyContinue
|
$existingGroup = Get-AdGroupBySamAccountName -SamAccountName $Name
|
||||||
if ($existingGroup) {
|
if ($existingGroup) {
|
||||||
return $existingGroup
|
return $existingGroup
|
||||||
}
|
}
|
||||||
@@ -397,7 +438,7 @@ function Ensure-AdGroup {
|
|||||||
-Description $Description | Out-Null
|
-Description $Description | Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
return Get-ADGroup -Identity $Name -ErrorAction SilentlyContinue
|
return Get-AdGroupBySamAccountName -SamAccountName $Name
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ensure-AdGroupMembership {
|
function Ensure-AdGroupMembership {
|
||||||
@@ -409,8 +450,8 @@ function Ensure-AdGroupMembership {
|
|||||||
[string]$MemberGroup
|
[string]$MemberGroup
|
||||||
)
|
)
|
||||||
|
|
||||||
$parent = Get-ADGroup -Identity $ParentGroup -Properties member -ErrorAction SilentlyContinue
|
$parent = Get-AdGroupBySamAccountName -SamAccountName $ParentGroup -Properties @('member')
|
||||||
$member = Get-ADGroup -Identity $MemberGroup -ErrorAction SilentlyContinue
|
$member = Get-AdGroupBySamAccountName -SamAccountName $MemberGroup
|
||||||
if (-not $parent -or -not $member) {
|
if (-not $parent -or -not $member) {
|
||||||
if ($WhatIfPreference) {
|
if ($WhatIfPreference) {
|
||||||
if ($PSCmdlet.ShouldProcess($ParentGroup, "Add member group '$MemberGroup'")) {
|
if ($PSCmdlet.ShouldProcess($ParentGroup, "Add member group '$MemberGroup'")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user