Skip to content

Instantly share code, notes, and snippets.

@TScalzott
Created September 24, 2015 19:29
Show Gist options
  • Save TScalzott/8d32bbbf625910d24031 to your computer and use it in GitHub Desktop.
Save TScalzott/8d32bbbf625910d24031 to your computer and use it in GitHub Desktop.
#
# Event Handlers for our WPF controls
#
# WPF form is loaded
$Form.Add_Loaded({
# Connect to vCenter and populate the list of clusters
Connect-VIServer $vCenter
Get-Cluster | ForEach-Object { $WPF_cluster.Items.Add($_.Name) }
$WPF_console.IsEnabled = $false # No sense in clicking the button yet!
})
# A Cluster selection is made in the Cluster ComboBox
$WPF_cluster.Add_SelectionChanged({
# On selection of a cluster, populate the list of VMs. The DataGrid control
# requires an ArrayList for loading
$VM_array = New-Object System.Collections.ArrayList
$VMs = Get-Cluster $WPF_cluster.SelectedValue | Get-VM |Select-Object Name, PowerState, NumCPU, MemoryGB | Sort-Object Name
$VM_array.AddRange($VMs)
$WPF_details.clear() # Clear any prior values
$WPF_details.ItemsSource = $VM_array # Load our new values
$WPF_details.IsReadOnly = $true # No, these aren't editable
# Allow sorting on all columns
$WPF_details.Columns | ForEach-Object { $_.CanUserSort = $true }
})
# VM Selection is made in the details DataGrid
$WPF_details.Add_SelectionChanged({
# Console option is present if VM is on
$WPF_console.IsEnabled = ($WPF_details.SelectedItems -ne $null) -and
($WPF_details.SelectedValue.PowerState -eq "PoweredOn")
})
# Take action on pressing the Console button
$WPF_console.Add_Click({
Open-VMConsoleWindow -VM $WPF_details.SelectedValue.Name
$WPF_console.IsEnabled = $false # Disable until next choice
})
# WPF for is closed
$Form.Add_Closed({
Disconnect-VIServer $vCenter -Confirm:$false
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment