Hide New-UDGrid

Product: PowerShell Universal
Version: 3.7.9

Hi,

Im building a PoC for managing DNS records using a PSU dashboard, but im facing 2 minor issues.
The first one:
The bottom UD grid, should only appear once the top selection have been done. I understand that the code dosent work and it shouldnt, but i cant figure out how to make it work - any suggestions?

The second one:
On line 35 of the script, i use the -Property Recordata - but it actually have to be RecordData.IPv4Address.IPAddressToString , since the value is a subproperty.

I tried different methods (manipulating the members of the array, inserting it into another array, etc) - but none of it works - any idea?

$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 -Option {
                    foreach ($DNSZone in $Zones) {
                        New-UDSelectOption -Name $DNSZone -Value $DNSZone
                    }
                } -OnChange { 
                    Show-UDToast -Message $EventData
                    Sync-UDElement -Id 'table'
                    $Session:ZoneName = $EventData
                }

                
                
            }
        }
    }
}
New-UDGrid -Container -Content {
    New-UDGrid -item -SmallSize 6 -Content {
        New-UDCard -Title "Select record in $($Session:ZoneName)" -Content {
            New-UDStack -Direction 'column' -Content {
                New-UDDynamic -Id 'table' -Content {
                    New-UDButton -Text 'Refresh' -OnClick {
                        Sync-UDElement -Id 'table'
                    } -Icon (New-UDIcon -Icon 'Sync') -Color info
                    
                    $ZoneData = Get-DnsServerResourceRecord -ZoneName $Session:ZoneName -ComputerName $(Get-ADDomain).RIDMaster | Where-Object { $_.RecordType -eq "A" -or $_.RecordType -eq "CNAME" } | Select-Object Hostname, RecordType, RecordData
                    $Columns = @(
                        New-UDTableColumn -Property Hostname -Title "Hostname" -Filter
                        New-UDTableColumn -Property RecordData -Title "Record Data" -Filter
                        New-UDTableColumn -Property RecordType -Title "Record Type" -Filter
                    )
                    New-UDTable -Id 'DNSRecordSelection' -Data $ZoneData -Columns $Columns -ShowPagination -PageSize 15 -ShowExport -ShowSort -ShowSelection
                    
                    New-UDButton -Text "Delete record" -OnClick {
                        $Value = Get-UDElement -Id "DNSRecordSelection"
                        Show-UDToast -Message "Deleting $($Value.SelectedRows.Hostname)" -Duration 3000
                        Try {
                            Remove-DnsServerResourceRecord -ZoneName $Session:ZoneName -Name $Value.SelectedRows.Hostname -ComputerName $(Get-ADDomain).RIDMaster -RRType $Value.SelectedRows.RecordType -ErrorAction stop -Confirm:$false -force
                            Show-UDToast -Message "Deleted  $($Value.SelectedRows.Hostname)" -Duration 3000 -MessageColor green -Persistent
                        }
                        catch {
                            Show-UDToast -Message "Failed to delete $($Value.SelectedRows.Hostname) - $($_.Exception.Message)" -Duration 3000 -Persistent
                        }
                    } -ShowLoading
                }
            }
        }
    }
}

Hi @ksl ,

in the UDDynamic you can check if there was a selection made and only then load the content like

New-UDDynamic -Id 'table' -Content {
    $SelectedDNSZone = (Get-UDElement -Id SelectedDNSZone).Value
    if($SelectedDNSZone -notin $null,''){
        #do stuff
    }
}

Every time the raw data is not in the state i want it for the UDTable i build a little helper object

$DnsServerResourceRecord = Get-DnsServerResourceRecord -ZoneName $Session:ZoneName -ComputerName $(Get-ADDomain).RIDMaster | Where-Object { $_.RecordType -eq "A" -or $_.RecordType -eq "CNAME" } | Select-Object Hostname, RecordType, RecordData
$ZoneData = $DnsServerResourceRecord | foreach{
    [pscustomobject]@{
        Hostname = $_.HostName
        RecordData = $_.RecordData.IPv4Address.IPAddressToString 
        RecordType = $_.RecordType
    }
}

Final code


$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'
                }
            }
        }
    }
}
New-UDDynamic -Id 'table' -Content {
    $SelectedDNSZone = (Get-UDElement -Id SelectedDNSZone).Value
    if($SelectedDNSZone -notin $null,''){
        New-UDGrid -Container -Content {
            New-UDGrid -item -SmallSize 6 -Content {
                New-UDCard -Title "Select record in $($SelectedDNSZone)" -Content {
                    New-UDStack -Direction 'column' -Content {
                        
                        New-UDButton -Text 'Refresh' -OnClick {
                            Sync-UDElement -Id 'table'
                        } -Icon (New-UDIcon -Icon 'Sync') -Color info
                        
                        
                        $DnsServerResourceRecord = Get-DnsServerResourceRecord -ZoneName $Session:ZoneName -ComputerName $(Get-ADDomain).RIDMaster | Where-Object { $_.RecordType -eq "A" -or $_.RecordType -eq "CNAME" } | Select-Object Hostname, RecordType, RecordData
                        $ZoneData = $DnsServerResourceRecord | foreach{
                            [pscustomobject]@{
                                Hostname = $_.HostName
                                RecordData = $_.RecordData.IPv4Address.IPAddressToString 
                                RecordType = $_.RecordType
                            }
                        }

                        $Columns = @(
                            New-UDTableColumn -Property Hostname -Title "Hostname" -Filter
                            New-UDTableColumn -Property RecordData -Title "Record Data" -Filter
                            New-UDTableColumn -Property RecordType -Title "Record Type" -Filter
                        )
                        New-UDTable -Id 'DNSRecordSelection' -Data $ZoneData -Columns $Columns -ShowPagination -PageSize 15 -ShowExport -ShowSort -ShowSelection
                        
                        New-UDButton -Text "Delete record" -OnClick {
                            $Value = Get-UDElement -Id "DNSRecordSelection"
                            Show-UDToast -Message "Deleting $($Value.SelectedRows.Hostname)" -Duration 3000
                            Try {
                                Remove-DnsServerResourceRecord -ZoneName $Session:ZoneName -Name $Value.SelectedRows.Hostname -ComputerName $(Get-ADDomain).RIDMaster -RRType $Value.SelectedRows.RecordType -ErrorAction stop -Confirm:$false -force
                                Show-UDToast -Message "Deleted  $($Value.SelectedRows.Hostname)" -Duration 3000 -MessageColor green -Persistent
                            }
                            catch {
                                Show-UDToast -Message "Failed to delete $($Value.SelectedRows.Hostname) - $($_.Exception.Message)" -Duration 3000 -Persistent
                            }
                        } -ShowLoading
                        
                    }
                }
            }
        }     
    }
} -LoadingComponent {
    New-UDTypography -Text "Loading ..." -Variant h4
    New-UDProgress
}
1 Like

Thanks a lot for the explanation! I really appreciate that part! :slight_smile: