Public/Get-ALHRegistryItem.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 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
<#PSScriptInfo
.VERSION 1.2.0 .GUID 340f3e43-7088-4966-9f8a-b79416ec5b64 .AUTHOR Dieter Koch .COMPANYNAME .COPYRIGHT (c) 2021-2023 Dieter Koch .TAGS .LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE .PROJECTURI https://github.com/admins-little-helper/ALH .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES 1.0.0 - Initial release 1.1.0 - Renamed script/function - Fixed issue with computername in case no computername was specified 1.1.1 - Changed what results are returned in case a ValueName was specified 1.1.2 - Removed parameter 'RegistryHive' - Fixed typo - Fixed issue with path names in recursion 1.1.3 - Fixed issue of incorrect handling of root path 1.1.4 - Added error handling 1.2.0 - Changed name of custom type ALHRegistryItem #> <# .DESCRIPTION Function to read registry keys and values from local or remote computers #> function Get-ALHRegistryItem { <# .SYNOPSIS Read Registry values from local or remote computer. .DESCRIPTION The function allows to list and read registry keys and values from local or remote computer(s). .PARAMETER ComputerName Optional. Name of a computer. If no name is sepcified, the command runs agains the local computer. Multiple Names can be specified as comma separated strings. .PARAMETER Path Mandatory. Registry Path to query. .PARAMETER ValueName Optional. Name of a registry value to query. If ommitted, all values under a key will be returned. .PARAMETER RegistryView Optional. Specifies which registry view to target on a 64-bit operating system. See https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.registryview?view=net-6.0 for details .PARAMETER Recurse Optional. Run query recursively (query sub keys). .EXAMPLE Get-ALHRegistryItem -ComputerName Computer1 -Path "HKLM:\SOFTWARE\Policies\Microsoft\office\16.0\common" -Recurse -Verbose Get all registry subkeys and values recursively under "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\office\16.0\common" from Computer "Computer1" .EXAMPLE (Get-ADComputer -SearchBase "OU=myOU,DC=myDomain,DC=tld" -Filter *).Name | Get-ALHRegistryItem -Path "HKLM:\SOFTWARE\Policies\Microsoft\office\16.0\common\officeupdate" -ValueName "enableautomaticupdates" Get all registry subkeys and values recursively under "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\office\16.0\common\officeupdate" from Computer "Computer1". .INPUTS String .OUTPUTS Nothing .NOTES Author: Dieter Koch Email: diko@admins-little-helper.de .LINK https://github.com/admins-little-helper/ALH/blob/main/Help/Get-ALHRegistryItem.txt #> [CmdletBinding()] param ( [parameter(ValueFromPipeline)] [String[]] $ComputerName = $env:COMPUTERNAME, [Parameter(Mandatory)] [String] $Path, [String] $ValueName, [ValidateSet('Default', 'Registry32', 'Registry64')] [String] $RegistryView = 'Default', [Switch] $Recurse, [switch] $SkipConnectionTest ) begin { $IsRecursive = (Get-PSCallStack)[1].Command -eq $MyInvocation.MyCommand $Hive = "" if ($Path -match "\\") { $Hive = $Path.Substring(0, $Path.IndexOf("\")) $Path = $Path.Substring($Path.IndexOf("\") + 1) } else { $Hive = $Path $Path = "" } $RegistryHiveList = @{ "HKCR" = @{ "ShortName" = "HKCR:" "HiveName" = "ClassesRoot" } "HKCU" = @{ "ShortName" = "HKCU:" "HiveName" = "CurrentUser" } "HKLM" = @{ "ShortName" = "HKLM:" "HiveName" = "LocalMachine" } "HKU" = @{ "ShortName" = "HKU:" "HiveName" = "Users" } "HKCC" = @{ "ShortName" = "HKCC:" "HiveName" = "CurrentConfig" } } switch ($Hive) { { "HKCR:", "HKEY_CLASSES_ROOT", "ClassesRoot" -contains $_ } { $RegistryHive = $RegistryHiveList."HKCR" } { "HKCU:", "HKEY_CURRENT_USER", "CurrentUser" -contains $_ } { $RegistryHive = $RegistryHiveList."HKCU" } { "HKLM:", "HKEY_LOCAL_MACHINE", "LocalMachine" -contains $_ } { $RegistryHive = $RegistryHiveList."HKLM" } { "HKU:", "HKEY_USERS", "Users" -contains $_ } { $RegistryHive = $RegistryHiveList."HKU" } { "HKCC:", "HKEY_CURRENT_CONFIG", "CurrentConfig" -contains $_ } { $RegistryHive = $RegistryHiveList."HKCC" } default { Write-Error -Message "Registry hive not detected!" break; } } } process { foreach ($Computer in $ComputerName) { $IsComputerOnline = $false if ($IsRecursive) { Write-Debug -Message "Recusion detected. Skipping 'Test-Connection'" $IsComputerOnline = $true } else { Write-Verbose -Message "Reading values from Computer: $Computer" if ($SkipConnectionTest.IsPresent) { Write-Verbose -Message "Skipping Test-Connection" $IsComputerOnline = $true } else { Write-Verbose -Message "Checking if system is online." $IsComputerOnline = Test-Connection $Computer -Count 1 -Quiet Write-Verbose -Message "Test result: $IsComputerOnline" } } if ($IsComputerOnline) { try { $Registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($($RegistryHive.HiveName), $Computer, $RegistryView) $RegistryKey = $Registry.OpenSubKey($Path) } catch [System.Security.SecurityException] { Write-Warning -Message "Registry key does not exist or can not be read: '$($RegistryHive.ShortName)\$Path'" } catch { Write-Warning -Message "Unknown error occured reading registry key: '$($RegistryHive.ShortName)\$Path'" } if ($RegistryKey) { $Path = "$($RegistryHive.ShortName)\$Path" if ($null -eq $ValueName -or $ValueName -eq "") { $SearchValueName = "*" } else { $SearchValueName = $ValueName } [array]$RegKeys = foreach ($Subkey in $RegistryKey.GetSubKeyNames()) { $RegistryKeyObject = [PSCustomObject]@{ Computer = $Computer Path = $Path SubKey = $SubKey } $RegistryKeyObject.psobject.TypeNames.Insert(0, "ALHRegistryItem") if ($Recurse.IsPresent) { Get-ALHRegistryItem ` -ComputerName $Computer ` -Path ("$Path\$Subkey" -replace "\\\\", "\") ` -RegistryView $RegistryView ` -ValueName $SearchValueName ` -Recurse:($Recurse.IsPresent) } # Only return RegistryKeyObject if no value was specified - means we return everything # If a ValueName was specified, we only want to see results for this ValueName if ($null -eq $ValueName -or $ValueName -eq "") { $RegistryKeyObject } } [array]$RegVals = foreach ($Value in ($RegistryKey.GetValueNames().where({ $_ -like "$SearchValueName" }))) { $Data = $RegistryKey.GetValue($Value) $DataType = $RegistryKey.GetValueKind($Value) $RegistryValObject = [PSCustomObject]@{ Computer = $Computer Path = $Path Value = $Value Data = $Data DataType = $DataType } $RegistryValObject.psobject.TypeNames.Insert(0, "ALHRegistryItem") $RegistryValObject } if ($null -eq $RegVals) { if ($SearchValueName -eq "*") { Write-Verbose -Message "Registry '$Path' does not contain any values" } else { Write-Verbose -Message "Registry value '$SearchValueName' does not exits in key '$Path'" } } $RegKeys $RegVals } else { Write-Verbose -Message "Nothing found for specified key." } } else { Write-Warning -Message "Computer not reachable: $Computer" } } } } #region EndOfScript <# ################################################################################ ################################################################################ # # ______ _ __ _____ _ _ # | ____| | | / _| / ____| (_) | | # | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_ # | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __| # | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_ # |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__| # | | # |_| ################################################################################ ################################################################################ # created with help of http://patorjk.com/software/taag/ #> #endregion |