# properties to exclude (add or remove as needed) $blacklist = 'FileList', 'Tags'
$data = Invoke-RestMethod-Uri$url-UseBasicParsing | ForEach-Object { $hash = [Ordered]@{} $moduleInfo = $_.Properties foreach($_in$moduleInfo.PSObject.Properties) { # name of property $name = $_.Name # if it is in blacklist, skip and continue with next property if ($name-in$blacklist) { continue } # if it is the property "name", then skip # all remaining (xml default properties) if ($name-eq'Name') { break }
# if type is "xmlelement", retrieve underlying text value in #text if ($_.TypeNameOfValue -eq'System.Xml.XmlElement') { $hash[$name] = $moduleInfo.$name.'#text'
# if a datatype is assigned, try and convert to appropriate type if ($moduleInfo.$name.type -like'Edm.*') { $typename = $moduleInfo.$name.type.replace('Edm.','') $hash[$name] = $hash[$name] -as$typename } } else { $hash[$name] = $_.Value } }
# convert a hash table to object and return it [PSCustomObject]$hash }
# determine the primary module location for your PowerShell version $path = if ('Management.Automation.Platform'-as [Type]) { # PowerShell CLR if ([Environment]::OSVersion.Platform -like'Win*'-or$IsWindows) { # on Windows Join-Path-Path$env:ProgramFiles-ChildPath'PowerShell' } else { # on Non-Windows $name = [Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES') Split-Path-Path$name-Parent } } else { # Windows PowerShell Join-Path-Path$env:ProgramFiles-ChildPath"WindowsPowerShell" }
在 Windows 上,PowerShell 7 和 Windows PowerShell 可以共享一个文件夹,因此如果您不想专门为 PowerShell 7 部署模块,则可以进一步简化脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# determine the primary module location for your PowerShell version $path = if ([Environment]::OSVersion.Platform -like'Win*'-or$IsWindows) { # Windows Join-Path-Path$env:ProgramFiles-ChildPath"WindowsPowerShell" } else { # Non-Windows $name = [Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES') Split-Path-Path$name-Parent }