Forum Discussion
strange behavior when selecting multiple values in GridView
Thanks for the response, sure, here is the link to complete code on github: https://github.com/fahidsh/hyper-v-exporter/blob/main/vm-exporter.ps1
But I think it has to run on a Computer where Hyper-V is installed and probably has a few VMs (can be dummy/empty ones), since it uses the Get-VM command to get the VMs configured.
the way I am building my array for the GridView is through Get-VM, in the code
# Get All Virtual Machines on the Hyper-V Host
$VirtualMachines = Get-VM
# Add 'All' to the start of array of VM Names
$VirtualMachinesNames = @("All") + $VirtualMachines.Name
# Display the list in Grid
$VMName = $VirtualMachinesNames | Out-GridView -Title "Select VM to export" -OutputMode Multiple
# Print the Selected VM Names
foreach($vm in $VMName){
Write-Host $vm
}
# Print the number of elements selected
Write-Host $VMName.Count
I know if use a static list, it works fine, i.e.
# a static list/array
$VirtualMachines = @("VM1", "VM2", "VM3", "VM4")
# Add 'All' to the start of array of VM Names
$VirtualMachinesNames = @("All") + $VirtualMachines
# Display the list in Grid
$VMName = $VirtualMachinesNames | Out-GridView -Title "Select VM to export" -OutputMode Multiple
# Print the Selected VM Names
foreach($vm in $VMName){
Write-Host $vm
}
# Print the number of elements selected
Write-Host $VMName.Count
Actually, let me add some context to what I just said.
I tested using the script you posted in here rather than the full script from git, which I read just now and can see has other issues, not the least of which is the if statement splitting on commas, since that can cause issues given a comma can be part of a virtual machine name as shown below.
If you're using the script from git as-is, then I can have a proper read, but at this stage, it's either a very unique issue local to your computer; an oddity with PowerShell (as distinct from Windows PowerShell); or an issue with something else in the git script (likely that cumbersome if block). My feeling is it's most likely to be the latter.
Conversely, if your script is different to the one on git, we'd probably need to see it to learn more.
Cheers,
Lain
- ShehzadOct 02, 2023Copper Contributor
yes you are right about splitting from Comman, it was either to be semi-colons or comman, but since there are rarely commas in VM-Names, especially in the case where I wanted to use the script. So I went with commas.
The script on github is my script, I have posted it this morning myself