Skip to content

Instantly share code, notes, and snippets.

@pinecones-sx
Created May 17, 2019 20:28
Show Gist options
  • Save pinecones-sx/19efffa49570e3d8c0e694505734c82e to your computer and use it in GitHub Desktop.
Save pinecones-sx/19efffa49570e3d8c0e694505734c82e to your computer and use it in GitHub Desktop.
# List of accepted app parameters
$appSet = 'PowerShell','Chrome','FireFox','Reader'
#STUB# If (Test-Path ($pathToFileWithNames)){$appSet = $importedFileStringArray}
# ps 2.0 compatible enum for accepted app names == [ChocoApp]
Add-Type -TypeDefinition "public enum ChocoApp {$($appSet -join ',')}"
Function Load-Chocolatey{
If (-not $env:ChocoInstalled){
# install
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
$env:ChocoInstalled = $true
}
}
Function Install-ChocoApps{
param([Parameter(ValueFromPipeline = $true)][ChocoApp[]]$Install)
process{
Load-Chocolatey
switch ($null){
{$Install -contains 'PowerShell'}{choco install powershell -y}
{$Install -contains 'Chrome'}{choco install googlechrome -y}
{$Install -contains 'FireFox'}{choco install firefox -y}
{$Install -contains 'Reader'}{
choco install adobereader -y -params '"/EnableUpdateService"'
### stub - set defaults for PDFs
}
}
}
}
# Load GUI
##########
# Required assemblies
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Main window
$formAddQOLApps = [System.Windows.Forms.Form]::new()
$formAddQOLApps.Text = 'QOL apps install'
$formAddQOLApps.Size = [System.Drawing.Size]::new(295,325)
$formAddQOLApps.StartPosition = 'CenterScreen'
$formAddQOLApps.FormBorderStyle = 'Fixed3D'
$formAddQOLApps.MaximizeBox = $false
$margin = @{
'top' = 10
'left' = 15
'right' = 15
'bottom' = 10
'buffer' = 10
}
# Title
$labelTitle = [System.Windows.Forms.Label]::new()
$labelTitle.Location = [System.Drawing.Point]::new(($margin.left - 5),$margin.top)
$labelTitle.Size = [System.Drawing.Size]::new(260,25)
$labelTitle.Text = 'Select apps to install:'
$labelTitle.Font = [System.Drawing.Font]::new('Calibri',16)
#$labelTitle.BackColor = [System.Drawing.Color]::Beige
$formAddQOLApps.Controls.Add($labelTitle)
# CheckedList box
$checkedListBoxCompany = [System.Windows.Forms.CheckedListBox]::new()
$checkedListBoxCompany.Size = [System.Drawing.Size]::new(260,20)
$checkedListBoxCompany.Height = 200
$checkedListBoxCompany.Location = [System.Drawing.Point]::new(
$labelTitle.Left,
($labelTitle.Bottom + $margin.buffer)
)
$checkedListBoxCompany.CheckOnClick = $true
# add items
ForEach ($app in $appSet){
$null = $checkedListBoxCompany.Items.Add($app,$true)
}
$formAddQOLApps.Controls.Add($checkedListBoxCompany)
# Buttons
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Location = New-Object System.Drawing.Point(
($checkedListBoxCompany.Right - $CancelButton.Size.Width),
($checkedListBoxCompany.Bottom + $margin.buffer)
)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$formAddQOLApps.CancelButton = $CancelButton
$formAddQOLApps.Controls.Add($CancelButton)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Location = New-Object System.Drawing.Point(
($CancelButton.Left - $OKButton.Size.Width - $margin.buffer),
$CancelButton.Top
)
$OKButton.Text = 'Install'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$formAddQOLApps.AcceptButton = $OKButton
$formAddQOLApps.Controls.Add($OKButton)
### show form
# DO
$formAddQOLApps.BringToFront() <#this may not work. may need to ascync call the .showdialog()#>
$formButtonResponse = $formAddQOLApps.ShowDialog()
If (-not $checkedListBoxCompany.CheckedItems){$formButtonResponse = 'None'}
# Install apps based on selection
If($formButtonResponse -eq 'OK'){Install-ChocoApps $checkedListBoxCompany.CheckedItems}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment