Wednesday, April 3, 2013

Powershell: Learning something new everyday

Learning something new everyday is what makes programming interesting. The lesson learned today is to stop thinking powershell is "just like any other language".  Because it is not.

Consider the following:

My task is to check the local registry for the version of the OS.  So, in line, I did the following to check it:

PS C:\Users\Administrator> $HKEY_LOCAL_MACHINE = 2147483650
PS C:\Users\Administrator> $Reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
PS C:\Users\Administrator> $Key = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
PS C:\Users\Administrator> $Result = $Reg.GetStringValue($HKEY_LOCAL_MACHINE, $Key, "CurrentVersion")
PS C:\Users\Administrator> write-host $Result
System.Management.ManagementBaseObject
PS C:\Users\Administrator> $Result.GetText(0)
instance of __PARAMETERS
{
        ReturnValue = 0;
        sValue = "6.1";
};

Okay, everything looks good. In line, that is.  What happens when you want to put the routine into a function and call it anywhere.

Well, it turns out, you can try, but you'll be wasting a lot of hours.

First, let's create the function wrapper:

function get_operatingsystem_version()
{

}


Now, you would think, if you've been programming for as long as I have, that you would od the following:

$HKLM = 2147483650
$Key = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"

$Version = get_operatingsystem_version
if ($Version -ne  "Error")
{
      write-host $version
}
function get_operatingsystem_version()
{
      $Reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
      $Result = $Reg.GetStringValue($HKLM, $Key, "CurrentVersion")
      if($Result.ReturnValue -eq 0)
      {
            return  $Result.sValue          
      }
      else
      {
            return   "Error"    
      }
}
Should work, right?  Doesn't. In fact, it comes back with the following error:

The term 'get_operatingsystem_version' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Administrator\Desktop\GetOS.ps1:5 char:39
+ $Version = get_operatingsystem_version <<<<
    + CategoryInfo          : ObjectNotFound: (get_operatingsystem_version:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Administrator>
Why did this happen?  Because the function has not yet been seen by Powershell at the point where the call is being made.

Now, let's try it this way:

$HKLM = 2147483650
$Key = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"

function get_operatingsystem_version()
{
      $Reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
      $Result = $Reg.GetStringValue($HKLM, $Key, "CurrentVersion")
      if($Result.ReturnValue -eq 0)
      {
            return  $Result.sValue          
      }
      else
      {
            return   "Error"    
      }
}

$Version = get_operatingsystem_version
if ($Version -ne "Error")
{
write-host $version
}
When I run this, I get:

PS C:\Users\Administrator> C:\Users\Administrator\Desktop\GetOS.ps1
6.1

No comments:

Post a Comment