Product: PowerShell Universal
Version: 3.7.9
With reference to this post - Hide New-UDGrid - #3 by ksl
Im building a PoC for managing DNS records using a PSU dashboard, but im struggling with the $Session variables - like in this topic New-UDButton OnClick variables
If i use replace $Session:SelectedDNSRecordType with $SelectedDNSRecordType, my New-UDDynamic dont seem to work.
But the $Session: variable is (as indicated by the name) used throughout the entire session - meaning that my if statement on line 45 is loaded, if i refresh the browser.
What is the best way here?
$PermittedDomains = @("dnssample.com")
$Zones = $(Get-DnsServerZone -ComputerName $(Get-ADDomain).RIDMaster | Where-Object { $_.ZoneName -in $PermittedDomains }).ZoneName
New-UDGrid -Container -Content {
New-UDGrid -item -SmallSize 6 -Content {
New-UDCard -Title 'Select DNS zone' -Content {
New-UDStack -Direction 'column' -Content {
New-UDSelect -Id SelectedDNSZone -Option {
foreach ($DNSZone in $Zones) {
New-UDSelectOption -Name $DNSZone -Value $DNSZone
}
} -OnChange {
Show-UDToast -Message $EventData
Sync-UDElement -Id 'table'
$Session:ZoneName = $EventData
}
}
}
}
}
New-UDDynamic -Id 'table' -Content {
$SelectedDNSZone = (Get-UDElement -Id SelectedDNSZone).Value
if ($null -ne $SelectedDNSZone) {
New-UDGrid -Container -Content {
New-UDGrid -Item -SmallSize 6 -content {
New-UDCard -Title "Select record type" -Content {
New-UDStack -Direction column -content {
New-UDSelect -Id "RecordType" -Label "Type" -Option {
New-UDSelectOption -Name 'A' -Value A
New-UDSelectOption -Name 'CNAME' -Value CNAME
} -OnChange {
Sync-UDElement -Id 'RecordType'
$Session:SelectedDNSRecordType = $eventdata[0]
}
}
}
}
}
}
}
New-UDDynamic -Id 'RecordType' -Content {
if ($null -ne $Session:SelectedDNSRecordType) {
New-UDGrid -Container -Content {
New-UDGrid -Item -SmallSize 6 -content {
New-UDCard -Title "Enter new DNS record" -Content {
if ($Session:SelectedDNSRecordType -eq "A") {
New-UDStack -Direction column -content {
New-UDTextbox -Id "Name" -Label "Name" -Placeholder "Server01"
New-UDTextbox -Id "RecordValue" -Label "Value" -Placeholder "10.20.30.40"
}
}
if ($Session:SelectedDNSRecordType -eq "CNAME") {
New-UDStack -Direction column -content {
New-UDTextbox -Id "Name" -Label "Name" -Placeholder "Server01"
New-UDTextbox -Id "HostNameAlias" -Label "HostNameAlias" -Placeholder "Server01.contoso.com"
}
}
}
New-UDButton -ShowLoading -Text "Add record" -OnClick {
Show-UDToast -Message "Adding the new record"
if ($Session:SelectedDNSRecordType -eq "A") {
$RecordName = (Get-UDElement -Id "Name").Value
$RecordValue = (Get-UDElement -Id "RecordValue").value
$DNSZone = $Session:ZoneName
try {
Add-DnsServerResourceRecordA -Name $RecordName -IPv4Address $RecordValue -ZoneName $DNSZone -ComputerName $(Get-ADDomain).RIDMaster -ErrorAction stop
Show-UDToast -Message "Added the new record $recordname" -MessageColor green -Persistent -CloseOnClick -CloseOnEscape
}
catch {
throw "Failed to add the record $recordname - $($_.Exception.Message)"
}
}
if ($Session:SelectedDNSRecordType -eq "CNAME") {
$RecordName = (Get-UDElement -Id "Name").Value
$RecordAlias = (Get-UDElement -Id "HostNameAlias").value
$DNSZone = $Session:ZoneName
try {
Add-DnsServerResourceRecordCName -Name $RecordName -HostNameAlias $RecordAlias -ZoneName $DNSZone -ComputerName $(Get-ADDomain).RIDMaster -ErrorAction stop
Show-UDToast -Message "Added the new record $recordname" -MessageColor green -Persistent -CloseOnClick -CloseOnEscape
}
catch {
throw "Failed to add the record $recordname - $($_.Exception.Message)"
}
}
Start-Sleep -Seconds 2
}
}
}
}
}