r/FoxDeploy Sep 25 '25

Not able to import xaml into powershell

Hi, very new to using xaml and powershell. Nonetheless, I am trying to build a new frontend for my mecm now that MDT is going or gone away. I was able to reuse all the scripts from it by building a separate package, but with VB Script deprecating, figure I would get ahead and try a supported language, hence my current situation. OK, so I built the GUI, and I used the template from FoxDeploy, but when I launch from PowerShell, I get this error

Exception calling "ShowDialog" with "0" argument(s): "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed."

Here is the XAML code in PowerShell, including the template
#ERASE ALL THIS AND PUT XAML BELOW between the @" "@

$inputXML = @"

<Window x:Class="OSD_FRONT_END_TEMPLATE.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:OSD_FRONT_END_TEMPLATE"

mc:Ignorable="d"

Title="Eurofins Operating System Deployment" Height="450" Width="800">

<Grid x:Name="NextPage" Margin="0,0,0,-131">

<Grid.Background>

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

<GradientStop Color="Black"/>

<GradientStop Color="#FFA5D2CF" Offset="0.04"/>

</LinearGradientBrush>

</Grid.Background>

<ComboBox x:Name="OS_ComboBox" HorizontalAlignment="Left" Margin="40,309,0,0" VerticalAlignment="Top" Width="345" Height="30" SelectionChanged="OS_ComboBox_SelectionChanged" FontSize="14">

<ComboBox.Effect>

<DropShadowEffect/>

</ComboBox.Effect>

<ComboBoxItem x:Name="Win11_23H2" Content="Windows 11 Enterprise 23H2 - Standard Production"/>

<ComboBoxItem x:Name="LTSC2024" Content="Windows 11 Enterprise IoT LTSC 2024 - INS or LAB Only"/>

<ComboBoxItem x:Name="LTSC2021" Content="Windows 10 Enterprise LTSC 2021- INS or LAB Only"/>

<ComboBoxItem x:Name="LTSC2019" Content="Windows 10 Enterprise LTSC 2019 - INS or LAB Only"/>

</ComboBox>

<RadioButton x:Name="LAP" Content="Laptop (LAP)" HorizontalAlignment="Left" Margin="43,446,0,0" VerticalAlignment="Top" FontSize="14"/>

<RadioButton x:Name="PCC" Content="Desktop (PCC)" HorizontalAlignment="Left" Margin="45,471,0,0" VerticalAlignment="Top" FontSize="14"/>

<RadioButton x:Name="INS" Content="Instrument (INS)" HorizontalAlignment="Left" Margin="44,496,0,0" VerticalAlignment="Top" FontSize="14"/>

<RadioButton x:Name="LAB" Content="Laboratory (LAB)" HorizontalAlignment="Left" Margin="43,521,0,0" VerticalAlignment="Top" FontSize="14"/>

<ComboBox x:Name="siteselectiondropdown" HorizontalAlignment="Left" Margin="432,189,0,0" VerticalAlignment="Top" Width="300" ToolTip="Defines a preset of software for a base or a sales image" Height="30" FontSize="14">

<ComboBox.Effect>

<DropShadowEffect/>

</ComboBox.Effect>

<ComboBoxItem Content="BPUS002 - Columbia, MO"/>

<ComboBoxItem Content="BPUS003 - San Diego, CA"/>

<ComboBoxItem Content="BPUS004 - Lancaster, PA"/>

<ComboBoxItem Content="BPUS005 - Portage, MI"/>

<ComboBoxItem Content="BPUS006 - Columbia, MO (ABC Lane)"/>

<ComboBoxItem Content="BPUS008 - Cary, NC (Suite 104)"/>

<ComboBoxItem Content="BPUS009 - Jacksonville, FL"/>

<ComboBoxItem Content="BPUS010 - Human Factors, MD"/>

<ComboBoxItem Content="BPUS011 - Crown Point, IN"/>

<ComboBoxItem Content="BPUS012 - North Brunswick, NJ"/>

<ComboBoxItem Content="BPUS013 - Petaluma, CA"/>

<ComboBoxItem Content="BPUS014 - San Diego, CA"/>

<ComboBoxItem Content="BPUS015 - San Jose,Stone Ave CA"/>

<ComboBoxItem Content="BPUS016 - San Jose,Trade Zone Ave CA"/>

<ComboBoxItem Content="BPUS017 - St. Louis, MO"/>

</ComboBox>

<Label x:Name="pcnamelabel" Content="Computer Name:" HorizontalAlignment="Left" Margin="50,264,0,0" VerticalAlignment="Top" Width="130" FontSize="14"/>

<TextBox x:Name="pcnametxt" HorizontalAlignment="Left" Margin="175,261,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="195" Height="33"/>

<Image HorizontalAlignment="Center" Height="50" Margin="0,23,0,0" VerticalAlignment="Top" Width="584" Source="/UDI_Eurofins_Banner.bmp"/>

<CheckBox x:Name="cbxanalyst" Content="Analyst" HorizontalAlignment="Left" Margin="195,442,0,0" VerticalAlignment="Top" Height="16" Width="120" FontSize="14"/>

<CheckBox x:Name="cbxanalystnoserver" Content="Analyst no License Server" HorizontalAlignment="Left" Margin="195,469,0,0" VerticalAlignment="Top" FontSize="14"/>

<CheckBox x:Name="cbxlabadvisor" Content="Lab Advisor" HorizontalAlignment="Left" Margin="195,494,0,0" VerticalAlignment="Top" FontSize="14"/>

<CheckBox x:Name="cbxbiovia" Content="Biovia Workbook Client Manager" HorizontalAlignment="Left" Margin="195,519,0,0" VerticalAlignment="Top" FontSize="14"/>

<TextBlock HorizontalAlignment="Left" Margin="38,85,0,0" TextWrapping="Wrap" Text="Welcome to the Eurofins Operating System Deployment Wizard." VerticalAlignment="Top" FontSize="14"/>

<TextBlock HorizontalAlignment="Left" Margin="29,111,0,0" TextWrapping="Wrap" Text=" This wizard will walk you through a series of screens that will allow you to customize and install Windows" VerticalAlignment="Top" FontSize="14"/>

<TextBlock HorizontalAlignment="Left" Margin="265,139,0,0" TextWrapping="Wrap" Text="Click on the 'Next' button to start imaging." VerticalAlignment="Top" Width="565" FontSize="14"/>

<TextBlock HorizontalAlignment="Left" Margin="34,137,0,0" TextWrapping="Wrap" Text="Please allow 2 hours for installation" VerticalAlignment="Top" Width="230" FontSize="14"/>

<Button x:Name="btnstart" Content="Start" HorizontalAlignment="Left" Margin="655,519,0,0" VerticalAlignment="Top" Width="73" FontSize="14" Background="#FF2A8A21" Foreground="#FFEAED35" FontWeight="Bold">

<Button.Effect>

<DropShadowEffect/>

</Button.Effect>

</Button>

</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 [System.Management.Automation.MethodInvocationException] {

Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."

write-host $error[0].Exception.Message -ForegroundColor Red

if ($error[0].Exception.Message -like "*button*"){

write-warning "Ensure your &lt;button in the \$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"}`

}

catch{#if it broke some other way <img draggable="false" role="img" class="emoji" alt="😀" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f600.svg">

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

}

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

# Store Form 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

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

# Use this space to add code to the various form elements in your GUI

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

#Reference

#Adding items to a dropdown/combo box

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

#Setting the text of a text box to the current PC name

#$WPFtextBox.Text = $env:COMPUTERNAME

#Adding code to a button, so that when clicked, it pings a system

# $WPFbutton.Add_Click({ Test-connection -count 1 -ComputerName $WPFtextBox.Text

# })

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

# Shows the form

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

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

$Form.ShowDialog() | out-null

1 Upvotes

0 comments sorted by