gpt4 book ai didi

powershell - 使用非字符串作为 $env : variable

转载 作者:行者123 更新时间:2023-12-02 05:04:52 24 4
gpt4 key购买 nike

我想在环境变量中存储一个哈希表——这在 Powershell 中可行吗?

这是我测试的方法:

$env:test = @{}
$env:test
$env:test|gm

并感到困惑,因为输出是:

System.Collections.Hashtable

TypeName: System.String

Name MemberType Definition

---- ---------- ---------- Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB) ...



... 所以当我直接返回 $env:test 时,我得到了 'Hashtable',但是当我通过管道将它传递给 get-member 时,
返回 system.string。有人可以解释这种行为吗?

最佳答案

环境变量 ($env:stringVariable) 是字符串。但是,您似乎希望将 hashtable 类型的非字符串变量设为全局。这可以通过使用全局修饰符 来完成。 ($global:hashtableVariable) .这将允许您从另一个脚本文件中的一个脚本文件访问变量,如此处所示

PowerShellScriptFile-1.ps1

$global:hashtableVariable = @{
'key1' = 'value1'
'key2' = 'value2'
}

PowerShellScriptFile-2.ps1
$global:hashTableVariable.GetEnumerator() | ForEach-Object {
Write-Host "$($_.Key):$($_.Value)"
}

关于powershell - 使用非字符串作为 $env : variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13128284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com