Should this function return a value other than null ?

Welcome Forums General PowerShell Q&A Should this function return a value other than null ?

  • This topic has 5 replies, 3 voices, and was last updated 9 months ago by
    Participant
    .
Viewing 5 reply threads
  • Author
    Posts
    • #194948
      Participant
      Topics: 3
      Replies: 4
      Points: 23
      Rank: Member

      Hello,

      I am puzzle over what is expected to return from this sample function below. It should return to me some value other than null which is in fact returning. If you could shed some light into what’s going on , please let me know.

      Below is the simple code that I got from Google and I’m trying to understand  of why it returns a null value on the reg value.

       

      $gprRegPath = “HKLM:\Software\Microsoft\Windows\CurrentVersion”
      function Get-Reg($key, $val) {

      if($key -and !$val) { return test-path $key }
      elseif($key) { write-host “Key is $key”

      return (gp $key -ea silentlycontinue).$val }
      else { help get-reg }
      }

      ##get-reg $gprRegPath Domain
      $reg = GET-REG $gprRegPath Domain
      write-host “Reg is $reg”

       

    • #194954
      Participant
      Topics: 51
      Replies: 209
      Points: 860
      Helping Hand
      Rank: Major Contributor

      What is returned when you run the base registry query?

      Is there a ‘Domain’ entry in the registry you’re querying ?

    • #194957
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      What are you expecting from this piece of code? In-fact there is no property called ‘Domain’ in the given key, that’s why it is returning null

    • #194963
      Participant
      Topics: 3
      Replies: 4
      Points: 23
      Rank: Member

      Thank you so much Kiran for sheding lights into my confusion.

      As I ran gp “HKLM:\Software\Microsoft\Windows\CurrentVersion”

      It returns :

      ProgramFilesDir : C:\Program Files
      CommonFilesDir : C:\Program Files\Common Files
      ProgramFilesDir (x86) : C:\Program Files (x86)
      CommonFilesDir (x86) : C:\Program Files (x86)\Common Files
      CommonW6432Dir : C:\Program Files\Common Files
      DevicePath : C:\Windows\inf
      MediaPathUnexpanded : C:\Windows\Media
      ProgramFilesPath : C:\Program Files
      ProgramW6432Dir : C:\Program Files
      SM_ConfigureProgramsName : Set Program Access and Defaults
      SM_GamesName : Games
      PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion
      PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
      PSChildName : CurrentVersion
      PSDrive : HKLM
      PSProvider : Microsoft.PowerShell.Core\Registry

       

      Could I ask what do you mean by if there is a Domain entry in the registry that I am querying ? Based on what I got from the above returned, I could not find

      any Domain entry. Please correct me if I was looking at the right place or should I look somewhere else for the Domain entry  and how ?

      Many Thanks for all of the help!

       

    • #194969
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      When you call a property of an object, first it should exist otherwise it returns a null value.

      By the way, what is your requirement?

    • #194975
      Participant
      Topics: 3
      Replies: 4
      Points: 23
      Rank: Member

      Thank you so much Kiran for all of the helps and explanations.

      I  undestand now why I got a null value because of like you were explaining that there is no property called “Domain” in the given

      “HKLM:\Software\Microsoft\Windows\CurrentVersion” key; so that’s why it gave me a null value.

      Furthere more, gp “HKLM:\Software\Microsoft\Windows\CurrentVersion” show me that there is no “Domain” property.

       

Viewing 5 reply threads
  • The topic ‘Should this function return a value other than null ?’ is closed to new replies.