0

I am trying to return registry values from specific keys. For some reason I can not get the string value of a key. I can get the DWordValue no problem.

Can anyone tell me why the string value won't return?

The code:

function getRegistry(){
    Param (
        [string]$hklm = "2147483650",
        [string]$key = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
        [string]$Value2 = "ConnectionCenter"
    )

    $credential = Get-Credential

    $test | ForEach-Object {
        $wmi = Get-WmiObject -List "StdRegProv" -Namespace root\default -ComputerName "compName" -Credential $credential
        #($wmi.GetStringValue($hklm, $key, $value)).sValue

        $wmi.GetStringValue($hklm, $key, $Value2)
    }
}

getRegistry
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Blurr.P
  • 35
  • 1
  • 10
  • 2
    Among other things `$test` is null in this code. Your function should be doing nothing since the loop has no objects. – Matt May 02 '17 at 18:02
  • The script functions fine when I'm returning the DWordValue of a registry so I'm not sure what you mean it should be doing nothing. – Blurr.P May 02 '17 at 18:06
  • 1
    What purpose does $test serve? – Walter Mitty May 02 '17 at 18:21
  • I stand corrected. `$test` is redundant but the loop still fires. In either case the whole loop structure is pointless unless you allow for multiple computers which it does not look like it now. I would recommend moving the credential to a parameter that you pass in so that you dont have to type in new credentials every time. – Matt May 02 '17 at 18:24
  • Yeah I get what you're saying. The look structure is there because I will be using it for multiple computers when everything is said and done. – Blurr.P May 02 '17 at 18:27
  • You need to ensure that what you put up here is a [mcve] else we go round like we are right now. What exactly are you doing that is not working? The code in the comments? What happens why you try what you want to do? Do you get an error or does nothing happen? – Matt May 02 '17 at 18:29
  • 2
    Do you need to be using WMI for this? This whole thing becomes so much simpler if you can just use a remote session instead. – TheMadTechnician May 02 '17 at 18:31
  • I am unable to replicate. After getting the `$wmi` object, I can run the following to see all properties and their values: `$wmi.EnumValues($hklm,$key).sNames|%{"{0} - {1}" -f $_,$wmi.GetStringValue($hklm,$key,$_).sValue}` – TheMadTechnician May 02 '17 at 18:43
  • I'd for sure prefer to not go down the wmi route but remote powershell is disabled unfortunately. I'll try that out @TheMadTechnician thank you. – Blurr.P May 02 '17 at 18:55
  • 1
    Neither WinRM nor WMI is required, just [remote registry access](http://stackoverflow.com/a/15069578/1630171). – Ansgar Wiechers May 02 '17 at 19:55

0 Answers0