Guest User

Verify, Audit, and/or Export Remote Registry Key Values

a guest
Sep 4th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .Synopsis
  3.         Get the registry key values from remote computers.
  4.     .Description
  5.         This script helps you to get the get details of a given registry key from multiple remote computers.
  6.         This script will give the if the registry exists or not, it's value and type of valus.
  7.     .Parameter ComputerName
  8.    
  9.     How to run:
  10.     Get-Content C:\ListofComputers.csv | .\ThisScript.ps1 -RegistryKey "HKEY_LOCAL_MACHINE\key\you're\looking\for" -     KeyProperty Nameof| Export-CSV -NoTypeInformation -append -path C:\WrongSNRegs.csv
  11. #>
  12.  
  13. [cmdletbinding()]
  14. param (
  15.     [parameter(Mandatory=$true)]
  16.     [String]$RegistryKey,
  17.     [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
  18.     [String[]]$ComputerName = $env:ComputerName,
  19.     [parameter(Mandatory=$true)]
  20.     [String]$KeyProperty
  21. )
  22.  
  23. Begin  {
  24.     $RegistryKey -match "(?<BaseKey>\w+)\\(?<SubKey>.+)" | Out-Null
  25.  
  26.     switch($Matches.BaseKey) {
  27.         "HKEY_LOCAL_MACHINE"    {$BaseKey = "LocalMachine"}
  28.         "HKEY_USERS"            {$BaseKey = "Users"}
  29.         "HKEY_CLASSES_ROOT"     {$BaseKey = "ClassesRoot"}
  30.         "HKEY_CURRENT_USER"     {$BaseKey = "CurrentUser"}
  31.         "HKEY_CURRENT_CONFIG"   {$BaseKey = "CurrentConfig"}
  32.         default {
  33.             write-host "Unable to determine base key type. Exiting"
  34.             exit(1)
  35.         }
  36.     }
  37. }
  38.  
  39. <# Checks if the computer is reachable #>
  40. process {
  41.     foreach($Computer in $ComputerName) {
  42.         $CompName = $Computer.ToUpper()
  43.         $Computer = $Computer.ToUpper()
  44.         if(Test-Connection -ComputerName $Computer -count 1 -ea 0) {
  45.             $flag = $false
  46.             try {
  47.                 $BaseKeyObj = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($BaseKey, $computer)
  48.                 $KeyObj = $BaseKeyObj.OpenSubKey($Matches.SubKey)
  49.                 if($KeyObj) {
  50.                     foreach ($Property in $KeyObj.GetValueNames()) {
  51.                         if($KeyProperty -contains $Property) {
  52.                             $flag = $true
  53.                         }
  54.  
  55.                     }
  56.                 } else {
  57.                     Write-Verbose "$($Matches.SubKey) not found on $Computer"
  58.                 }
  59.  
  60.             } catch {
  61.                
  62.                 Write-Verbose "Failed to query the registry on Computer: $Computer"
  63.             }
  64.  
  65.             $PropertyType=""
  66.             $PropertyValue=""
  67.             if($flag) {
  68.                 $PropertyType  = $KeyObj.GetValueKind($KeyProperty)
  69.                 $PropertyValue = $KeyObj.GetValue($KeyProperty)
  70.             }
  71.             $OutputObj = New-Object PSObject -Prop (
  72.             @{
  73.                 ComputerName = $Computer.ToUpper()
  74.                 PropertyValue = $PropertyValue
  75.                         IsPresent = $flag
  76.                
  77.             })
  78.  
  79. <# Logical Operators. Use this to create CSVs of computer names based on what their key contains #>
  80.  
  81. <# This will create the CSV the user defined in the execution command #>
  82. if(($PropertyValue -like "*Bad*") -or ($PropertyValue -like "*Wrong*")){
  83.             $OutputObj
  84. }
  85. <# These next three create additional CSV's, separate from the primary command #>
  86. elseif($PropertyValue -like "*Correct*"){  
  87.     Write-Verbose "$Computer is good"
  88.     Export-CSV -NoTypeInformation -append -inputobject $OutputObj -path C:\CorrectReg.csv
  89. }
  90. else{
  91.     Write-Verbose "Unable to verify Key on $Computer - Check Manually"
  92.     Export-CSV -NoTypeInformation -append -inputobject $OutputObj -path C:\UnknownSN.csv
  93.         }
  94. } else {
  95.             Write-Verbose "$Computer is not reachable"
  96.             $CompName = New-Object PSObject -Prop (
  97.             @{
  98.                 ComputerName = $Computer.ToUpper()
  99.             })
  100.             $CompName | Export-CSV -NoTypeInformation -append -path C:\Unreachable.csv
  101.         }
  102.     }
  103. }
  104. end {
  105. }
Advertisement
Add Comment
Please, Sign In to add comment