Forum Discussion

NThoman's avatar
NThoman
Iron Contributor
Jul 10, 2024

Read Item and Subs from System.Windows.Forms.ListView

I am building a UI around a PowerShell script to make it more user friendly. I am have ListView that holds a Queue of Jobs that need to be run. How do I loop through it and get the values of each column so I can pass the properties as variables to another process\function?

 

$JobQue = New-Object System.Windows.Forms.ListView
$JobQue.Text = 'Queued Jobs'
#$JobQue.Font = $DefaultFont
$JobQue.Location = '435,140'
$JobQue.Size = '730,235'
$JobQue.View = "Details"
$JobQue.Sorting = "Ascending"
$JobQue.CheckBoxes     = $true
$JobQue.MultiSelect    = $true
$JobQue.FullRowSelect  = $true
[VOID]$JobQue.Columns.Add('Time', 170)
[VOID]$JobQue.Columns.Add('Source Server',115)
[VOID]$JobQue.Columns.Add('Source DB',115)
[VOID]$JobQue.Columns.Add('Dest Server',115)
[VOID]$JobQue.Columns.Add('Dest DB',115)
[VOID]$JobQue.Columns.Add('Status',90)

 

That is the snippet of the JobQue List.  Whole PS script is quite large. Trying to do something like this:

 

$GoButton.Add_Click({
    ForEach($Task in $JobQue.Items){
        Write-Host "Starting Refresh $Task"
        $Task.SubItems.Text
        My-Task($Task.['Source Server'],$Task.['Dest Server'])
    }
})
  • sdtslmn's avatar
    sdtslmn
    Brass Contributor

    NThoman 

     

    hope the following example helps if you need help please ping me 

     

    $GoButton.Add_Click({
        foreach ($item in $JobQue.Items) {
            if ($item.Checked) { # Process only if the item is checked
                $time = $item.SubItems[0].Text
                $sourceServer = $item.SubItems[1].Text
                $sourceDB = $item.SubItems[2].Text
                $destServer = $item.SubItems[3].Text
                $destDB = $item.SubItems[4].Text
                $status = $item.SubItems[5].Text  # Assuming you have 6 columns
    
                Write-Host "Starting Refresh $time - $sourceServer/$sourceDB to $destServer/$destDB"
                My-Task -SourceServer $sourceServer -DestServer $destServer # Pass values to your function
            }
        }
    })

Resources