Can't access environment variable from remote server using PowerShell

I have a script to query a list of remote window servers to get the value of an environment variable I created.

I can see that the variable does exist because I can see it when I print all the 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 belongs to <SYSTEM>

:

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

      

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

None of these works, but print $EnvObj

shows that $Name ("MY_VAR")

:

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

# PS Way
$res4=$Env:Name

      

... although when I print the object $EnvObj

I can see that it $Name

definitely exists.

What's wrong?

+3


source to share


1 answer


Your string

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

      

Will return an array [ManagementObject]

.

To explore all properties, you can link 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

      

+1


source







All Articles