Guest User

Get-RemoteRegistryKeyProperty.ps1

a guest
Jul 25th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##############################################################################
  2. ##
  3. ## Get-RemoteRegistryKeyProperty.ps1
  4. ##
  5. ## Get the value of a remote registry key property
  6. ##
  7. ## ie:
  8. ##
  9. ##  PS >$registryPath =
  10. ##       "HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
  11. ##  PS >Get-RemoteRegistryKeyProperty LEE-DESK $registryPath "ExecutionPolicy"
  12. ##
  13. ##############################################################################
  14.  
  15. param(
  16.   $ComputerName = $(Throw "Please specify a ComputerName name."),
  17.   $path = $(Throw "Please specify a registry path"),
  18.   $property = "*"
  19.   )
  20.  
  21. ## Validate and extract out the registry key
  22. if($path -match "^HKLM:\\(.*)")
  23. {
  24.     $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
  25.         "LocalMachine", $ComputerName)
  26. }
  27. elseif($path -match "^HKCU:\\(.*)")
  28. {
  29.     $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
  30.         "CurrentUser", $ComputerName)
  31. }
  32. else
  33. {
  34.     Write-Error ("Please specify a fully-qualified registry path " +
  35.         "(i.e.: HKLM:\Software) of the registry key to open.")
  36.     return
  37. }
  38.  
  39. ## Open the key
  40. $key = $baseKey.OpenSubKey($matches[1])
  41. $returnObject = New-Object PsObject
  42.  
  43. ## Go through each of the properties in the key
  44. foreach($keyProperty in $key.GetValueNames())
  45. {
  46.     ## If the property matches the search term, add it as a
  47.     ## property to the output
  48.     if($keyProperty -like $property)
  49.     {
  50.         $returnObject |
  51.             Add-Member NoteProperty $keyProperty $key.GetValue($keyProperty)
  52.     }
  53. }
  54.  
  55. ## Return the resulting object
  56. $returnObject
  57.  
  58. ## Close the key and base keys
  59. $key.Close()
  60. $baseKey.Close()
Advertisement
Add Comment
Please, Sign In to add comment