4

I have a script to query a list of remote windows servers to retrieve the value of an Environment Variable I have created.

I can see that the variable is indeed there because I can see it when I print all variables: $EnvObj:

try 
{
    $Name="MY_VAR"
    $VerbosePreference="Continue"

    # Read a list of server names
    $file = New-Object System.IO.StreamReader -Arg "C:\Users\foo\Documents\test.txt"

    while ($Computer = $file.ReadLine()) 
    {    
        if(!(Test-Connection -ComputerName $Computer -Count 1 -quiet)) 
        {            
            Write-Verbose "$Computer is not online"            
            Continue            
        }            

        try 
        {            
            $EnvObj = @(Get-WMIObject -Class Win32_Environment -ComputerName $Computer -EA Stop)                        
            if(!$EnvObj) 
            {            
               Write-Verbose "$Computer returned empty list of environment variables"            
               Continue            
            }            

            if($Name) 
            {         
                Write-Verbose "Looking for environment variable with the name $name"            
                $Env = $EnvObj | Where-Object {$_.Name -eq $Name}        

                # None of these work but printing $EnvObj shows that $Name ("MY_VAR") is there:
                $res=[environment]::GetEnvironmentVariable($Name, "Process")
                $res1=[environment]::GetEnvironmentVariable($Name, "User")
                $res2=[environment]::GetEnvironmentVariable($Name, "Machine")
                $res4=$Env:Name

                if(!$Env) 
                {            
                    Write-Verbose "$Computer has no environment variable with name $Name"            
                    Continue            
                }                                            
            } 
            else 
            {            
                Write-Verbose "No environment variable specified. Listing all"            
                $EnvObj # shows that the requested environment variable is present            
            }            
        } 
        catch 
        {            
            Write-Verbose "Error occurred while querying $Computer. $_"            
            Continue            
        }           
    }
}
finally
{
    $file.close()
} 

The variable itself is owned by <SYSTEM>:

VariableValue    Name    UserName                                                
-------------    ----    --------                                                
1234            MY_VAR  <SYSTEM> 

But when I try to retrieve it with any of the possible enumerations, it just returns nothing:

None of these work but printing $EnvObj shows that $Name ("MY_VAR") is there:

# .Net way
$res=[environment]::GetEnvironmentVariable($Name, "Process")
$res1=[environment]::GetEnvironmentVariable($Name, "User")
$res2=[environment]::GetEnvironmentVariable($Name, "Machine")

# PS Way
$res4=$Env:Name

... even though when I print the $EnvObj object I can see that $Name is definitely there.

What is wrong?

13
  • Your .NET lines [environment]::GetEnvironmentVariable... are looking at the local PC not remote. What is in your $Env variable? Commented Aug 6, 2015 at 2:09
  • Hi Jan, the $Env has the data I'm after but when I request it: $val = $Env:Name it just returns empty. From what I've read this is the correct way to retrieve the data. Commented Aug 6, 2015 at 2:17
  • Try using a dot . Instead of : Commented Aug 6, 2015 at 2:19
  • $val = $Env.Name has the same result... I've been going off this: technet.microsoft.com/en-us/library/ff730964.aspx Commented Aug 6, 2015 at 2:20
  • 1
    Step 1, stop using the variable name $env, when you use $ENV:Name it is a special variable looking in the PSDrive of ENV: for an item called Name. Change the variable name and try again. Commented Aug 6, 2015 at 2:24

1 Answer 1

1

Your line

$EnvObj = @(Get-WMIObject -Class Win32_Environment -ComputerName $Computer -EA Stop) 

Will return an array of [ManagementObject].

To examine all the properties you can pipe one of these objects to Get-Member

$EnvObj[0] | Get-Member

   TypeName: System.Management.ManagementObject#root\cimv2\Win32_Environment

Name                MemberType    Definition                             
----                ----------    ----------                             
PSComputerName      AliasProperty PSComputerName = __SERVER              
Caption             Property      string Caption {get;set;}              
Description         Property      string Description {get;set;}          
InstallDate         Property      string InstallDate {get;set;}          
Name                Property      string Name {get;set;}                 
Status              Property      string Status {get;set;}               
SystemVariable      Property      bool SystemVariable {get;set;}         
UserName            Property      string UserName {get;set;}             
VariableValue       Property      string VariableValue {get;set;}        
__CLASS             Property      string __CLASS {get;set;}              
__DERIVATION        Property      string[] __DERIVATION {get;set;}       
__DYNASTY           Property      string __DYNASTY {get;set;}            
__GENUS             Property      int __GENUS {get;set;}                 
__NAMESPACE         Property      string __NAMESPACE {get;set;}          
__PATH              Property      string __PATH {get;set;}               
__PROPERTY_COUNT    Property      int __PROPERTY_COUNT {get;set;}        
__RELPATH           Property      string __RELPATH {get;set;}            
__SERVER            Property      string __SERVER {get;set;}             
__SUPERCLASS        Property      string __SUPERCLASS {get;set;}         
PSStatus            PropertySet   PSStatus {Status, Name, SystemVariable}
ConvertFromDateTime ScriptMethod  System.Object ConvertFromDateTime();   
ConvertToDateTime   ScriptMethod  System.Object ConvertToDateTime();    

From this you can see that the value property is called VariableValue.

So,

$EnvLookup = $EnvObj | Where-Object {$_.Name -eq $Name}
$res = $EnvLookup.VariableValue
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.