Fix switch regex match;Add GPO link debugging

This commit is contained in:
Danny Bessems 2021-03-24 11:19:27 +01:00
parent a0956209de
commit b02dcfa717
2 changed files with 47 additions and 30 deletions

View File

@ -15,13 +15,15 @@ If (@('primary','standalone') -contains $Parameter['deployment.type']) {
} }
$PSSession = New-PSSession @NewPSSessionSplat $PSSession = New-PSSession @NewPSSessionSplat
$ParseErrors = @()
$GetItemSplat = @{ $GetItemSplat = @{
Path = "$($PSScriptRoot)\$($MyInvocation.MyCommand)".Replace('.ps1', '.*.yml') Path = "$($PSScriptRoot)\$($MyInvocation.MyCommand)".Replace('.ps1', '.*.yml')
} }
Get-Item @GetItemSplat | ForEach-Object { ForEach ($File in (Get-Item @GetItemSplat)) {
Write-Host "Loading/parsing file '$($_)' ..." Try {
Write-Host "Loading/parsing file '$($File)' ..."
$GetContentSplat = @{ $GetContentSplat = @{
Path = $_ Path = $File
Raw = $True Raw = $True
} }
$RawContent = Get-Content @GetContentSplat $RawContent = Get-Content @GetContentSplat
@ -30,9 +32,15 @@ If (@('primary','standalone') -contains $Parameter['deployment.type']) {
AllDocuments = $True AllDocuments = $True
} }
$YamlDocuments = ConvertFrom-Yaml @ConvertFromYamlSplat $YamlDocuments = ConvertFrom-Yaml @ConvertFromYamlSplat
}
Catch {
$ParseErrors += "While processing '$($File)': $($_.Exception.Message)"
Continue
}
# Check if the respective .yml file declared substitutions which need to be parsed # Check if the respective .yml file declared substitutions which need to be parsed
If (($YamlDocuments.Count -gt 1) -and $YamlDocuments[-1].Variables) { If (($YamlDocuments.Count -gt 1) -and $YamlDocuments[-1].Variables) {
Try {
ForEach ($Pattern in $YamlDocuments[-1].Variables) { ForEach ($Pattern in $YamlDocuments[-1].Variables) {
$RawContent = $RawContent -replace "\{\{ ($($Pattern.Name)) \}\}", [string](Invoke-Expression -Command $Pattern.Expression) $RawContent = $RawContent -replace "\{\{ ($($Pattern.Name)) \}\}", [string](Invoke-Expression -Command $Pattern.Expression)
} }
@ -42,6 +50,12 @@ If (@('primary','standalone') -contains $Parameter['deployment.type']) {
AllDocuments = $True AllDocuments = $True
} }
$YamlDocuments = ConvertFrom-Yaml @ConvertFromYamlSplat $YamlDocuments = ConvertFrom-Yaml @ConvertFromYamlSplat
}
Catch {
$ParseErrors += "While processing '$($File)' (after substitutions): $($_.Exception.Message)"
Continue
}
$GroupPolicies = $YamlDocuments[0..($YamlDocuments.Count - 2)] $GroupPolicies = $YamlDocuments[0..($YamlDocuments.Count - 2)]
} }
Else { Else {
@ -160,19 +174,22 @@ If (@('primary','standalone') -contains $Parameter['deployment.type']) {
} }
ForEach ($OU in $GroupPolicy.LinkedOUs) { ForEach ($OU in $GroupPolicy.LinkedOUs) {
If (Test-Path "AD:\$($OU + ',DC=' + $Parameter['addsconfig.domainname'].Replace('.', ',DC='))") { If (Test-Path "AD:\$($OU + (',{0}' -f (Get-ADRootDSE).rootDomainNamingContext))") {
Write-Host "Linking policy '$($NewGPO.DisplayName)' to OU '$($OU)' ..." Write-Host "Linking policy '$($NewGPO.DisplayName)' to OU '$($OU)' ..."
$NewGPLinkSplat = @{ $NewGPLinkSplat = @{
Name = $NewGPO.DisplayName Name = $NewGPO.DisplayName
Target = $OU + ',DC=' + $Parameter['addsconfig.domainname'].Replace('.', ',DC=') Target = $OU + (',{0}' -f (Get-ADRootDSE).rootDomainNamingContext)
# ErrorAction = 'SilentlyContinue' # ErrorAction = 'SilentlyContinue'
} }
New-GPLink @NewGPLinkSplat | Out-Null New-GPLink @NewGPLinkSplat | Out-Null
} }
Else { Else {
Throw "Path not accessible: 'AD:\$($OU + ',DC=' + $Parameter['addsconfig.domainname'].Replace('.', ',DC='))'" Throw "Path not accessible: 'AD:\$($OU + (',{0}' -f (Get-ADRootDSE).rootDomainNamingContext))"
} }
} }
} }
} }
If ($ParseErrors) {
Throw "One or more errors occurred:`n$($ParseErrors -join "`n")"
}
} }

View File

@ -128,13 +128,13 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
$XMLPropertyAttrKey.Value = $Property.Key $XMLPropertyAttrKey.Value = $Property.Key
$XMLPropertyAttrType = $XML.CreateAttribute('type', $XML.DocumentElement.ovf) $XMLPropertyAttrType = $XML.CreateAttribute('type', $XML.DocumentElement.ovf)
Switch -regex ($Property.Type) { Switch -regex ($Property.Type) {
'boolean' { '^boolean' {
$XMLPropertyAttrType.Value = 'boolean' $XMLPropertyAttrType.Value = 'boolean'
} }
'int' { '^int' {
$XMLPropertyAttrType.Value = 'uint8' $XMLPropertyAttrType.Value = 'uint8'
$Qualifiers = @() $Qualifiers = @()
If ($Property.Type -match 'int\((\d*)\.\.(\d*)\)') { If ($Property.Type -match '^int\((\d*)\.\.(\d*)\)') {
If ($Matches[1]) { If ($Matches[1]) {
$Qualifiers += "MinValue($($Matches[1]))" $Qualifiers += "MinValue($($Matches[1]))"
} }
@ -146,20 +146,20 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers) [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
} }
} }
'ip' { '^ip' {
$XMLPropertyAttrType.Value = 'string' $XMLPropertyAttrType.Value = 'string'
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.vmw) $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.vmw)
$XMLPropertyAttrQualifiers.Value = 'Ip' $XMLPropertyAttrQualifiers.Value = 'Ip'
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers) [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
} }
'password' { '^password' {
$XMLPropertyAttrType.Value = 'string' $XMLPropertyAttrType.Value = 'string'
$XMLPropertyAttrPassword = $XML.CreateAttribute('password', $XML.DocumentElement.ovf) $XMLPropertyAttrPassword = $XML.CreateAttribute('password', $XML.DocumentElement.ovf)
$XMLPropertyAttrPassword.Value = 'true' $XMLPropertyAttrPassword.Value = 'true'
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrPassword) [void]$XMLProperty.Attributes.Append($XMLPropertyAttrPassword)
$Qualifiers = @() $Qualifiers = @()
If ($Property.Type -match 'password\((\d*)\.\.(\d*)\)') { If ($Property.Type -match '^password\((\d*)\.\.(\d*)\)') {
If ($Matches[1]) { If ($Matches[1]) {
$Qualifiers += "MinLen($($Matches[1]))" $Qualifiers += "MinLen($($Matches[1]))"
} }
@ -171,10 +171,10 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers) [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
} }
} }
'string' { '^string' {
$XMLPropertyAttrType.Value = 'string' $XMLPropertyAttrType.Value = 'string'
$Qualifiers = @() $Qualifiers = @()
If ($Property.Type -match 'string\((\d*)\.\.(\d*)\)') { If ($Property.Type -match '^string\((\d*)\.\.(\d*)\)') {
If ($Matches[1]) { If ($Matches[1]) {
$Qualifiers += "MinLen($($Matches[1]))" $Qualifiers += "MinLen($($Matches[1]))"
} }
@ -184,7 +184,7 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf) $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
$XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' ' $XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' '
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers) [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
} ElseIf ($Property.Type -match 'string\[(.*)\]') { } ElseIf ($Property.Type -match '^string\[(.*)\]') {
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf) $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
$XMLPropertyAttrQualifiers.Value = "ValueMap{$($Matches[1] -replace '","', '", "')}" $XMLPropertyAttrQualifiers.Value = "ValueMap{$($Matches[1] -replace '","', '", "')}"
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers) [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)