Product: PowerShell Universal
Version: 1.4.7
Good day!
I’m currently in the process of building a stepper to walk my offshore team through a few things using PowerCLI. In my code, I am building a folder tree (if you’ve used vCenter, it’s basically the VM folders), which works fine. Where I am running into problems is getting the value of the selected node to be able to pass to a subsequent script.
I tried using the -OnNodeClicked option to view any $EventData, but I get nothing. Does anyone know how to retrieve the selected node value from a UDTreeNode?
Code below for reference:
New-UDStep -OnLoad {
$json = ConvertFrom-Json $Body
$vcServer = $json.context.vcenterSelect
$vcCred = $json.context.vcenterLogin
$ssoUser = $json.context.ssoUser
$ssoRsaToken = $json.context.ssoRSAToken
# First, we build the tree for the VM folders.
New-UDGrid -Container -Content {
New-UDGrid -Item -ExtraSmallSize 3 -Content {
New-UDCard -Elevation 5 -Title "VM Folder Selection" -Content {
$json = ConvertFrom-Json $Body
$vcServer = $json.context.vcenterSelect
$vcCred = $json.context.vcenterLogin
$datacenters = Get-Datacenter -Server $Cache:vcSession
[System.Collections.ArrayList]$subFolderTreeList = @()
[System.Collections.ArrayList]$folderTreeList = @()
New-UDTreeView -Id 'FolderSelect' -Node {
#Do this block for each virtual datacenter.
foreach ($datacenter in $datacenters) {
# Get and generate the vCenter vmFolder MoRef from the ExtensionData.
$topParentFolderRef = (Get-Datacenter -Server $Cache:vcSession).ExtensionData.vmFolder
$topParentFolder = "$($topParentFolderRef.Type)-$($topParentFolderRef.value)"
# Get all the vm Folders.
$allVMFolders = Get-Folder -Server $Cache:vcSession -Type VM | Get-View
# Get all the top level folders, sorted by name
$topFolders = $allVMFolders | ?{ $_.Parent -eq $topParentFolder } | Sort-Object Name
# Get each child folder (any folder where the datacenter vmFolder MoRef is NOT the Parent).
$childFolders = $allVMFolders | ?{ $_.Parent -ne $topParentFolder }
# For each child folder, add the Name and PARENT MoRef to the ArrayList.
foreach ($childFolder in $childFolders) {
if ($childFolder.Parent -in $topFolders.MoRef) {
$parent = ($topFolders | ?{$_.MoRef -eq $childFolder.Parent}).MoRef
$subFolderTreeList.Add(@{$childFolder.Name = $parent}) | Out-Null
}
}
# Now we build the tree. For each top level folder (folder where the Parent MoRef is the datacenter's vmFolder MoRef),
# Add an entry that does not expand. Otherwise, add the top level entry followed by each of the child entries.
foreach ($item in $topFolders) {
$children = $false
for ($i=0; $i -lt $subFolderTreeList.Keys.Count; $i++) {
if ($subFolderTreeList.Values[$i] -eq $item.MoRef) {
$children = $true
}
}
if ($children) {
New-UDTreeNode -Name $item.Name -id $item.MoRef -Children {
for ($i=0; $i -lt $subFolderTreeList.Keys.Count; $i++) {
if ($subFolderTreeList.Values[$i] -eq $item.MoRef) {
# Since we have the folder name, but only it's PARENT MoRef, we need to determine the folder's
# actual MoRef here so we can pass the location properly. This helps ensure that we have the
# correct path/location when there are multiple sub-folders with the same name under different
# parent folders.
$folderID = (Get-Folder -Server $Cache:vcSession -Type VM -Name $subFolderTreeList.Keys[$i] `
| Get-View | ?{ $_.Parent -eq $item.MoRef }).MoRef
New-UDTreeNode -Name $subFolderTreeList.Keys[$i] -id $folderID
}
}
}
}
else {
New-UDTreeNode -Name $item.Name -id $item.MoRef
}
}
}
} -OnNodeClicked {
Show-UDModal -Content { "Data is $EventData" }
}
}
}
New-UDGrid -Item -ExtraSmallSize 3 -Content {
New-UDCard -Elevation 5 -Title "Local Admin Account" -Content {
New-UDTextbox -Id 'custUser' -Label "Customer Username"
New-UDTextbox -Id 'custPassword' -Label "Customer Password" -Type password
}
}
}
} -Label "Choose customer and Local User/Password" # End Step 2