Found a “bug” (of my own creation) based on this thread here
The bug I created is the logic when removing a row. I based it on the ‘PrinterName’ value assuming it would be unique within the CSV files, however some files have “duplicate” PrinterName values. The team is going to try and correct those as ideally PrinterName should be unique. However, if someone mistakenly adds a duplicate PrinterName value or deletes an incorrect duplicate PrinterName row, the entire table/CSV file gets deleted. I’d like to avoid that and am wondering if there is a property in the $EventData or logic I can build within the -OnClick action to return all rows except for the row where user clicked on the ‘Delete’ button. Below is the relevant code section for the dynamic table:
New-UDDynamic -Id 'table' -Content {
$CsvRootPath = 'D:\Files\PrinterTest'
$Session:CsvPath = Join-Path $CsvRootPath "$($Session:Endpoint).csv"
if (Test-Path $Session:CsvPath) {
[Array]$RawData = Import-Csv $Session:CsvPath -ErrorAction Stop
$DefaultPrinter = ($RawData | Select-Object -First 1).PrinterName
$DefaultSet = $false # used to avoid multiple "Default" result returns
$FormattedData = $RawData.ForEach({
$Obj = [PSCustomObject]@{
DefaultPrinter = $null
Hostname = $_.Hostname
PrinterName = $_.PrinterName
PrinterIP = $_.PrinterIP
PrinterDriver = $_.PrinterDriver
}
If($_.PrinterName -eq $DefaultPrinter -and $DefaultSet -eq $false){ # Check/Define default printer
$Obj.DefaultPrinter = 'True'
$DefaultSet = $true
}
$Obj #Returns object to $FormattedData parent array
})
# Create Table from Data #
$Columns = @(
New-UDTableColumn -Property Hostname -Title 'Hostname'
New-UDTableColumn -Property PrinterName -Title 'Printer Name'
New-UDTableColumn -Property PrinterIP -Title 'Printer IP'
New-UDTableColumn -Property PrinterDriver -Title 'Printer Driver'
New-UDTableColumn -Property DefaultPrinter -Title 'Default Printer'
New-UDTableColumn -Property Delete -Title Delete -Render {
New-UDButton -Text 'Delete' -OnClick {
$PrinterName = $EventData.PrinterName
Show-UDModal -Content {
New-UDForm -Content {
New-UDCheckBox -Label "Check box and click Submit to confirm deletion of [$PrinterName], or choose Cancel." -Id 'Confirmation'
} -OnSubmit {
If ((Get-UDElement -Id 'Confirmation').checked -eq $true) {
## PrinterName property is no good if there are duplicates, need to uniquely identify the row/value somehow ##
$Output = $FormattedData.Where({$_.PrinterName -notin $PrinterName})
$Output | Export-Csv $Session:CsvPath -NoTypeInformation -Force -UseQuotes Never
Sync-UDElement -Id 'table'
Hide-UDModal
}
Else {
Hide-UDModal
}
} -OnCancel {
Hide-UDModal
}
} -FullWidth -MaxWidth 'md'
}
}
)
New-UDTable -Data $FormattedData -Columns $Columns -ShowSelection
} Else {
New-UDAlert -Severity 'error' -Content { New-UDHtml "Unable to find file for <strong>$($Session:Endpoint)</strong>" }
}
}
Product: PowerShell Universal
Version: 1.5.19