Registry Slideshow

  1. Function RegUpdate { 
  2.  
  3. param
  4.  
  5. [string]$regKey
  6. [string]$Name
  7. [string]$data
  8. [string]$type 
  9.  
  10.  
  11. If($data -eq "null")  
  12. $data = "null" 
  13. }  
  14.  
  15. Write-Host "Value Data is >>>> $data" 
  16. Write-host "Create/ Update the Registry Value" 
  17. Write-Host "Checking the machine to see $regKey" 
  18. $pattern = "(.\w+).+" 
  19. $reg = [regex]$pattern 
  20. #Write-host $reg 
  21. $value = $regkey -match $reg 
  22.     
  23.   If (( $matches[1] -eq "HKLM") -or ($matches[1] -eq "HKEY_LOCAL_MACHINE")) { 
  24.      
  25.     $old  = $matches[1]+"\" 
  26.     $updateKey = $regKey.replace($old , "HKLM:\"
  27.      
  28.   else 
  29.   
  30.    $pattern = "(.\w)+." 
  31.    $reg = [regex]$pattern 
  32.    $value = $regkey -match $reg 
  33.     
  34.     
  35.    If (( $matches[0] -eq "HKCU") -or ($matches[0] -eq "HKEY_CURRENT_USER")) { 
  36.     $old  = $matches[0]+"\" 
  37.     $updateKey = $regKey.replace($old , "HKCU:\"
  38.      
  39.     
  40.  
  41.      if(Test-Path  -Path  $updateKey) { 
  42.        Write-Host "Registry Hive found $updatekey" 
  43.        Write-Host "Checking if the Value of the RegKey is already Present" 
  44.        Write-Host "Value is Present. Finding the Value Data for the $name" 
  45.        $Valuedata = (Get-ItemProperty -Path $updateKey).$Name 
  46.        #Remove-ItemProperty -Path $updateKey -name $Name 
  47.         
  48.        if ($data -eq "null") { 
  49.        Write-Host "Reg Key:     $updateKey" 
  50.        Write-Host "Value Name:  $Name" 
  51.        Write-Host "Value data:  $Valuedata" 
  52.        return 
  53.         }  
  54.      if ($Valuedata -ne $null) { 
  55.         if ($Valuedata -eq $data) { 
  56.           Write-Host "=====================================================================" 
  57.           Write-Host "Found the key. No Change made to the Registry" 
  58.           Write-Host "Reg Key:     $updateKey" 
  59.           Write-Host "Value Name:  $Name" 
  60.           Write-Host "Value data:  $Valuedata" 
  61.           Write-Host "Scripting Ending.... " 
  62.           Write-Host "=====================================================================" 
  63.           return 
  64.          
  65.          
  66.       
  67.       
  68.      else 
  69.       
  70.      
  71.      Write-Host "Value Not found $updatekey" 
  72.      Write-Host "Current Registry value is  $Valuedata" 
  73.      Write-Host "=====================================================================" 
  74.      Write-Host "Creating the Registry key" 
  75.      Set-ItemProperty -Path $updateKey -Name $Name -Value $data  
  76.      Write-Host "=====================================================================" 
  77.      $Valuedata = (Get-ItemProperty -Path $updateKey).$Name 
  78.      if ($Valuedata -eq $data) { 
  79.           Write-Host "=====================================================================" 
  80.           Write-Host "Reg Key:     $updateKey" 
  81.           Write-Host "Value Name:  $Name" 
  82.           Write-Host "Value data:  $Valuedata" 
  83.           Write-Host "Found the key." 
  84.           Write-Host "Successfully Created the Registry Key" 
  85.           Write-Host "S U C C E S S" 
  86.           Write-Host "Scripting Ending.... " 
  87.           Write-Host "=====================================================================" 
  88.           return 
  89.          
  90.          
  91.        }  
  92.     
  93.     else
  94.     Write-Host "Value does not exist on the machine" 
  95.     Write-Host "=====================================================================" 
  96.     Write-Host "Creating the Registry key" 
  97.     New-ItemProperty -Path $updateKey -Name $Name -Value $data -PropertyType $type | Out-Null 
  98.     Write-Host "=====================================================================" 
  99.     Write-Host "Checking if the Registry key is created on the machine" 
  100.      $Valuedata = (Get-ItemProperty -Path $updateKey).$Name 
  101.      if ($Valuedata -eq $data) { 
  102.           Write-Host "=====================================================================" 
  103.           Write-Host "Reg Key:     $updateKey" 
  104.           Write-Host "Value Name:  $Name" 
  105.           Write-Host "Value data:  $Valuedata" 
  106.           Write-Host "Found the key." 
  107.           Write-Host "Successfully Created the Registry Key" 
  108.           Write-Host "S U C C E S S" 
  109.           Write-Host "Scripting Ending.... " 
  110.           Write-Host "=====================================================================" 
  111.           return 
  112.          
  113.          
  114.          else
  115.            Write-Host "=====================================================================" 
  116.           Write-Host "Registry Key is not present." 
  117.           Write-Host "F A I L" 
  118.           Write-Host "Scripting Ending.... " 
  119.           Write-Host "=====================================================================" 
  120.           return 
  121.          
  122.        
  123.      
  124.    
  125.  
  126.  
  127.  
  128. #usage  
  129.  
  130. # Call the Function by passing the Registry Key , Value Name , Value Data , Type" 
  131. # This Function Create/Update the Exitng value  
  132. # Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "E2" "0" "Dword" 
  133. # Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "E2" "0" "Dword" 
  134. # Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "Entry" "Newupdate" 
  135. # Pass "Null"  for the Third Argument while calling the Function if you want to retive the Registry Value Data" 
  136. # Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "Entry" "Null" 
 
Loading...
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.