Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Changing Registry Key values

$
0
0

I have the following function to modify values in a group of registry keys.  It is run with an account that has local admin rights on all the machines in my environment.  The script that calls the function is elevated to run with elevated permissions ('as administrator').  The issue I am having is that the first key never gets updated, while the other two do.  However if I go back to my trusty command prompt (using the same account 'as administrator') and do a REG ADD, that updates that key not being updated by the PowerShell script without a problem.  I tried two different methods to modify the key not updating as you will see in the code below.

Here is the function:

Function Update_Registry($ComputerName, $521) {
   $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$ComputerName'" | Select-Object StatusCode
   If ($PingStatus.StatusCode -eq 0){
      $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $ComputerName )
      $regkey = $null
      $regKey = $reg.OpenSubKey("SOFTWARE\\Company\\Tattoo",$true)
      $Values = $regKey.GetValueNames()
      ForEach ($Value in $Values) {
         If ($Value -eq "Owner") {
            $regKey.SetValue($Value, $521, [Microsoft.Win32.RegistryValueKind]::String) # Doesn't set the key
            Invoke-Command -Computername $ComputerName -ScriptBlock { Set-ItemProperty -Path 'HKLM:\SOFTWARE\Company\Tattoo' -Name $Value -Value $521 } # WinRM cannot complete the operation
         }
      }
      $regkey = $null
      $regKey = $reg.OpenSubKey("SOFTWARE\\Wow6432Node\\Company\\Tattoo",$true)
      $Values = $regKey.GetValueNames()
      ForEach ($Value in $Values) {
         If ($Value -eq "Owner") {
            $regKey.SetValue($Value, $521, [Microsoft.Win32.RegistryValueKind]::String) # Sets the key
         }
      }
      $regkey = $null
      $regKey = $reg.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",$true)
      $Values = $regKey.GetValueNames()
      ForEach ($Value in $Values) {
         If ($Value -eq "CoOwner") {
            $regKey.SetValue($Value, $521, [Microsoft.Win32.RegistryValueKind]::String) # Sets the key
         }
      }
   } Else {
      Write-Host "$ComputerName unreachable"
   }
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles