| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Brian Scholer <@briantist> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
|
| 8 |
$spec = @{ |
| 9 |
supports_check_mode = $true |
| 10 |
options = @{ |
| 11 |
source = @{ |
| 12 |
type = 'path' |
| 13 |
default = '%LOCALAPPDATA%\Microsoft\Windows\PowerShell\PowerShellGet\PSRepositories.xml' |
| 14 |
} |
| 15 |
name = @{ |
| 16 |
type = 'list' |
| 17 |
elements = 'str' |
| 18 |
default = @( |
| 19 |
'*' |
| 20 |
) |
| 21 |
} |
| 22 |
exclude = @{ |
| 23 |
type = 'list' |
| 24 |
elements = 'str' |
| 25 |
} |
| 26 |
profiles = @{ |
| 27 |
type = 'list' |
| 28 |
elements = 'str' |
| 29 |
default = @( |
| 30 |
'*' |
| 31 |
) |
| 32 |
} |
| 33 |
exclude_profiles = @{ |
| 34 |
type = 'list' |
| 35 |
elements = 'str' |
| 36 |
default = @( |
| 37 |
'systemprofile' |
| 38 |
'LocalService' |
| 39 |
'NetworkService' |
| 40 |
) |
| 41 |
} |
| 42 |
force = @{ |
| 43 |
type = 'bool' |
| 44 |
default = $false |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 50 |
|
| 51 |
$module.Diff.before = @{} |
| 52 |
$module.Diff.after = @{} |
| 53 |
|
| 54 |
function Select-Wildcard { |
| 55 |
<# |
| 56 |
.SYNOPSIS |
| 57 |
Compares a value to an Include and Exclude list of wildcards, |
| 58 |
returning the input object if a match is found |
| 59 |
|
| 60 |
.DESCRIPTION |
| 61 |
If $Property is specified, that property of the input object is |
| 62 |
compared rather than the object itself, but the original object |
| 63 |
is returned, not the property. |
| 64 |
#> |
| 65 |
[CmdletBinding()] |
| 66 |
[OutputType([object])] |
| 67 |
param( |
| 68 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 69 |
[object] |
| 70 |
$InputObject , |
| 71 |
|
| 72 |
[Parameter()] |
| 73 |
[String] |
| 74 |
$Property , |
| 75 |
|
| 76 |
[Parameter()] |
| 77 |
[String[]] |
| 78 |
$Include , |
| 79 |
|
| 80 |
[Parameter()] |
| 81 |
[String[]] |
| 82 |
$Exclude |
| 83 |
) |
| 84 |
|
| 85 |
Process { |
| 86 |
$o = if ($Property) { |
| 87 |
$InputObject.($Property) |
| 88 |
} |
| 89 |
else { |
| 90 |
$InputObject |
| 91 |
} |
| 92 |
|
| 93 |
foreach ($inc in $Include) { |
| 94 |
$imatch = $o -like $inc |
| 95 |
if ($imatch) { |
| 96 |
break |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (-not $imatch) { |
| 101 |
return |
| 102 |
} |
| 103 |
|
| 104 |
foreach ($exc in $Exclude) { |
| 105 |
if ($o -like $exc) { |
| 106 |
return |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
$InputObject |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
function Get-ProfileDirectory { |
| 115 |
<# |
| 116 |
.SYNOPSIS |
| 117 |
Returns DirectoryInfo objects for each profile on the system, as reported by the registry |
| 118 |
|
| 119 |
.DESCRIPTION |
| 120 |
The special "Default" profile, used as a template for newly created users, is explicitly |
| 121 |
added to the list of possible profiles returned. Public is explicitly excluded. |
| 122 |
Paths reported by the registry that don't exist on the filesystem are silently skipped. |
| 123 |
#> |
| 124 |
[CmdletBinding()] |
| 125 |
[OutputType([System.IO.DirectoryInfo])] |
| 126 |
param( |
| 127 |
[Parameter()] |
| 128 |
[String[]] |
| 129 |
$Include , |
| 130 |
|
| 131 |
[Parameter()] |
| 132 |
[String[]] |
| 133 |
$Exclude |
| 134 |
) |
| 135 |
|
| 136 |
$regPL = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' |
| 137 |
|
| 138 |
# note: this is a key named "Default", not the (Default) key |
| 139 |
$default = Get-ItemProperty -LiteralPath $regPL | Select-Object -ExpandProperty Default |
| 140 |
|
| 141 |
# "ProfileImagePath" is always the local side of the profile, even if roaming profiles are used |
| 142 |
# This is what we want, because PSRepositories are stored in AppData/Local and don't roam |
| 143 |
$profiles = ( |
| 144 |
@($default) + |
| 145 |
(Get-ChildItem -LiteralPath $regPL | Get-ItemProperty | Select-Object -ExpandProperty ProfileImagePath) |
| 146 |
) -as [System.IO.DirectoryInfo[]] |
| 147 |
|
| 148 |
$profiles | |
| 149 |
Where-Object -Property Exists -EQ $true | |
| 150 |
Select-Wildcard -Property Name -Include $Include -Exclude $Exclude |
| 151 |
} |
| 152 |
|
| 153 |
function ConvertTo-ComparableObject { |
| 154 |
[CmdletBinding()] |
| 155 |
param ( |
| 156 |
[Parameter(Mandatory)] |
| 157 |
[object] |
| 158 |
$InputObject |
| 159 |
) |
| 160 |
|
| 161 |
if ($InputObject -is [System.Collections.IDictionary]) { |
| 162 |
# Using a sorted dictionary to ensure consistent key ordering |
| 163 |
$dict = [System.Collections.Generic.SortedDictionary[string, object]]::new() |
| 164 |
foreach ($kvp in $InputObject.GetEnumerator()) { |
| 165 |
$dict[$kvp.Key] = ConvertTo-ComparableObject -InputObject $kvp.Value |
| 166 |
} |
| 167 |
$dict |
| 168 |
} |
| 169 |
elseif ($InputObject -is [System.Collections.IList]) { |
| 170 |
# Leading , ensures the output is an array and isn't unrolled. |
| 171 |
, @( |
| 172 |
foreach ($item in $InputObject) { |
| 173 |
ConvertTo-ComparableObject -InputObject $item |
| 174 |
} |
| 175 |
) |
| 176 |
} |
| 177 |
else { |
| 178 |
$InputObject |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
function Compare-Dictionary { |
| 183 |
<# |
| 184 |
.SYNOPSIS |
| 185 |
Attempts to naively compare two dictionaries |
| 186 |
#> |
| 187 |
[CmdletBinding()] |
| 188 |
[OutputType([bool])] |
| 189 |
param( |
| 190 |
[Parameter(Mandatory)] |
| 191 |
[System.Collections.IDictionary] |
| 192 |
$ReferenceObject, |
| 193 |
|
| 194 |
[Parameter(Mandatory)] |
| 195 |
[System.Collections.IDictionary] |
| 196 |
$DifferenceObject |
| 197 |
) |
| 198 |
|
| 199 |
$refDict = ConvertTo-ComparableObject -InputObject $ReferenceObject |
| 200 |
$difDict = ConvertTo-ComparableObject -InputObject $DifferenceObject |
| 201 |
|
| 202 |
$sRef = ConvertTo-Json -InputObject $refDict -Compress |
| 203 |
$sDif = ConvertTo-Json -InputObject $difDict -Compress |
| 204 |
|
| 205 |
$sRef -ceq $sDif |
| 206 |
} |
| 207 |
|
| 208 |
# load the repositories from the source file |
| 209 |
try { |
| 210 |
$src = $module.Params.source |
| 211 |
$src_repos = Import-Clixml -LiteralPath $src -ErrorAction Stop |
| 212 |
} |
| 213 |
catch [System.IO.FileNotFoundException] { |
| 214 |
$module.FailJson("The source file '$src' was not found.", $_) |
| 215 |
} |
| 216 |
catch { |
| 217 |
$module.FailJson("There was an error loading the source file '$src': $($_.Exception.Message).", $_) |
| 218 |
} |
| 219 |
|
| 220 |
$profiles = Get-ProfileDirectory -Include $module.Params.profiles -Exclude $module.Params.exclude_profiles |
| 221 |
|
| 222 |
foreach ($user in $profiles) { |
| 223 |
$username = $user.Name |
| 224 |
|
| 225 |
$repo_dir = $user.FullName | Join-Path -ChildPath 'AppData\Local\Microsoft\Windows\PowerShell\PowerShellGet' |
| 226 |
$repo_path = $repo_dir | Join-Path -ChildPath 'PSRepositories.xml' |
| 227 |
|
| 228 |
if (Test-Path -LiteralPath $repo_path) { |
| 229 |
$cur_repos = Import-Clixml -LiteralPath $repo_path |
| 230 |
} |
| 231 |
else { |
| 232 |
$cur_repos = @{} |
| 233 |
} |
| 234 |
|
| 235 |
$updated = $false |
| 236 |
if ($module.Params.force) { |
| 237 |
$new_repos = $src_repos.Clone() |
| 238 |
$updated = $true |
| 239 |
} |
| 240 |
else { |
| 241 |
$new_repos = $cur_repos.Clone() |
| 242 |
$src_repos.Keys | |
| 243 |
Select-Wildcard -Include $module.Params.name -Exclude $module.Params.exclude | |
| 244 |
ForEach-Object -Process { |
| 245 |
# explicit scope used inside ForEach-Object to satisfy lint (PSUseDeclaredVarsMoreThanAssignment) |
| 246 |
# see https://github.com/PowerShell/PSScriptAnalyzer/issues/827 |
| 247 |
$Script:updated = $true |
| 248 |
$Script:new_repos[$_] = $Script:src_repos[$_] |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
$module.Diff.before[$username] = $cur_repos |
| 253 |
$module.Diff.after[$username] = $new_repos |
| 254 |
|
| 255 |
if ($updated -and -not (Compare-Dictionary -ReferenceObject $cur_repos -DifferenceObject $new_repos)) { |
| 256 |
if (-not $module.CheckMode) { |
| 257 |
$null = New-Item -Path $repo_dir -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 258 |
$new_repos | Export-Clixml -LiteralPath $repo_path -Force |
| 259 |
} |
| 260 |
$module.Result.changed = $true |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
$module.ExitJson() |