publish/Lib.psm1

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<#
This module contains library cmdlets for Genesis
#>


$script:RestartNeeded = $false

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$Browsers = @{
    "Chrome" = @{
        LocalPath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
        ChocolateyPackage = "googlechrome"
        Tag = "ChromeHTML"
    }
    "Firefox" = @{
        LocalPath = "C:\Program Files\Mozilla Firefox\firefox.exe"
        ChocolateyPackage = "firefox"
        Tag = "FirefoxURL"
    }
    "Edge" = @{
        Tag = "MSEdgeHTM"
    }
    "InternetExplorer" = @{
        Tag = "IE.HTTP"
    }
}

$SpecialFolders = @{
    "Desktop" = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"
    "Documents" = "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"
    "Downloads" = "{374DE290-123F-4565-9164-39C4925E467B}"
    "Favorites" = "{1777F761-68AD-4D8A-87BD-30B759FA33DD}"
    "ProgramData" = "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}"
}

function Get-Browser {
    param(
        $Name
    )
    $browser = $Browsers[$Name]
    if ($browser -eq $null) {
        throw "Invalid browser name: $Name"
    }
    return $browser
}

function Set-RestartNeeded {
    $script:RestartNeeded = $true
}

function Get-RestartNeeded {
    return $script:RestartNeeded
}

function Assert-SpecialFolder {
    param(
        $Name,
        $PreferredLocation
    )
    Write-Debug "Checking special folder $Name for $PreferredLocation"
    if (!(Test-Path $PreferredLocation)) {
        Write-Output "Creating new $Name folder at: $PreferredLocation"
        mkdir $PreferredLocation
    }
    $regName = $SpecialFolders[$Name]
    Write-Progress " $Name..."
    Assert-SpecialFolderPath -Name $regName -FolderPath $PreferredLocation
}

function Assert-NoKeepAwake {
    param(
        $Process,
        $Mode
    )
    $args = @("/requestsoverride", "PROCESS", $Process, $Mode)
    Write-Debug "Running powercfg $args"
    # it's harmless to redo this configuration each time so we don't
    # necessarily check for existing config values
    & powercfg $args
}

function Assert-RegistryValue {
    param(
        $Path,
        $Name,
        $Type,
        $Value
    )
    if ($null -eq $Value) {
        # skip this if relevant configuration information is missing
        # therefore the function is called unnecessarily
        Write-Warning "Missing configuration option for registry $Path\$Name"
        return
    }
    Write-Debug "Checking registry path $Path"
    if (!(Test-Path $Path)) {
        Write-Debug "Creating registry path $Path"
        New-Item -Path $Path -Force
    }
    try {
        Write-Debug "Getting registry property $Name from $Path"
        $prop = (Get-ItemProperty -Path $Path | Select-Object -ExpandProperty $Name)
    } catch {
        Write-Debug "Error getting registry property $Name from $Path"
        $prop = $null
    }
    if ($prop -ne $Value) {
        Write-Progress "updating..."
        Set-ItemProperty -Path $Path -Name $Name -Type $Type -Value $Value
    }
}

function Assert-SpecialFolderPath {
    param(
        $Name,
        $FolderPath
    )
    return (Assert-RegistryValue `
        -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" `
        -Name $Name -Type ExpandString -Value $FolderPath)
}

function Assert-StoreAppsInstalled {
    param(
        $Apps
    )
    if ($null -eq $Apps) {
        Write-Warning "Store apps config not found"
        return
    }
    foreach ($name in $Apps.Keys) {
        Write-Progress " $name..."
        $item = (Get-AppxPackage | Where-Object { $_.Name -eq $name })
        if ($null -eq $item) {
            $productId = $Apps[$name]
            Start-Process "https://www.microsoft.com/store/productId/$productId"
        }
    }
}

function Assert-DesktopShortcut {
    param(
        $Name,
        $Url
    )
    $desktop = Get-DesktopPath
    $filename = (Join-Path $desktop $Name)
    if (!(Test-Path $filename)) {
        Write-Progress "downloading..."
        Invoke-WebRequest -Uri $Url -OutFile $filename
    }
    Write-Progress "nice..."
}

function Assert-ChocolateyPackages {
    param(
        [string[]]$Packages
    )
    $list = choco list --id-only --local-only --limit-output
    [System.Collections.Generic.HashSet[string]]$installedPackages = $list
    foreach ($name in $Packages) {
        Write-Progress "$name..."
        if ($installedPackages -notcontains $name) {
            Write-Progress "installing"
            & choco install $Name -y
        }
    }
}

function Assert-WindowsFeature {
    param(
        $Name
    )
    $feature = Get-WindowsOptionalFeature -FeatureName $Name -Online
    if ($feature -and ($feature.State -eq "Disabled"))
    {
        Write-Progress "enabling $Name..."
        Enable-WindowsOptionalFeature -FeatureName $Name -Online -All -NoRestart
    }
}

function Assert-WindowsCapability {
    param(
        $Name,
        $Id
    )
    $state = (Get-WindowsCapability -Online -Name $Id).State
    if ($state -ne 'Installed') {
        Write-Progress "installing $Name..."
        Add-WindowsCapability -Online -Name $Id
    }
}

function Test-DefaultBrowser {
    param (
        $Tag
    )
    return (Get-ItemPropertyValue `
        "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" ProgId) `
        -like "$Tag*"
}

function Assert-Configuration {
    param (
        $Name,
        $Script
    )
    Write-Debug "Setting up $Name"
    Write-Progress $Name
    & $Script
}

function Set-RecycleBinCapacity {
    param(
        $Volume,
        $Capacity
    )
    Assert-RegistryValue `
        -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Bitbucket\Volume\$Volume" `
        -Name MaxCapacity -Type Dword -Value $Capacity
}

function Get-DesktopPath {
    return [Environment]::GetFolderPath("Desktop")
}