locked
Registry Edit via WMI RRS feed

  • Question

  • All,

    Normally I would have no issues modifying registry settings.  However, the company I work for will not all PSRemoting to be turned on.  That's a whole other story.

    What I am trying to is find a specific registry key and it's subkeys.  This I am able to do.  I have gotten down to where I can list the properties for the subkeys, but I cannot list their values.  I am looking for specific values.  I am now stuck at the point of getting the values and modifying a specific value.

    Here is the script so far:

    # WMI Method
    $HKEY_LOCAL_MACHINE = 2147483650
    $strKeyPath = "SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}"
    
    $reg = Get-WmiObject -List "StdRegProv" -Namespace "Root\DEFAULT"
    $values = @()
    $arrSubKeys = $reg.EnumKey($HKEY_LOCAL_MACHINE, $strKeyPath)
    foreach ($subKey in ($arrSubKeys.sNames)) {
    	$strKeyPath2 = "SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\$subkey"
    	$snames = $reg.EnumValues($HKEY_LOCAL_MACHINE, $strKeyPath2) | select -ExpandProperty sNames
    	foreach ($sname in $snames) {
    		$strKeyPath3 = "SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\$subkey\$sname"
    		$desc = "DriverDesc"
    		$reg.GetDWORDValue($HKEY_LOCAL_MACHINE, $strKeyPath3, $desc)
    	}
    }

    Here is the script I should be able to use with PSRemoting but cannot convince my company to let me turn that on.  This should give you a better idea of what I am trying to accomplish.

    $regroot = "HKLM:\\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\"
    $dir = Get-ChildItem $regroot -Recurse -ErrorAction SilentlyContinue
    $subkeys = $dir.Name
    foreach ($key in $subkeys) {
    	$start = $key.IndexOf("}") + 2
    	$subkey = $key.SubString($start, 4)
    	$driver = Get-ItemPropertyValue "$regroot$subkey" -Name "DriverDesc" -ErrorAction SilentlyContinue
    	if ($driver -match "Ethernet Connection") { # Intel
    		$rootsubkey = @()
    		$driverkey = $key.SubString($start, 4)
    		$rootsubkey += $driverkey
    	}
    	elseif ($driver -match "Realtek PCIe GBE Family Controller") { #RealTek
    		$rootsubkey = @()
    		$driverkey = $key.SubString($start, 4)
    		$rootsubkey += $driverkey
    	}
    	else {
    		break	
    	}
    }
    
    $WoL = $rootsubkey | select -Unique
    $status = Get-ItemPropertyValue "$regroot$WoL" -Name "*WakeOnMagicPacket"
    if ($status -eq 1) {
    	Write-Host "OS WoL Enabled"
    }
    elseif ($status -eq 0) {
    	Write-Host "Enabling OS WoL"
    	Set-ItemProperty "$regroot$WoL" -Name "*WakeOnMagicPacket" -Value 1
    }
    else {
    	break	
    }

    Monday, June 18, 2018 6:52 PM

Answers


  • Here is the script I should be able to use with PSRemoting but cannot convince my company to let me turn that on.  This should give you a better idea of what I am trying to accomplish.


    Use remote registry.

    Example:

    $key = 'SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\'
    $regbase=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('localmachine',$env:computername)
    $regroot=$regbase.OpenSubKey($key)
    $regroot.GetSubKeyNames()
    


    \_(ツ)_/

    Monday, June 18, 2018 7:15 PM

All replies


  • Here is the script I should be able to use with PSRemoting but cannot convince my company to let me turn that on.  This should give you a better idea of what I am trying to accomplish.


    Use remote registry.

    Example:

    $key = 'SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\'
    $regbase=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('localmachine',$env:computername)
    $regroot=$regbase.OpenSubKey($key)
    $regroot.GetSubKeyNames()
    


    \_(ツ)_/

    Monday, June 18, 2018 7:15 PM
  • This is what I have been referencing for my current script.
    Monday, June 18, 2018 7:37 PM
  • I forgot about this method.  I will give it a try.
    Monday, June 18, 2018 7:38 PM
  • Perhaps the easiest way to do this remotely is this:

    $pnpID = 'PCI\\VEN_8086&DEV_095A&SUBSYS_50108086&REV_61\\4&2722d2b9&0&00E5_0'
    $device = Get-WmiObject MSPower_DeviceWakeEnable -Filter "InstanceName= '$pnpID'"-Namespace root\wmi -Computer Alpha
    $device.SetPropertyValue('Enable',$true)
    


    \_(ツ)_/

    Monday, June 18, 2018 7:44 PM
  • This is what my finished script looks like.  After completing and testing, I figured out that our Update Management software automatically turns on WoL in the OS.  I learned something new even though I didn't use it.

    Import-Module ActiveDirectory -ErrorAction Stop
    
    $workstations = Get-ADComputer -SearchBase 'OU=Workstations,DC=Server,DC=local' -SearchScope 2 -Filter * | Select -ExpandProperty Name | Sort
    
    $rootkey = 'SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}'
    
    foreach ($pc in $workstations) {
    	if (Test-Connection $pc -Quiet -Count 2) {
    		$regbase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $pc)
    		$regroot = $regbase.OpenSubKey($rootkey, $true)
    		$regsubkeys = $regroot.GetSubKeyNames()
    		foreach ($subkey in $regsubkeys) {
    			$rootsubkey = 'SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\' + $subkey
    			$regrootsub = $regbase.OpenSubKey($rootsubkey, $true)
    			$keyvalue = $regrootsub.GetValue('DriverDesc')
    			if ($keyvalue -eq 'Realtek PCIe GBE Family Controller') {
    				$WoL = $regrootsub.GetValue('*WakeOnMagicPacket')
    				if ($WoL -eq 1) {
    					Write-Host "$pc - OS Wol Enabled"
    				}
    				elseif ($WoL -eq 0) {
    					$regrootsub.SetValue('*WakeOnMagicPacket', 1, 'String')
    					Write-Host "$pc- Enabling OS WoL"
    				}
    			}
    			if ($keyvalue -match 'Ethernet Connection') {
    				$WoL = $regrootsub.GetValue('*WakeOnMagicPacket')
    				if ($WoL -eq 1) {
    					Write-Host "$pc - OS Wol Enabled"
    				}
    				elseif ($WoL -eq 0) {
    					$regrootsub.SetValue('*WakeOnMagicPacket', 1, 'String')
    					Write-Host "$pc- Enabling OS WoL"
    				}
    			}
    		}
    	}
    	else {
    		Write-Host "$pc - Offline"	
    	}
    }

    Monday, June 18, 2018 9:57 PM