Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Synopsis
- Get the registry key values from remote computers.
- .Description
- This script helps you to get the get details of a given registry key from multiple remote computers.
- This script will give the if the registry exists or not, it's value and type of valus.
- .Parameter ComputerName
- How to run:
- 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
- #>
- [cmdletbinding()]
- param (
- [parameter(Mandatory=$true)]
- [String]$RegistryKey,
- [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
- [String[]]$ComputerName = $env:ComputerName,
- [parameter(Mandatory=$true)]
- [String]$KeyProperty
- )
- Begin {
- $RegistryKey -match "(?<BaseKey>\w+)\\(?<SubKey>.+)" | Out-Null
- switch($Matches.BaseKey) {
- "HKEY_LOCAL_MACHINE" {$BaseKey = "LocalMachine"}
- "HKEY_USERS" {$BaseKey = "Users"}
- "HKEY_CLASSES_ROOT" {$BaseKey = "ClassesRoot"}
- "HKEY_CURRENT_USER" {$BaseKey = "CurrentUser"}
- "HKEY_CURRENT_CONFIG" {$BaseKey = "CurrentConfig"}
- default {
- write-host "Unable to determine base key type. Exiting"
- exit(1)
- }
- }
- }
- <# Checks if the computer is reachable #>
- process {
- foreach($Computer in $ComputerName) {
- $CompName = $Computer.ToUpper()
- $Computer = $Computer.ToUpper()
- if(Test-Connection -ComputerName $Computer -count 1 -ea 0) {
- $flag = $false
- try {
- $BaseKeyObj = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($BaseKey, $computer)
- $KeyObj = $BaseKeyObj.OpenSubKey($Matches.SubKey)
- if($KeyObj) {
- foreach ($Property in $KeyObj.GetValueNames()) {
- if($KeyProperty -contains $Property) {
- $flag = $true
- }
- }
- } else {
- Write-Verbose "$($Matches.SubKey) not found on $Computer"
- }
- } catch {
- Write-Verbose "Failed to query the registry on Computer: $Computer"
- }
- $PropertyType=""
- $PropertyValue=""
- if($flag) {
- $PropertyType = $KeyObj.GetValueKind($KeyProperty)
- $PropertyValue = $KeyObj.GetValue($KeyProperty)
- }
- $OutputObj = New-Object PSObject -Prop (
- @{
- ComputerName = $Computer.ToUpper()
- PropertyValue = $PropertyValue
- IsPresent = $flag
- })
- <# Logical Operators. Use this to create CSVs of computer names based on what their key contains #>
- <# This will create the CSV the user defined in the execution command #>
- if(($PropertyValue -like "*Bad*") -or ($PropertyValue -like "*Wrong*")){
- $OutputObj
- }
- <# These next three create additional CSV's, separate from the primary command #>
- elseif($PropertyValue -like "*Correct*"){
- Write-Verbose "$Computer is good"
- Export-CSV -NoTypeInformation -append -inputobject $OutputObj -path C:\CorrectReg.csv
- }
- else{
- Write-Verbose "Unable to verify Key on $Computer - Check Manually"
- Export-CSV -NoTypeInformation -append -inputobject $OutputObj -path C:\UnknownSN.csv
- }
- } else {
- Write-Verbose "$Computer is not reachable"
- $CompName = New-Object PSObject -Prop (
- @{
- ComputerName = $Computer.ToUpper()
- })
- $CompName | Export-CSV -NoTypeInformation -append -path C:\Unreachable.csv
- }
- }
- }
- end {
- }
Advertisement
Add Comment
Please, Sign In to add comment