-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply-configuration.ps1
113 lines (92 loc) · 4.37 KB
/
apply-configuration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Check for admin rights
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Output "WinGet needs Administrator rights to run. Restarting in Admin mode..."
Start-Process -Verb runas -FilePath powershell.exe -ArgumentList "iwr -useb https://raw.githubusercontent.com/robert-cpl/winget-dsc/main/apply-configuration.ps1 | iex"
break
}
Write-Host "See full source code on GitHub @ https://github.com/robert-cpl/winget-dsc" -ForegroundColor Yellow
# Check if running locally so we can use the local files
$runLocally = if (Test-Path ".gitignore") { $true } else { $false }
# Functions
function GetUserInput {
param(
[string]$message,
[string[]]$choices,
[string]$defaultValue
)
while ($true) {
# User input
Write-Host "$message Press ENTER for default value ($defaultValue)." -ForegroundColor Green
$userInput = Read-Host
# Validation
$userInput = $userInput.ToLower()
if ($userInput -eq "") {
return $defaultValue
}
if ($choices -notcontains $userInput) {
Write-Host "Invalid input, try again." -ForegroundColor Yellow
continue
}
break
}
return $userInput
}
# Variables
$defaultDscProfile = "personal"
$dscProfiles = @("personal", "developer")
# User Input
$dscProfile = GetUserInput -message "What DSC profile you want to install? ($($dscProfiles -join '/' ))." -choices $dscProfiles -defaultValue $defaultDscProfile
# Configuration file path setup
$desktopPath = $DesktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
$configurationFolderPath = "$($DesktopPath)\winget-configuration"
if (!(Test-Path $configurationFolderPath)) {
Write-Host "Creating missing configuration folder."
$folder = New-Item -ItemType Directory -Path $configurationFolderPath
$folderPath = $folder.FullName
Write-Host "Configuration folder created at $folderPath."
}
$configurationFileName = "configuration.dsc.yaml"
$configurationFilePath = "$configurationFolderPath/$configurationFileName"
function GetContent() {
param(
[string]$filePath,
[string]$indentation = '',
[bool]$runLocally = $true
)
Write-Host $filePath
$content = if ($runLocally) { Get-Content $filePath } else { (Invoke-WebRequest -useb $filePath).Content }
# add indentation to each line
$formatedContent = $content -replace '(?m)^', $indentation
return $formatedContent
}
# Indentation
$twoSpacesIndentation = ' '
$fourSpacesIndentation = ' '
# Modules
$fileFolderPath = if ($runLocally) { "./configuration" }else { "https://raw.githubusercontent.com/robert-cpl/winget-dsc/main/configuration" }
$headerContent = GetContent -filePath "$fileFolderPath/modules/header.yaml" -runLocally $runLocally
$footerContent = GetContent -filePath "$fileFolderPath/modules/footer.yaml" -indentation $twoSpacesIndentation -runLocally $runLocally
$finishersContent = GetContent -filePath "$fileFolderPath/modules/finishers.yaml" -indentation $fourSpacesIndentation -runLocally $runLocally
# DSC's
$sharedConfigContent = GetContent -filePath "$fileFolderPath/shared.yaml" -indentation $fourSpacesIndentation -runLocally $runLocally
$personalConfigContent = GetContent -filePath "$fileFolderPath/personal.yaml" -indentation $fourSpacesIndentation -runLocally $runLocally
$developerConfigContent = GetContent -filePath "$fileFolderPath/developer.yaml" -indentation $fourSpacesIndentation -runLocally $runLocally
if ($dscProfile -eq $defaultDscProfile) {
Write-Host "Using $dscProfile DSC configuration." -ForegroundColor Yellow
$configTypeContent = $personalConfigContent
}
else {
Write-Host "Using $dscProfile DSC configuration." -ForegroundColor Yellow
$configTypeContent = $developerConfigContent
}
# Build the DSC configuration file
$headerContent, $sharedConfigContent, $configTypeContent, $finishersContent, $footerContent | Set-Content -Path $configurationFilePath
# Run the configuration
winget configuration --file $configurationFilePath
# Catch errors
if ($LASTEXITCODE -ne 0) {
Write-Host "Error running the configuration. See the error message above." -ForegroundColor Red
Read-Host -Prompt "Press ENTER to exit."
exit $LASTEXITCODE
}
Read-Host "Configuration completed. Press ENTER to exit."