VMware Cloud Community
ITaaP
Enthusiast
Enthusiast

PowerShell GUI to Get VM Info

I am trying to do multiple things from a GUI interface, but figured I would start with something easy such as connecting to vCenter and getting a VM. Apparently not so easy. For reference on making the GUI in the first place, I was using this. https://foxdeploy.com/2015/04/16/part-ii-deploying-powershell-guis-in-minutes-using-visual-studio/

Here is the code I ended up with.

#ERASE ALL THIS AND PUT XAML BELOW between the @" "@

$inputXML = @"

<Window x:Class="WpfApplication2.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApplication2"

        mc:Ignorable="d"

        Title="PowerShell GUI" Height="345.992" Width="530.344" Topmost="True">

    <Grid Margin="0,0,45,0">

        <Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="24,28,0,0" VerticalAlignment="Top" Width="100" Source="C:\vcap.jpg"/>

        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="100" Margin="174,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" FontSize="16" Text="This is my first PowerShell GUI"/>

        <Button x:Name="button" Content="OK" HorizontalAlignment="Left" Height="55" Margin="367,254,0,0" VerticalAlignment="Top" Width="102" FontSize="18.667"/>

        <TextBox x:Name="vmname" HorizontalAlignment="Left" Height="35" Margin="223,196,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="168" FontSize="16"/>

        <Label x:Name="vm" Content="VM Name" HorizontalAlignment="Left" Height="46" Margin="56,196,0,0" VerticalAlignment="Top" Width="138" FontSize="16"/>

        <Label x:Name="vc" Content="vCenter Server" HorizontalAlignment="Left" Height="46" Margin="56,146,0,0" VerticalAlignment="Top" Width="138" FontSize="16"/>

        <TextBox x:Name="vcenter" HorizontalAlignment="Left" Height="35" Margin="223,150,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="168" FontSize="16"/>

    </Grid>

</Window>

"@      

$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')

[xml]$XAML = $inputXML

#Read XAML

    $reader=(New-Object System.Xml.XmlNodeReader $xaml)

  try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}

catch{Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."}

#===========================================================================

# Load XAML Objects In PowerShell

#===========================================================================

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}

Function Get-FormVariables{

if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}

write-host "Found the following interactable elements from our form" -ForegroundColor Cyan

get-variable WPF*

}

Get-FormVariables

#===========================================================================

# Actually make the objects work

#===========================================================================

$WPFbutton.Add_Click({

$userInput = $WPFvmname.text

$userInput = $WPFvcenter.text

$form.Close()

Connect-VIServer $WPFvcenter.Text

Get-VM $WPFvmname.text

})

#Sample entry of how to add data to a field

#$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"})

#===========================================================================

# Shows the form

#===========================================================================

write-host "To show the form, run the following" -ForegroundColor Cyan

$Form.ShowDialog() | out-null

The GUI works fine and I am able to enter in the vCenter server and name of a VM. Next the script connects to vCenter, but the Get-VM command doesn't do anything. The only time I can get Get-VM to work is if I manually enter in "Get-VM $WPFvmname.text".

Ultimately if I can get this working, I want to use it for creating folders, resource pools, distributed port groups with VLAN, etc.

https://tactsol.com https://vmware.solutions
Tags (2)
Reply
0 Kudos
0 Replies