5 min read

ConfigMgr - HTA Template example

ConfigMgr - HTA Template example

Some time ago i did a template to be used with eg configmgr. Did some more work to it now in order to add some logic and functions like radiobuttons and comboboxes being disabled/enabled depending on choices

Now its just filled with some dummy-data in order to show something and for you to easier understand what does what.

EndpointManagement/OSD-FrontEndHTATemplate.ps1 at master · Love-A/EndpointManagement
Endpoint Management stuff galore! Contribute to Love-A/EndpointManagement development by creating an account on GitHub.
<#
  Just a template for an OSD FrontEndHTA to be used with eg. ConfigMgr.
  Check "$WPFRunButton.Add_Click({})" to enable / edit what happens when you 	click the "run" button.
#>

#region Load Pre-req

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms

[Windows.Forms.Application]::EnableVisualStyles()

## Try to Load TS Environment
Try {
    $TSEnvironment = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction Continue 
    Write-Output "Task Sequence Variables loaded"
}
Catch {
    $_.Exception.Message
    Write-Output "Task Sequence Variables not loaded, assuming run in full OS, outside of task sequence environment.."
    $TSEnvironment = $null
}

#endregion
$InputXML = @"
<Window x:Name="OSDFrontendHTA" x:Class="OSD_FrontendHTA.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_FrontendHTA"
        mc:Ignorable="d"
        Title="Device Customization" Height="367" Width="320" WindowStartupLocation="CenterScreen" Topmost="True" IsManipulationEnabled="False">
    <Grid>
        <TextBlock 
            x:Name="TopTextBlock" 
            HorizontalAlignment="Left" 
            Margin="77,13,0,0" 
            TextWrapping="Wrap" 
            VerticalAlignment="Top"
        />
        <ComboBox 
            x:Name="TopComboBox" 
            HorizontalAlignment="Center" 
            Margin="0,34,0,0" 
            VerticalAlignment="Top" 
            Width="166"
            IsReadOnly="True"
            ItemsSource = "{DynamicResource Top}"
        />
        <TextBlock 
            x:Name="MidTextBlock" 
            HorizontalAlignment="Left" 
            Margin="77,73,0,0" 
            TextWrapping="Wrap" 
            VerticalAlignment="Top"
        />
        <ComboBox 
            x:Name="MidComboBox" 
            HorizontalAlignment="Center" 
            Margin="0,94,0,0" 
            VerticalAlignment="Top" 
            Width="166"
            IsReadOnly="True"
            ItemsSource="{DynamicResource Mid}"
        />
        <TextBlock 
            x:Name="BottomTextBlock" 
            HorizontalAlignment="Left" 
            Margin="77,133,0,0" 
            TextWrapping="Wrap" 
            VerticalAlignment="Top"
        />
        <ComboBox 
            x:Name="BottomComboBox" 
            HorizontalAlignment="Center" 
            Margin="0,154,0,0" 
            VerticalAlignment="Top" 
            Width="166" 
            IsReadOnly="True"
            ItemsSource="{DynamicResource Bottom}"
        />
        <TextBlock 
            x:Name="OfficeTextBlock" 
            HorizontalAlignment="Left" 
            Margin="36,208,0,0" 
            TextWrapping="Wrap" 
            VerticalAlignment="Top"
        />
        <RadioButton 
        x:Name="RadioButton_Yes" 
        HorizontalAlignment="Left" 
        Margin="36,228,0,0" 
        VerticalAlignment="Top" 
        IsChecked="False">
            <RadioButton.Style>
                <Style TargetType="RadioButton">
                    <Setter Property="IsEnabled" Value="False"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=TopComboBox}" Value="Company X">
                            <Setter Property="IsEnabled" Value="True"/>
                            <Setter Property="IsChecked" Value="False"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=TopComboBox}" Value="Company Y">
                            <Setter Property="IsEnabled" Value="True"/>
                            <Setter Property="IsChecked" Value="False"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RadioButton.Style>
        </RadioButton>
        <RadioButton 
        x:Name="RadioButton_No" 
        HorizontalAlignment="Left" 
        Margin="36,243,0,0" 
        VerticalAlignment="Top" 
        IsChecked="False">
            <RadioButton.Style>
                <Style TargetType="RadioButton">
                    <Setter Property="IsEnabled" Value="False"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=TopComboBox}" Value="Company X">
                            <Setter Property="IsEnabled" Value="True"/>
                            <Setter Property="IsChecked" Value="False"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=SelectedItem, ElementName=TopComboBox}" Value="Company Y">
                            <Setter Property="IsEnabled" Value="True"/>
                            <Setter Property="IsChecked" Value="False"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RadioButton.Style>
        </RadioButton>
        <Button 
        x:Name="RunButton"
        HorizontalAlignment="Center" 
        Margin="0,287,0,0" 
        VerticalAlignment="Top" 
        Height="34" 
        Width="90"
        />
    </Grid>
</Window>
"@

# Rewrite XAML

$inputXML = $inputXML -replace '\s{1}[\w\d_-]+="{x:Null}"', ''
$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
$inputXML = $inputXML -replace 'TextChanged="[\w\d-]+\w"', ''
$inputXML = $inputXML -replace 'SelectionChanged="[\w\d-]+\w"', ''
$inputXML = $inputXML -replace ' Selected="[\w\d-]+\w"', ''
$inputXML = $inputXML -replace ' Click="[\w\d-]+"', ''
$inputXML = $inputXML -replace 'Checked="CheckBox_Checked" ', ''
$inputXML = $inputXML -replace 'Checked="RadioButton_Checked" ', ''

[xml]$xaml = $inputXML
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
try {
    $Form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch {
    Write-Warning $_.Exception
    throw
}

$xaml.SelectNodes("//*[@Name]") | ForEach-Object {
    try {
        Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop
    }
    catch { throw }
}

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 }
    get-variable WPF*
}

Get-FormVariables

#===========================================================================
# Element Code
#===========================================================================

# Element Text Customization
    $WPFTopTextBlock.Text = "Company"
    $WPFMidTextBlock.Text = "Organisation"
    $WPFBottomTextBlock.Text = "Department"
    $WPFOfficeTextBlock.Text = "Enroll in Intune?"

    $WPFRadioButton_Yes.Content = "Yes"
    $WPFRadiobutton_No.Content = "No"

    $WPFRunButton.Content = "Run"

# Combobox Mid and Bottom Default State
    $WPFMidComboBox.IsEnabled = $false
    $WPFBottomComboBox.IsEnabled = $false

# Top ComboBox Items
$TopItems = (
    "Company X",
    "Company Y",
    "Company Z"
)

[Void]$Form.Resources.Add("Top", $TopItems)

# Mid Combobox Items
$MidItems = (
    "Organisation 1",
    "Organisation 2"
)


# Bottom Combobox Items
$BottomItemsCompanyX = (
    "Development",
    "Finance"
)

$BottomItemsCompanyY = (
    "Support",
    "Communication"
)

$BottomItemsCompanyZ = (
    "HR",
    "Corporate Management"
)

$WPFTopComboBox.add_SelectionChanged({
    $Form.Resources.Remove("Bottom")
    $Form.Resources.Remove("Mid")
    $WPFMidComboBox.IsEnabled = $false
    $WPFBottomComboBox.IsEnabled = $false
    
    if($WPFTopComboBox.SelectedItem -eq "Company Z"){
        $WPFMidComboBox.IsEnabled = $true
        $Form.Resources.Add("Mid", $MidItems)
    }

    If($WPFTopComboBox.SelectedItem -eq 'Company X' -or $WPFTopComboBox.SelectedItem -eq 'Company Z'){
        $WPFBottomComboBox.IsEnabled = $true
    }

    If($WPFTopComboBox.SelectedItem -eq 'Company X'){
        $Form.Resources.Add("Bottom", $BottomItemsCompanyX)
        $WPFRadioButton_Yes.IsChecked = "True"
        $WPFBottomComboBox.IsEnabled = $true
    }

    If($WPFTopComboBox.SelectedItem -eq 'Company Y'){
        $Form.Resources.Add("Bottom", $BottomItemsCompanyY)
        $WPFRadioButton_No.IsChecked = "True"
        $WPFBottomComboBox.IsEnabled = $true
    }

    If($WPFTopComboBox.SelectedItem -eq 'Company Z'){
        $Form.Resources.Add("Bottom", $BottomItemsCompanyZ)
        $WPFRadioButton_No.IsChecked = "True"
        $WPFBottomComboBox.IsEnabled = $true
    }
})

#Run Button
$WPFRunButton.Add_Click({
         
    # RadioButton Select message
        if($WPFRadioButton_Yes.IsChecked -eq "True"){
        $IntuneChoice =  "Device will be enrolled in Intune"
    }
    if($WPFRadiobutton_No.IsChecked -eq "True"){
        $IntuneChoice = "Device wont enroll in Intune"
    }

    # Determine RadioButton Selection
    $RadioButtons = ($WPFRadioButton_Yes, $WPFRadioButton_No)
    $IntuneEnrollemnt = $RadioButtons | Where-object {$_.IsChecked -eq "true"} | Select -ExpandProperty Content

    # Run Button Final Message
    $RunMessage = "
        This client will be installed with the following preferences:` 
        -$($WPFTopComboBox.SelectedItem) `
            -$($WPFMidComboBox.SelectedItem) `
                -$($WPFBottomComboBox.SelectedItem) `
        `
        *$IntuneChoice`
    "

    If([string]::IsNullOrEmpty($WPFTopComboBox.SelectedItem)){
        If([string]::IsNullOrEmpty($WPFTopComboBox.SelectedItem)){
            $msgBoxInput = [System.Windows.MessageBox]::Show("You need to choose a company", 'Choose Company', 'OK')
            return
        }
    }
    if($WPFTopComboBox.SelectedItem -eq "Company Z"){
        If([string]::IsNullOrEmpty($WPFMidComboBox.SelectedItem)){
            $msgBoxInput = [System.Windows.MessageBox]::Show("You need to choose organisation!", 'Choose Organisation', 'OK')
            return
        }
    }

    If([string]::IsNullOrEmpty($WPFBottomComboBox.SelectedItem)){
        $msgBoxInput = [System.Windows.MessageBox]::Show("You need to choose an Department", 'Choose Department', 'OK')
        return
    }

    $msgBoxInput = [System.Windows.MessageBox]::Show(
        "$RunMessage",`
        'Confirm settings',`
        'OKCancel'
    )
    If($msgBoxInput -eq "Cancel"){
        return
    }
    
    If($TSEnvironment -eq "null"){
        ##Set/Create TS Variables
        $TSEnvironment.Value("TopBox") = $WPFTopComboBox.SelectedItem
        $TSEnvironment.Value("MidBox") = $WPFMidComboBox.SelectedItem
        $TSEnvironment.Value("BottomBox") = $WPFBottomComboBox.SelectedItem
        $TSEnvironment.Value("RadioButton") = $IntuneEnrollemnt
    }

    $Form.Close()
})

#===========================================================================
# Shows the form
#===========================================================================

[void]$Form.ShowDialog()