Forum Discussion

Shehzad's avatar
Shehzad
Copper Contributor
Oct 01, 2023

strange behavior when selecting multiple values in GridView

 

I was writing a script to export Hyper-V VM, the script is basically meant to be run from CLI or scheduled-tasks. but I like to make my script also able to be used interactively.

 

In Screencast GIF above, if no VMs have been supplied through CLI, a GridView is shown with names of available VMs. Multiple values (VMs) can be selected in the GridView. The Code for displaying GridView and reading the selection is highlighted in screencast.

 

If I start the script through Powershell-ISE start button (F5), then the script works as expected, if multiple values are selected, values are then stored as array.

 

If the script starts through commandline, and multiple values are selected in GridView, all the selected values are stored as a string, which is not the desired behavior!

 

Why is it so and how to resolve it to get the expected result?

 

OS is Windows 10 Pro 22H2

10 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Shehzad 

     

    Are you able to post the code down to and including the remote call to the hypervisor?

     

    Also, which version of PowerShell are you running within the ISE session? (Type $PSVersionTable in the blue section.)

     

    I can't reproduce the behaviour of Output-GridView producing a string when multiple list values have been selected.

     

    Cheers,

    Lain

    • Shehzad's avatar
      Shehzad
      Copper Contributor

      LainRobertson 

      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

       

       

      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        Shehzad 

         

        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

Resources