2023-07-27 16:06:41 -05:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Set the maximum number of threads for the RunspacePool to the number of threads on the machine
|
2023-05-09 13:14:27 -05:00
|
|
|
$maxthreads = [int]$env:NUMBER_OF_PROCESSORS
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Create a new session state for parsing variables into our runspace
|
2023-05-09 13:14:27 -05:00
|
|
|
$hashVars = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'sync',$sync,$Null
|
|
|
|
$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Add the variable to the session state
|
2023-05-09 13:14:27 -05:00
|
|
|
$InitialSessionState.Variables.Add($hashVars)
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Get every private function and add them to the session state
|
2023-05-09 13:14:27 -05:00
|
|
|
$functions = Get-ChildItem function:\ | Where-Object {$_.name -like "*winutil*" -or $_.name -like "*WPF*"}
|
|
|
|
foreach ($function in $functions){
|
|
|
|
$functionDefinition = Get-Content function:\$($function.name)
|
|
|
|
$functionEntry = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList $($function.name), $functionDefinition
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2023-05-09 13:14:27 -05:00
|
|
|
$initialSessionState.Commands.Add($functionEntry)
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Create the runspace pool
|
|
|
|
$sync.runspace = [runspacefactory]::CreateRunspacePool(
|
|
|
|
1, # Minimum thread count
|
|
|
|
$maxthreads, # Maximum thread count
|
|
|
|
$InitialSessionState, # Initial session state
|
|
|
|
$Host # Machine to create runspaces on
|
|
|
|
)
|
2023-05-09 13:14:27 -05:00
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Open the RunspacePool instance
|
2023-05-09 13:14:27 -05:00
|
|
|
$sync.runspace.Open()
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Create classes for different exceptions
|
2023-03-07 14:28:00 -06:00
|
|
|
|
|
|
|
class WingetFailedInstall : Exception {
|
|
|
|
[string] $additionalData
|
|
|
|
|
|
|
|
WingetFailedInstall($Message) : base($Message) {}
|
|
|
|
}
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2023-03-07 14:28:00 -06:00
|
|
|
class ChocoFailedInstall : Exception {
|
|
|
|
[string] $additionalData
|
|
|
|
|
|
|
|
ChocoFailedInstall($Message) : base($Message) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GenericException : Exception {
|
|
|
|
[string] $additionalData
|
|
|
|
|
|
|
|
GenericException($Message) : base($Message) {}
|
|
|
|
}
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2023-03-07 14:28:00 -06:00
|
|
|
|
|
|
|
$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
|
2023-07-27 16:06:41 -05:00
|
|
|
|
|
|
|
if ((Get-WinUtilToggleStatus WPFToggleDarkMode) -eq $True){
|
|
|
|
$ctttheme = 'Matrix'
|
|
|
|
}
|
|
|
|
Else{
|
|
|
|
$ctttheme = 'Classic'
|
|
|
|
}
|
|
|
|
|
2023-07-13 15:46:00 -05:00
|
|
|
$inputXML = Set-WinUtilUITheme -inputXML $inputXML -themeName $ctttheme
|
|
|
|
|
2023-03-07 14:28:00 -06:00
|
|
|
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
|
|
|
|
[xml]$XAML = $inputXML
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Read the XAML file
|
2023-03-07 14:28:00 -06:00
|
|
|
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
|
2023-05-09 13:14:27 -05:00
|
|
|
try { $sync["Form"] = [Windows.Markup.XamlReader]::Load( $reader ) }
|
2023-03-07 14:28:00 -06:00
|
|
|
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 <button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
|
|
|
|
}
|
|
|
|
|
|
|
|
#===========================================================================
|
|
|
|
# Store Form Objects In PowerShell
|
|
|
|
#===========================================================================
|
|
|
|
|
2023-05-09 13:14:27 -05:00
|
|
|
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)}
|
2023-03-07 14:28:00 -06:00
|
|
|
|
2023-05-09 13:14:27 -05:00
|
|
|
$sync.keys | ForEach-Object {
|
|
|
|
if($sync.$psitem){
|
|
|
|
if($($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "Button"){
|
|
|
|
$sync["$psitem"].Add_Click({
|
|
|
|
[System.Object]$Sender = $args[0]
|
|
|
|
Invoke-WPFButton $Sender.name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 14:28:00 -06:00
|
|
|
}
|
|
|
|
|
2023-05-09 13:14:27 -05:00
|
|
|
|
2023-07-27 16:06:41 -05:00
|
|
|
$sync.keys | ForEach-Object {
|
|
|
|
if($sync.$psitem){
|
|
|
|
if(
|
|
|
|
$($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "CheckBox" `
|
|
|
|
-and $sync["$psitem"].Name -like "WPFToggle*"
|
|
|
|
){
|
|
|
|
$sync["$psitem"].IsChecked = Get-WinUtilToggleStatus $sync["$psitem"].Name
|
|
|
|
|
|
|
|
$sync["$psitem"].Add_Click({
|
|
|
|
[System.Object]$Sender = $args[0]
|
|
|
|
Invoke-WPFToggle $Sender.name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:28:00 -06:00
|
|
|
|
|
|
|
#===========================================================================
|
|
|
|
# Setup background config
|
|
|
|
#===========================================================================
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Load computer information in the background
|
2023-03-07 14:28:00 -06:00
|
|
|
Invoke-WPFRunspace -ScriptBlock {
|
|
|
|
$sync.ConfigLoaded = $False
|
|
|
|
|
|
|
|
$sync.ComputerInfo = Get-ComputerInfo
|
|
|
|
|
|
|
|
$sync.ConfigLoaded = $True
|
|
|
|
} | Out-Null
|
|
|
|
|
|
|
|
#===========================================================================
|
2023-10-19 17:12:55 -05:00
|
|
|
# Setup and Show the Form
|
2023-03-07 14:28:00 -06:00
|
|
|
#===========================================================================
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Print the logo
|
2023-03-07 14:28:00 -06:00
|
|
|
Invoke-WPFFormVariables
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Check if Chocolatey is installed
|
|
|
|
Install-WinUtilChoco
|
|
|
|
|
|
|
|
# Set the titlebar
|
2023-05-09 13:14:27 -05:00
|
|
|
$sync["Form"].title = $sync["Form"].title + " " + $sync.version
|
2023-10-19 17:12:55 -05:00
|
|
|
# Set the commands that will run when the form is closed
|
2023-05-09 13:14:27 -05:00
|
|
|
$sync["Form"].Add_Closing({
|
|
|
|
$sync.runspace.Dispose()
|
|
|
|
$sync.runspace.Close()
|
|
|
|
[System.GC]::Collect()
|
|
|
|
})
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
# Show the form
|
2023-05-09 13:14:27 -05:00
|
|
|
$sync["Form"].ShowDialog() | out-null
|
2023-10-19 17:12:55 -05:00
|
|
|
|
|
|
|
Stop-Transcript
|