| 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 |
#Requires -Module Ansible.ModuleUtils.CamelConversion |
| 8 |
#Requires -Module PowerShellGet |
| 9 |
|
| 10 |
$spec = @{ |
| 11 |
options = @{ |
| 12 |
name = @{ type = 'str' ; default = '*' } |
| 13 |
repository = @{ type = 'str' } |
| 14 |
} |
| 15 |
supports_check_mode = $true |
| 16 |
} |
| 17 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 18 |
|
| 19 |
# We need to remove this type data so that arrays don't get serialized weirdly. |
| 20 |
# In some cases, an array gets serialized as an object with a Count and Value property where the value is the actual array. |
| 21 |
# See: https://stackoverflow.com/a/48858780/3905079 |
| 22 |
Remove-TypeData System.Array |
| 23 |
|
| 24 |
function Convert-ObjectToSnakeCase { |
| 25 |
<# |
| 26 |
.SYNOPSIS |
| 27 |
Converts an object with CamelCase properties to a dictionary with snake_case keys. |
| 28 |
Works in the spirit of and depends on the existing CamelConversion module util. |
| 29 |
#> |
| 30 |
[CmdletBinding()] |
| 31 |
param( |
| 32 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 33 |
[OutputType([System.Collections.Specialized.OrderedDictionary])] |
| 34 |
[Object] |
| 35 |
$InputObject , |
| 36 |
|
| 37 |
[Parameter()] |
| 38 |
[Switch] |
| 39 |
$NoRecurse , |
| 40 |
|
| 41 |
[Parameter()] |
| 42 |
[Switch] |
| 43 |
$OmitNull |
| 44 |
) |
| 45 |
|
| 46 |
Process { |
| 47 |
$result = [Ordered]@{} |
| 48 |
foreach ($property in $InputObject.PSObject.Properties) { |
| 49 |
$value = $property.Value |
| 50 |
if (-not $NoRecurse -and $value -is [System.Collections.IDictionary]) { |
| 51 |
$value = Convert-DictToSnakeCase -dict $value |
| 52 |
} |
| 53 |
elseif (-not $NoRecurse -and ($value -is [Array] -or $value -is [System.Collections.ArrayList])) { |
| 54 |
$value = Convert-ListToSnakeCase -list $value |
| 55 |
} |
| 56 |
elseif ($null -eq $value) { |
| 57 |
if ($OmitNull) { |
| 58 |
continue |
| 59 |
} |
| 60 |
} |
| 61 |
elseif (-not $NoRecurse -and $value -isnot [System.ValueType] -and $value -isnot [string]) { |
| 62 |
$value = Convert-ObjectToSnakeCase -InputObject $value |
| 63 |
} |
| 64 |
|
| 65 |
$name = Convert-StringToSnakeCase -string $property.Name |
| 66 |
$result[$name] = $value |
| 67 |
} |
| 68 |
$result |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
function ConvertTo-SerializableModuleInfo { |
| 73 |
<# |
| 74 |
.SYNOPSIS |
| 75 |
Transforms some members of a ModuleInfo object to be more serialization-friendly and prevent infinite recursion. |
| 76 |
|
| 77 |
.DESCRIPTION |
| 78 |
Stringifies version properties so we don't get serialized [System.Version] objects which aren't very useful. |
| 79 |
|
| 80 |
ExportedCommands and some other Exported* members are problematic in a few ways. |
| 81 |
For one, they contain a reference to the full ModuleInfo object they are found in. This is probably useful |
| 82 |
for nested modules and such, but in most cases they are just a reference to the current module, and this will |
| 83 |
recurse infinitely. |
| 84 |
|
| 85 |
Further, the rest of the properties of the exported commands aren't needed for this module. A low depth on JSON |
| 86 |
conversion doesn't fully work because some other module fields need it deeper than 1, and since this is a |
| 87 |
dictionary we get a JSOn object whose property names and values are both just the name of the command. |
| 88 |
|
| 89 |
So instead we just make an array of the names, as that's good enough for what we want to return. |
| 90 |
|
| 91 |
NestedModules and RequiredModules are full ModuleInfo objects so that becomes a recursive call. |
| 92 |
Limiting depth or reference counting shouldn't be necessary because the entries aren't references; they |
| 93 |
have to actually exist. |
| 94 |
|
| 95 |
ModuleList gets recursed as well even though it's a tiny subset of fields, to transform versions and to |
| 96 |
convert to snake_case. |
| 97 |
|
| 98 |
PrivateData can contain any data but a module manifest is a static file that can't contain references or |
| 99 |
problematic types like [Type]. Unfortunately some module types like CIM (and presumably binary?) seem to be |
| 100 |
able to populate that with whatever they want. |
| 101 |
|
| 102 |
As a precaution then, for module type that is not Script or Manifest, we limit PrivateData to a shallow depth. |
| 103 |
#> |
| 104 |
[CmdletBinding()] |
| 105 |
param( |
| 106 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 107 |
[Object] |
| 108 |
$InputObject , |
| 109 |
|
| 110 |
[Parameter()] |
| 111 |
[string[]] |
| 112 |
$ExcludeProperty = @( |
| 113 |
<# |
| 114 |
Definition is the script that makes up the script module. |
| 115 |
Big to return from this module, but also it doesn't ever seem to be filled in |
| 116 |
in the data from Get-Module -ListAvailable. |
| 117 |
#> |
| 118 |
'Definition', |
| 119 |
<# |
| 120 |
OnRemove is a script that gets run before the module is removed. |
| 121 |
Not necessary for return. |
| 122 |
#> |
| 123 |
'OnRemove', |
| 124 |
<# |
| 125 |
For binary modules, ImplementingAssembly is a reference to the assembly. |
| 126 |
No use returning it. |
| 127 |
#> |
| 128 |
'ImplementingAssembly', |
| 129 |
<# |
| 130 |
The session state instance of a loaded module. It's a runtime property only returned |
| 131 |
without -ListAvailable and has no value serialized. |
| 132 |
#> |
| 133 |
'SessionState' |
| 134 |
) |
| 135 |
) |
| 136 |
|
| 137 |
Process { |
| 138 |
$properties = foreach ($p in $InputObject.PSObject.Properties) { |
| 139 |
$pName = $p.Name |
| 140 |
$pValue = $p.Value |
| 141 |
|
| 142 |
switch -Regex ($pName) { |
| 143 |
'^PrivateData$' { |
| 144 |
if ( |
| 145 |
$InputObject.ModuleType -notin @( |
| 146 |
[System.Management.Automation.ModuleType]::Script, |
| 147 |
[System.Management.Automation.ModuleType]::Manifest |
| 148 |
) |
| 149 |
) { |
| 150 |
$safeVal = $pValue | ConvertTo-Json -Depth 1 | ConvertFrom-Json |
| 151 |
@{ |
| 152 |
Name = $pName |
| 153 |
Expression = { $safeVal }.GetNewClosure() |
| 154 |
} |
| 155 |
} |
| 156 |
else { |
| 157 |
$pName |
| 158 |
} |
| 159 |
break |
| 160 |
} |
| 161 |
'^(?:NestedModules|ModuleList|RequiredModules)$' { |
| 162 |
# Nested and Required modules are full moduleinfo objects |
| 163 |
# ModuleList isn't but its simplified fields need much of the same treatment |
| 164 |
if ($pValue) { |
| 165 |
@{ |
| 166 |
Name = $pName |
| 167 |
Expression = { |
| 168 |
@(, ($pValue | ConvertTo-SerializableModuleInfo | Convert-ObjectToSnakeCase -NoRecurse)) |
| 169 |
}.GetNewClosure() |
| 170 |
} |
| 171 |
} |
| 172 |
else { |
| 173 |
$pName |
| 174 |
} |
| 175 |
break |
| 176 |
} |
| 177 |
'Version$' { |
| 178 |
@{ |
| 179 |
Name = $pName |
| 180 |
Expression = { $pValue.ToString() }.GetNewClosure() |
| 181 |
} |
| 182 |
break |
| 183 |
} |
| 184 |
'^Exported' { |
| 185 |
@{ |
| 186 |
Name = $pName |
| 187 |
Expression = { @(, $pValue.Keys) }.GetNewClosure() |
| 188 |
} |
| 189 |
break |
| 190 |
} |
| 191 |
default { |
| 192 |
if ($pValue -is [datetime]) { |
| 193 |
@{ |
| 194 |
Name = $pName |
| 195 |
Expression = { $pValue.ToString('o') }.GetNewClosure() |
| 196 |
} |
| 197 |
} |
| 198 |
elseif ($pValue -is [enum] -or $pValue -is [type]) { |
| 199 |
@{ |
| 200 |
Name = $pName |
| 201 |
Expression = { $pValue.ToString() }.GetNewClosure() |
| 202 |
} |
| 203 |
} |
| 204 |
else { |
| 205 |
$pName |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
$InputObject | Select-Object -Property $properties -ExcludeProperty $ExcludeProperty |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
function Add-ModuleRepositoryInfo { |
| 216 |
<# |
| 217 |
.SYNOPSIS |
| 218 |
Takes a ModuleInfo object and adds some info that came from PowerShellGet |
| 219 |
|
| 220 |
.DESCRIPTION |
| 221 |
Checks if there's information from Get-InstalledModule about the current module, |
| 222 |
and if so adds to it to ModuleInfo object. The fields are always added, with null |
| 223 |
values if need be. |
| 224 |
#> |
| 225 |
[CmdletBinding()] |
| 226 |
param( |
| 227 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 228 |
[System.Object] |
| 229 |
$InputObject , |
| 230 |
|
| 231 |
[Parameter()] |
| 232 |
[String] |
| 233 |
$RepositoryName |
| 234 |
) |
| 235 |
|
| 236 |
Begin { |
| 237 |
$wantedProperties = @( |
| 238 |
'PublishedDate', |
| 239 |
'InstalledDate', |
| 240 |
'UpdatedDate', |
| 241 |
'Dependencies', |
| 242 |
'Repository', |
| 243 |
'PackageManagementProvider', |
| 244 |
'InstalledLocation', |
| 245 |
'RepositorySourceLocation', |
| 246 |
'Tags', |
| 247 |
'CompatiblePSEditions', |
| 248 |
'LicenseUri', |
| 249 |
'ProjectUri', |
| 250 |
'IconUri', |
| 251 |
'ReleaseNotes', |
| 252 |
'ExportedDscResources', # ExportedDscResources is not returned here, this is a hack for Windows 2012/R2 to ensure the field is present |
| 253 |
'Prefix' # Prefix is not actually returned here, this is a hack for Windows 2012 just to ensure the field is present |
| 254 |
) |
| 255 |
|
| 256 |
# Get all the installed modules at once. This prevents us from having to make an expensive individual call for every |
| 257 |
# local module, the vast majority of which didn't come from PowerShellGet. |
| 258 |
# The results here won't contain every version, but that's ok because we'll still have a fast signal as to whether |
| 259 |
# it makes sense to make a version-specific call for the individual module. |
| 260 |
$installedModules = Get-InstalledModule -ErrorAction SilentlyContinue | Group-Object -Property Name -AsHashTable -AsString |
| 261 |
} |
| 262 |
|
| 263 |
Process { |
| 264 |
$moduleName = $InputObject.Name |
| 265 |
|
| 266 |
$installed = if ($installedModules -and $installedModules.Contains($moduleName)) { |
| 267 |
# we know at least one version of this module was installed from PowerShellGet |
| 268 |
# if the version of this local modle matches what we got it in the initial installed module list |
| 269 |
# use it |
| 270 |
if ($installedModules[$moduleName].Version -eq $InputObject.Version) { |
| 271 |
$installedModules[$moduleName] |
| 272 |
} |
| 273 |
else { |
| 274 |
# make an individual call to see if this specific version of the local module was installed from PowerShellGet |
| 275 |
$im = Get-InstalledModule -Name $InputObject.Name -RequiredVersion $InputObject.Version -AllowPrerelease -ErrorAction SilentlyContinue |
| 276 |
if ($im) { |
| 277 |
$im |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if ($RepositoryName -and $installed.Repository -ne $RepositoryName) { |
| 283 |
return |
| 284 |
} |
| 285 |
|
| 286 |
$members = @{} |
| 287 |
$wantedProperties | ForEach-Object -Process { |
| 288 |
if (-not $InputObject.$_) { |
| 289 |
# if the fields are present in both places, let's prefer what's sent in |
| 290 |
$members[$_] = $installed.$_ |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
$InputObject | Add-Member -NotePropertyMembers $members -Force -PassThru |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
$module.Result.modules = @( |
| 299 |
Get-Module -ListAvailable -Name $module.Params.name | |
| 300 |
Add-ModuleRepositoryInfo -RepositoryName $module.Params.repository | |
| 301 |
ConvertTo-SerializableModuleInfo | |
| 302 |
Convert-ObjectToSnakeCase -NoRecurse |
| 303 |
) |
| 304 |
|
| 305 |
$module.ExitJson() |