First, quality of life. Instead of doing $(get-udelement -Id $ID).value
you have access to the $Eventdata
variable.
For example…
$Eventdata.dhcpselector
returns “True” or “False”.
$Eventdata.datacenter
Will already contain the value of…
$(get-udelement -Id datacenter).value
Second, when you are running the same command twice, I recommend using splatting.
So you would do something like this…
$OSCNM = @{
Server = $session:VC
}
If ($EventData.dhcpselector -eq $false) {
$OSCNM.Add('IPMode', 'UseStaticIP')
$OSCNM.Add('IpAddress', $EventData.vmguestip)
$OSCNM.Add('SubnetMask', $EventData.vmguestmask)
$OSCNM.Add('DefaultGateway', $EventData.vmguestgateway)
$OSCNM.Add('Dns', @($EventData.vmguestdns1, $EventData.vmguestdns2) )
}
else {
$OSCNM.Add('IPMode', 'UseDhcp')
}
Set-OSCustomizationNicMapping @OSCNM
This ultimately makes it easier to manage your code because you don’t have to repeat declaring variables and having 2 lines running the same command with different parameters. If you ever add more options, now you only have to add it once if you do it this way.
General troubleshooting of PSU Dashboards:
Comment out everything that “does” something (like Set-OSCustomizationNicMapping) and add the line…
Show-UDToast -message “$($OSCNM | ConvertTo-Json)” -Duration 50000
That lets you see the actual parameters you are passing. If you right click on the Toast, you can then click “Inspect” and drill down to see the JSON. From there you can copy it, paste it into Powershell and make it an object again. If you are using Powershell 7 you can also use ConvertFrom-Json -AsHashtable
.
I’ve used this to manually run commands using the parameters as presented by the dashboard and have found it useful when something isn’t immediately obvious or the error messages PSU gives aren’t enough.
All this being said, be very careful when running things like Wait-Task
and Start-Sleep
in the dashboard itself. I don’t use VMWare, but try adding Wait-Task
and the subsequent scriptblock to a New-UDDynamic
.
It should still wait, as Wait-Task
is tied to $Session:Buildtask
, but it won’t stop the dashboard page from executing code.