定义Powershell变量以具有列表值

定义Powershell变量以具有列表值,powershell,Powershell,我有一些变量如下: $a = "string1" $b = "string2" $c = "string3" 现在我想定义一个变量$list to,其中包含$a、$b、$c中的所有内容,如下所示: $list = " a: string1 b: string2 c: string3 " $list = @" a: ${a} b: ${b} c: ${c} "@ $list = 'a', 'b', 'c' | ForEach-Object { "${_}: $(Get-Variabl

我有一些变量如下:

$a = "string1"
$b = "string2"
$c = "string3"
现在我想定义一个变量$list to,其中包含$a、$b、$c中的所有内容,如下所示:

$list = "
a: string1
b: string2
c: string3
"
$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
$list = 'a', 'b', 'c' | ForEach-Object {
    "${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
$list = @(
    'a: string1',
    'b: string2',
    'c: string3'
)

$list | ForEach-Object {
    if ($_ -match '^\s*(?<VarName>[^\:]+)\s*:\s*(?<VarValue>.*)$') {
        Write-Verbose ('Name: {0}; Value {1}' -f $Matches.VarName, $Matches.VarValue)
        New-Variable -Name $Matches.VarName -Value $Matches.VarValue -Force
    }
}
$a
$b
$c
我该怎么做

***编辑:列表的内容必须和我写的完全一样,也就是说,像“a:b:”,…这样的部分也必须相同 您可以使用新对象System.Collections.ArrayList创建列表并使用它。 例如:

现在$list[1]给出了string2。

希望这有帮助。:


您可以使用新对象System.Collections.ArrayList创建列表并使用它。 例如:

现在$list[1]给出了string2。

希望这有帮助。:)

编辑: 我没有完全阅读您的问题,下面是一个键=值对的哈希表示例:

$a = "string1"
$b = "string2"
$c = "string3"

$list = @{
a = $a
b = $b
c = $c
}
最终,这取决于您是否知道如何使用该构造,以及您使用它的目的。有许多方法可以实现一个包含3个字符串的简单列表/数组/表

编辑: 我没有完全阅读您的问题,下面是一个键=值对的哈希表示例:

$a = "string1"
$b = "string2"
$c = "string3"

$list = @{
a = $a
b = $b
c = $c
}
最终,这取决于您是否知道如何使用该构造以及使用它的目的。有许多方法可以实现一个包含3个字符串的简单列表/数组/表。

Powershell有“”:

“数组是设计用于存储集合的数据结构 物品的数量。项目可以是相同类型,也可以是不同类型。”

在您的情况下,如果已经有变量,可以将它们包含在数组中:

$myarray = $a, $b, $c
Write-Host $myarray

或者,可以为输入键值元素创建关联数组:

$otherarray = @{}            # create en empty array
$otherarray['a'] = 'string1' # add key-value pairs
$otherarray['b'] = 'string2'
$otherarray['c'] = 'string3'
Write-Host $otherarray.c     #print in console example
编辑:根据您的示例,我推荐使用数组,因为我认为这是一个简单的用例,但如果您愿意经常修改它,最好使用:

Powershell有“”:

“数组是设计用于存储集合的数据结构 物品的数量。项目可以是相同类型,也可以是不同类型。”

在您的情况下,如果已经有变量,可以将它们包含在数组中:

$myarray = $a, $b, $c
Write-Host $myarray

或者,可以为输入键值元素创建关联数组:

$otherarray = @{}            # create en empty array
$otherarray['a'] = 'string1' # add key-value pairs
$otherarray['b'] = 'string2'
$otherarray['c'] = 'string3'
Write-Host $otherarray.c     #print in console example
编辑:根据您的示例,我推荐使用数组,因为我认为这是一个简单的用例,但如果您愿意经常修改它,最好使用:


看起来您不需要实际的列表,而是一个包含变量列表的字符串。包含变量名称和值的多行字符串可以这样定义:

$list = "
a: string1
b: string2
c: string3
"
$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
$list = 'a', 'b', 'c' | ForEach-Object {
    "${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
$list = @(
    'a: string1',
    'b: string2',
    'c: string3'
)

$list | ForEach-Object {
    if ($_ -match '^\s*(?<VarName>[^\:]+)\s*:\s*(?<VarValue>.*)$') {
        Write-Verbose ('Name: {0}; Value {1}' -f $Matches.VarName, $Matches.VarValue)
        New-Variable -Name $Matches.VarName -Value $Matches.VarValue -Force
    }
}
$a
$b
$c
另一个更具活力的选项是:

$list = "
a: string1
b: string2
c: string3
"
$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
$list = 'a', 'b', 'c' | ForEach-Object {
    "${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
$list = @(
    'a: string1',
    'b: string2',
    'c: string3'
)

$list | ForEach-Object {
    if ($_ -match '^\s*(?<VarName>[^\:]+)\s*:\s*(?<VarValue>.*)$') {
        Write-Verbose ('Name: {0}; Value {1}' -f $Matches.VarName, $Matches.VarValue)
        New-Variable -Name $Matches.VarName -Value $Matches.VarValue -Force
    }
}
$a
$b
$c

您可以指定一个包含变量名的列表,输出包含每个变量名和相应值的字符串,并在末尾将它们合并为一个字符串。

看起来您不需要实际的列表,而是一个包含变量列表的字符串。可以定义一个包含变量名和值的多行字符串,如e这是:

$list = "
a: string1
b: string2
c: string3
"
$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
$list = 'a', 'b', 'c' | ForEach-Object {
    "${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
$list = @(
    'a: string1',
    'b: string2',
    'c: string3'
)

$list | ForEach-Object {
    if ($_ -match '^\s*(?<VarName>[^\:]+)\s*:\s*(?<VarValue>.*)$') {
        Write-Verbose ('Name: {0}; Value {1}' -f $Matches.VarName, $Matches.VarValue)
        New-Variable -Name $Matches.VarName -Value $Matches.VarValue -Force
    }
}
$a
$b
$c
另一个更具活力的选项是:

$list = "
a: string1
b: string2
c: string3
"
$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
$list = 'a', 'b', 'c' | ForEach-Object {
    "${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
$list = @(
    'a: string1',
    'b: string2',
    'c: string3'
)

$list | ForEach-Object {
    if ($_ -match '^\s*(?<VarName>[^\:]+)\s*:\s*(?<VarValue>.*)$') {
        Write-Verbose ('Name: {0}; Value {1}' -f $Matches.VarName, $Matches.VarValue)
        New-Variable -Name $Matches.VarName -Value $Matches.VarValue -Force
    }
}
$a
$b
$c

您可以指定一个包含变量名的列表,输出包含每个变量名和相应值的字符串,并在末尾将它们合并为一个字符串。

我怀疑其他答案已经为您提供了您想要的答案。不过,以下是对您的问题的不同解释的其他解决方案


如果希望在运行时用
a
b
c
中的值填充列表,可以执行以下操作:

$a = "string1"
$b = "string2"
$c = "string3"

$list = @('a','b','c') | Get-Variable | ForEach-Object{'{0}: {1}' -f $_.Name, $_.Value}
$list
i、 e.这将查找变量
a
b
c
,并根据变量名称和格式为
name:value
的值填充
list


或者,如果您希望使用单个“此处字符串”(即可能是多行的字符串)填充列表,将其拆分为每行一项,您可以执行以下操作:

$list = @'
a: string1
b: string2
c: string3
'@ -split '[\r\n]+'
$list.Count #prove that our list contains 3 items rather than 1 multi line item
$list # show the contents of the array
这将获取多行字符串并在换行符处将其拆分(使用正则表达式以确保它以相同的方式处理不同的潜在行尾,并跳过多个连续换行符以避免在项目之间创建空白条目)


最后,如果您想从列表中填充变量
a
b
c
,可以使用如下方法:

$list = "
a: string1
b: string2
c: string3
"
$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
$list = 'a', 'b', 'c' | ForEach-Object {
    "${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
$list = @(
    'a: string1',
    'b: string2',
    'c: string3'
)

$list | ForEach-Object {
    if ($_ -match '^\s*(?<VarName>[^\:]+)\s*:\s*(?<VarValue>.*)$') {
        Write-Verbose ('Name: {0}; Value {1}' -f $Matches.VarName, $Matches.VarValue)
        New-Variable -Name $Matches.VarName -Value $Matches.VarValue -Force
    }
}
$a
$b
$c
$list=@(
“a:string1”,
“b:string2”,
‘c:string3’
)
$list | ForEach对象{
如果($匹配'^\s*(?[^\:]+)\s*:\s*(?*)$){
详细写入('Name:{0};Value{1}'-f$Matches.VarName$Matches.VarValue)
新变量-Name$Matches.VarName-Value$Matches.VarValue-Force
}
}
一美元
b美元
$c

我怀疑其他答案已经为您提供了您想要的答案。不过,以下是针对您的问题的不同解释的其他解决方案


如果希望在运行时用
a
b
c
中的值填充列表,可以执行以下操作:

$a = "string1"
$b = "string2"
$c = "string3"

$list = @('a','b','c') | Get-Variable | ForEach-Object{'{0}: {1}' -f $_.Name, $_.Value}
$list
i、 e.这将查找变量
a
b
c
,并根据变量名称和格式为
name:value
的值填充
list


或者,如果您希望使用单个“此处字符串”(即可能是多行的字符串)填充列表,将其拆分为每行一项,您可以执行以下操作:

$list = @'
a: string1
b: string2
c: string3
'@ -split '[\r\n]+'
$list.Count #prove that our list contains 3 items rather than 1 multi line item
$list # show the contents of the array
这将获取多行字符串并在换行符处将其拆分(使用正则表达式以确保它以相同的方式处理不同的潜在行尾,并跳过多个连续换行符以避免在项目之间创建空白条目)


最后,如果您想从列表中填充变量
a
b
c
,可以使用如下方法:

$list = "
a: string1
b: string2
c: string3
"
$list = @"
a: ${a}
b: ${b}
c: ${c}
"@
$list = 'a', 'b', 'c' | ForEach-Object {
    "${_}: $(Get-Variable $_ -ValueOnly)"
} | Out-String
$list = @(
    'a: string1',
    'b: string2',
    'c: string3'
)

$list | ForEach-Object {
    if ($_ -match '^\s*(?<VarName>[^\:]+)\s*:\s*(?<VarValue>.*)$') {
        Write-Verbose ('Name: {0}; Value {1}' -f $Matches.VarName, $Matches.VarValue)
        New-Variable -Name $Matches.VarName -Value $Matches.VarValue -Force
    }
}
$a
$b
$c
$list=@(
“a:string1”,
“b:string2”,
‘c:string3’
)
$list | ForEach对象{
如果($匹配'^\s*(?[^\:]+)\s*:\s*(?*)$){
详细写入('Name:{0};Value{1}'-f$Matches.VarName$Matches.VarValue)
新变量-名称$Matches.VarName-值$Matches。