简体   繁体   English

PowerShell 将字符串转换为变量

[英]PowerShell convert string to variable

In PowerShell I'm working on a script and can't solve this:在 PowerShell 中,我正在编写一个脚本,但无法解决这个问题:

$GROUPA = 'A', 'B', 'C', 'D'
$GROUPB = 'E', 'F', 'G', 'H'

$SELECT = 'GROUPA'

write-output $$SELECT (or something)

I have a view arrays, and than a script thats output one of the array names.我有一个视图数组,而不是一个输出数组名称之一的脚本。 And I want to output that array, but can't find how to do that with powershell.我想输出该数组,但找不到如何使用 powershell 执行此操作。

Your $SELECT is just a string , instead you probably want to assign it to the $GROUPA variable:您的$SELECT只是一个string ,而您可能希望将其分配给$GROUPA变量:

$SELECT = $GROUPA    
write-output $SELECT

Or, if you wan't a to resolve a variable using a string, use the Get-Variable cmdlet:或者,如果您不想使用字符串解析变量,请使用Get-Variable cmdlet:

$SELECT = Get-Variable 'GROUPA'

This may help you (even if it's old topic)这可能对您有所帮助(即使是旧话题)

$A=1
$B=2
$C=3
$D=4
$E=11
$F=12
$G=13
$H=14
$GROUPA = 'A', 'B', 'C', 'D'
$GROUPB = 'E', 'F', 'G', 'H'
$SELECT=$GROUPB
$myval1 = (Get-Variable $SELECT[0]).Value
$myval2 = (Get-Variable $SELECT[1]).Value
$myval3 = (Get-Variable $SELECT[2]).Value
$myval4 = (Get-Variable $SELECT[3]).Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM