$Session: variable refresh site

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
                }
            }
        }
    }
}

I guess you mean you want to hide the New-UDDynamic -Id ‘RecordType’ when you refresh the page and now $Session:SelectedDNSRecordType is theoretically null. When testing in my environment refreshing the page does not reset $Session:SelectedDNSRecordType. Not sure if this is expected behavior or not. If you are using New-UDPage for this you can have a script block to execute when the page loads. I did a quick test and it seems to work:

$onPageLoad = {$Session:SelectedDNSRecordType = $null}

New-UDPage -Name "DNS Test" -Content {
    $PermittedDomains = @("dnssample.com")
    $Zones = @("zone1","zone2")

    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 ($Session:SelectedDNSRecordType -notin $null,'') {

            show-udtoast -message "Session:SelectedDNSRecordType is $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
                    }
                }
            }
        }
        else {
            Set-UDElement -Id 'RecordType' -Properties @{
                attributes = @{
                    style = @{ 'display' = 'none' }
                }
                
            }
        }
    }
} -LoadNavigation $onPageLoad

Hi,

Well based on the documentation, it makes sense that the $Session variable is still active on a page reload - but i cant find another way, to actually do it.

I tried the code you provided (thanks for the time), but now the dashboard is not loading at all - its just all black.

Looks like you want to try the $Page: scope.