Skip to content

Commit fb8d426

Browse files
committed
Add default buttons for the installer script and restructure the code
1 parent 535b672 commit fb8d426

File tree

1 file changed

+113
-20
lines changed

1 file changed

+113
-20
lines changed

Install-Software.ps1

Lines changed: 113 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ param(
1010
[switch]$Elevated
1111
)
1212

13+
#####
14+
# Script startup
15+
#####
16+
1317
. "${PSScriptRoot}\Utils.ps1"
1418

1519
if ($RepoInUserDir) {
@@ -47,6 +51,10 @@ $GlobalWidth = 700;
4751
$SoftwareRepoPath = "V:\IT\Software"
4852
$ComputerSystem = Get-CimInstance -ClassName Win32_ComputerSystem
4953

54+
#####
55+
# Installer definitions
56+
#####
57+
5058
# TODO: hide non-work-related apps on domain computers
5159
$ChocoPrograms = [ordered]@{
5260
"7-Zip" = "7zip", "File compression utility";
@@ -655,8 +663,12 @@ $OtherOperations = [ordered]@{
655663
"Xeneth" = ${function:Install-Xeneth}, "Driver for Xenics cameras";
656664
}
657665

666+
#####
667+
# GUI functions
668+
#####
669+
658670
# Function definitions should be after the loading of utilities
659-
function CreateList {
671+
function New-List {
660672
<#
661673
.SYNOPSIS
662674
Create a GUI element for selecting options from a list with checkboxes
@@ -684,7 +696,8 @@ function CreateList {
684696
$List.Height = $Options.Count * 17 + 18;
685697
return $List;
686698
}
687-
function CreateTable {
699+
700+
function New-Table {
688701
<#
689702
.SYNOPSIS
690703
Create a GUI element for selecting items from a list with checboxes
@@ -774,7 +787,8 @@ function CreateTable {
774787

775788
return $View;
776789
}
777-
function GetSelectedCommands {
790+
791+
function Get-SelectedCommands {
778792
[OutputType([string[]])]
779793
param(
780794
[Parameter(mandatory=$true)][system.Windows.Forms.DataGridView]$View
@@ -787,7 +801,21 @@ function GetSelectedCommands {
787801
return $commands;
788802
}
789803

790-
# Script starts here
804+
function Select-Cells {
805+
param(
806+
[Parameter(mandatory=$true)][system.Windows.Forms.DataGridView]$View,
807+
[Parameter(mandatory=$true)][System.Collections.Specialized.OrderedDictionary]$Dict,
808+
[Parameter(mandatory=$true)][string[]]$Names
809+
)
810+
foreach ($Name in $Names) {
811+
$Index = $Dict.Keys.IndexOf($Name)
812+
$View.rows[$Index].Cells[0].Value = $true
813+
}
814+
}
815+
816+
#####
817+
# Create the GUI
818+
#####
791819

792820
Install-Chocolatey
793821
Install-Winget
@@ -797,28 +825,30 @@ Add-Type -AssemblyName System.Windows.Forms;
797825

798826
# Create the Form
799827
$Form = New-Object -TypeName System.Windows.Forms.Form;
800-
$Form.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right
828+
$AllDirectionsAnchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right
829+
$Form.Anchor = $AllDirectionsAnchor
801830
# $Form.AutoSize = $true;
802831
# $Form.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink;
803832
$Form.Text = "Mika's installer script"
804833
$Form.MinimumSize = New-Object System.Drawing.Size($GlobalWidth, $GlobalHeight);
805834

806835
$Layout = New-Object System.Windows.Forms.TableLayoutPanel;
807-
$Layout.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right
836+
$Layout.Anchor = $AllDirectionsAnchor
808837
# $Layout.AutoSize = $true;
809838
# $Layout.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink;
810839
# $Layout.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D;
811840
$Layout.RowCount = 6;
812841
$Form.Controls.Add($Layout);
813842

814-
$ChocoProgramsView = CreateTable -Form $Form -Parent $Layout -Title "Centrally updated programs (Chocolatey)" -Data $ChocoPrograms;
815-
$WingetProgramsView = CreateTable -Form $Form -Parent $Layout -Title "Centrally updated programs (Winget)" -Data $WingetPrograms;
843+
# Create grid views
844+
$ChocoProgramsView = New-Table -Form $Form -Parent $Layout -Title "Centrally updated programs (Chocolatey)" -Data $ChocoPrograms;
845+
$WingetProgramsView = New-Table -Form $Form -Parent $Layout -Title "Centrally updated programs (Winget)" -Data $WingetPrograms;
816846
$WingetProgramsView.Height = 50;
817-
$WindowsCapabilitiesView = CreateTable -Form $Form -Parent $Layout -Title "Windows capabilities" -Data $WindowsCapabilities;
847+
$WindowsCapabilitiesView = New-Table -Form $Form -Parent $Layout -Title "Windows capabilities" -Data $WindowsCapabilities;
818848
$WindowsCapabilitiesView.Height = 150;
819-
$WindowsFeaturesView = CreateTable -Form $Form -Parent $Layout -Title "Windows features" -Data $WindowsFeatures;
849+
$WindowsFeaturesView = New-Table -Form $Form -Parent $Layout -Title "Windows features" -Data $WindowsFeatures;
820850
$WindowsFeaturesView.Height = 95;
821-
$OtherOperationsView = CreateTable -Form $Form -Parent $Layout -Title "Other programs and operations. These you have to keep updated manually." -Data $OtherOperations
851+
$OtherOperationsView = New-Table -Form $Form -Parent $Layout -Title "Other programs and operations. These you have to keep updated manually." -Data $OtherOperations
822852
$OtherOperationsView.height = 150;
823853

824854
# Disable unsupported features
@@ -835,6 +865,10 @@ if (!(Test-CommandExists "Enable-WindowsOptionalFeature")) {
835865
$WindowsFeaturesView.Enabled = $false;
836866
}
837867

868+
$ButtonsLayout = New-Object System.Windows.Forms.TableLayoutPanel;
869+
$ButtonsLayout.Anchor = $AllDirectionsAnchor
870+
$Layout.Controls.Add($ButtonsLayout)
871+
838872
# Add OK button
839873
$Form | Add-Member -MemberType NoteProperty -Name Continue -Value $false;
840874
$OKButton = New-Object System.Windows.Forms.Button;
@@ -843,31 +877,90 @@ $OKButton.Add_Click({
843877
$Form.Continue = $true;
844878
$Form.Close();
845879
})
846-
$Layout.Controls.Add($OKButton);
880+
$ButtonsLayout.Controls.Add($OKButton);
881+
882+
# Add default buttons
883+
# These have to be after the grid view definitions.
884+
885+
function Select-CommonDefaults {
886+
$CommonDefaultChocoPrograms = @("7-Zip", "Chocolatey GUI (RECOMMENDED!)", "Firefox", "VLC")
887+
if ((Get-CimInstance -Class Win32_Processor).Manufacturer -eq "GenuineIntel") {
888+
$CommonDefaultChocoPrograms += "Intel Driver & Support Assistant"
889+
}
890+
Select-Cells -View $ChocoProgramsView -Dict $ChocoPrograms -Names $CommonDefaultChocoPrograms
891+
}
892+
893+
function Select-LabDefaults {
894+
Select-CommonDefaults
895+
Select-Cells -View $ChocoProgramsView -Dict $ChocoPrograms -Names @("Notepad++")
896+
Select-Cells -View $WindowsCapabilitiesView -Dict $WindowsCapabilities -Names @("SNMP client")
897+
}
847898

848-
function ResizeLayout {
899+
function Select-PersonalDefaults {
900+
Select-CommonDefaults
901+
Select-Cells -View $ChocoProgramsView -Dict $ChocoPrograms -Names @("KeePassXC")
902+
}
903+
904+
function Select-WorkstationDefaults {
905+
Select-CommonDefaults
906+
Select-Cells -View $ChocoProgramsView -Dict $ChocoPrograms -Names @("KeePassXC", "Notepad++", "OpenVPN", "PDF-XChange Editor", "Slack")
907+
Select-Cells -View $WindowsCapabilitiesView -Dict $WindowsCapabilities -Names @("SNMP client")
908+
Select-Cells -View $OtherOperationsView -Dict $OtherOperations -Names @("WithSecure Elements Agent")
909+
}
910+
911+
if (Get-IsDomainJoined) {
912+
$WorkstationDefaultsButton = New-Object System.Windows.Forms.Button
913+
$WorkstationDefaultsButton.Text = "Select workstation defaults"
914+
$WorkstationDefaultsButton.Width = 160
915+
$WorkstationDefaultsButton.Add_Click({
916+
Select-WorkstationDefaults
917+
})
918+
$ButtonsLayout.Controls.Add($WorkstationDefaultsButton, 1, 0)
919+
920+
$LabDefaultsButton = New-Object System.Windows.Forms.Button
921+
$LabDefaultsButton.Text = "Select lab defaults"
922+
$LabDefaultsButton.Width = 120
923+
$LabDefaultsButton.Add_Click({
924+
Select-LabDefaults
925+
})
926+
$ButtonsLayout.Controls.Add($LabDefaultsButton, 2, 0)
927+
} else {
928+
$PersonalDefaultsButton = New-Object System.Windows.Forms.Button
929+
$PersonalDefaultsButton.Text = "Select defaults for personal use"
930+
$PersonalDefaultsButton.Width = 200
931+
$PersonalDefaultsButton.Add_Click({
932+
Select-PersonalDefaults
933+
})
934+
$ButtonsLayout.Controls.Add($PersonalDefaultsButton, 1, 0)
935+
}
936+
937+
function Resize-Layout {
849938
$Layout.Width = $Form.Width - 10;
850939
$Layout.Height = $Form.Height - 10;
851940
$ChocoProgramsView.Height = $Form.Height - 660;
852941
}
853-
$Form.Add_Resize($function:ResizeLayout);
854-
ResizeLayout;
942+
$Form.Add_Resize(${function:Resize-Layout});
943+
Resize-Layout;
855944

856945
# Show the form
857946
$Form.ShowDialog();
858947
if (! $Form.Continue) {
859948
return 0;
860949
}
861950

862-
$ChocoSelected = GetSelectedCommands $ChocoProgramsView
951+
#####
952+
# Run the installations
953+
#####
954+
955+
$ChocoSelected = Get-SelectedCommands $ChocoProgramsView
863956
if ($ChocoSelected.Count) {
864957
Show-Output "Installing programs with Chocolatey."
865958
choco upgrade -y $ChocoSelected
866959
} else {
867960
Show-Output "No programs were selected to be installed with Chocolatey."
868961
}
869962

870-
$WingetSelected = GetSelectedCommands $WingetProgramsView
963+
$WingetSelected = Get-SelectedCommands $WingetProgramsView
871964
if ($WingetSelected.Count) {
872965
Show-Output "Installing programs with Winget. If asked to accept the license of the package repository, please select yes."
873966
foreach($program in $WingetSelected) {
@@ -877,7 +970,7 @@ if ($WingetSelected.Count) {
877970
Show-Output "No programs were selected to be installed with Winget."
878971
}
879972

880-
$WindowsCapabilitiesSelected = GetSelectedCommands $WindowsCapabilitiesView
973+
$WindowsCapabilitiesSelected = Get-SelectedCommands $WindowsCapabilitiesView
881974
if ($WindowsCapabilitiesSelected.Count) {
882975
Show-Output "Installing Windows capabilities."
883976
foreach($capability in $WindowsCapabilitiesSelected) {
@@ -888,7 +981,7 @@ if ($WindowsCapabilitiesSelected.Count) {
888981
Show-Output "No Windows capabilities were selected to be installed."
889982
}
890983

891-
$WindowsFeaturesSelected = GetSelectedCommands $WindowsFeaturesView
984+
$WindowsFeaturesSelected = Get-SelectedCommands $WindowsFeaturesView
892985
if ($WindowsFeaturesSelected.Count) {
893986
Show-Output "Installing Windows features."
894987
foreach($feature in $WindowsFeaturesSelected) {
@@ -900,7 +993,7 @@ if ($WindowsFeaturesSelected.Count) {
900993
}
901994

902995
# These have to be after the package manager -based installations, as the package managers may install some Visual C++ runtimes etc., which we want to update automatically.
903-
$OtherSelected = GetSelectedCommands $OtherOperationsView
996+
$OtherSelected = Get-SelectedCommands $OtherOperationsView
904997
if ($OtherSelected.Count) {
905998
Show-Output "Running other selected operations."
906999
foreach($command in $OtherSelected) {

0 commit comments

Comments
 (0)