Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

powershell - Pointer or reference variables

I want to use indirect reference variable. I am setting this at Command Prompt

SET RiskScheduler=true

My code is like this

Write-Host "$Env:RiskScheduler"  # prints true 

I want to achieve the same should be printed with code like this

$name='RiskScheduler'  
Write-host $name                # prints RiskScheduler
Write-Host $Env:$name           # gives error

The error I am getting is

Cannot process argument because the value of argument "path" is invalid.
Change the value of the "path" argument and run the operation again.
At D:mpuildtoolsudclient.6.2ud_clean.PS1:37 char:17
+ Write-Host $Env: <<<< `$name`
    + CategoryInfo          : InvalidArgument: (:) [], PSArgumentException
    + FullyQualifiedErrorId : Argument

I am looking for something that shall first evaluate $name and then evaluate $Env:(value of $name).

Can someone please suggest, what the correct syntax is?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Option 1

You can do this with:

$x = 'RiskScheduler'
Write-Host (Get-Item env:$x).Value

Which will output true in your case

If you have a list of variable names:

$varNames = @('COMPUTERNAME', 'SESSIONNAME', 'RiskScheduler')

ForEach($varName in $varNames) {
    Write-Host (Get-Item env:$varName).Value
}

Which will output:

MyPcName
Console
true

You can find more information about this by entering Get-Help about_environment_variables into PowerShell:

Get-Item -Path Env:* | Get-Member

Displaying Environment Variables You can use the cmdlets that contain the Item noun (the Item cmdlets) to display and change the values of environment variables. Because environment variables do not have child items, the output of Get-Item and Get-ChildItem is the same.

When you refer to an environment variable, type the Env: drive name followed by the name of the variable. For example, to display the value of the COMPUTERNAME environment variable, type:

Get-Childitem Env:Computername


Option 2

Another option would be to use this function:

function Get-EnvVar($Name) {
    $allVars = dir env:

    foreach ($var in $allVars) {
        If ($var.Name -eq $Name) {
            return $var
        }
    }
}

The function iterates around all available environment variables and returns the one you are after (in this case, $Env:COMPUTERNAME).

You can then call

Write-Host $myvar.Value

to display the value of COMPUTERNAME

If you have an array of variable names you want the value of:

$varNames = @('COMPUTERNAME', 'SESSIONNAME', 'RiskScheduler')

ForEach($varName in $varNames) {
    $var = Get-EnvVar -Name $varName
    Write-Host $var.Value
}

Which outputs (for me) :

MyPcName
Console
true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...