如果哈希表是在函数中生成的,如何访问 powershell 中的哈希表值?
how to access hashtable values in powershell if hashtable is generated in a function?
我创建了一个简单的 GUI 来输入一些值,这些值在用户单击“确定”后立即存储在 .txt 文件中。
为了进行验证,我在弹出窗口 window.
中显示了刚刚创建的文件的数据及其输入
因为我想在我用于项目的其他几个 .ps1 文件中使用配置文件的数据,所以我开始将内容移到全局文件中。ps1 文件.一切都很好,除了我无法再显示哈希表。
这是我的全局变量。ps1 :
# Wshell popup
$wshell = New-Object -ComObject Wscript.SHell
# read config
function readcfg
{
Get-Content -Path $cfg | foreach-object -begin { $conf = @{ } } -process {`
$key = [regex]::split($_, ':');
if (($key[0].CompareTo("") -ne 0) -and ($key[0].StartsWith("[") -ne $True))`
{ $conf.Add($key[0], $key[1]) }
}
}
这是我的设置 GUI 中的部分,如果按下“确定”按钮,就会执行该部分:
$cfgData = "
[Account]
Screds:" + $Admaccount.text + "
Spw:" + $PW + "
[Domainconfig]
Sdomain:" + $domain.text + "
SSearchBase:" + $searchBase.text + "
SdeactivatedUsers_OU:" + $deactivatedUsersOU.text + ""
Out-File -filepath $cfg -inputobject $cfgData -Force
Start-Sleep -s 1
$wshell.Popup("Settings saved:`nAccount: " + $conf.Screds + "`nDomain: " + $conf.Sdomain + "`nSearchBase: " + $conf.SSearchBase + "`ndeactivatedUsersOU " + $conf.SdeactivatedUsers_OU + "", 0, "Yarr...!", 0x0)
如果我将 get-Content 移出 readcfg 函数,我可以再次显示这些值。但这当然不是解决方案,因为如果更改设置并且弹出窗口再次出现,它将显示旧数据。
我在这里错过了什么?
如 Kayasax 在评论中所述,这只是一个 "out of scope" 问题。
将函数和散列表声明为全局后,它们再次完美运行。
# read config
function global:readcfg
{
Get-Content -Path $cfg | foreach-object -begin { $global:conf = @{ } } -process {`
$key = [regex]::split($_, ':');
if (($key[0].CompareTo("") -ne 0) -and ($key[0].StartsWith("[") -ne $True))`
{ $global:conf.Add($key[0], $key[1]) }
}
}
我创建了一个简单的 GUI 来输入一些值,这些值在用户单击“确定”后立即存储在 .txt 文件中。 为了进行验证,我在弹出窗口 window.
中显示了刚刚创建的文件的数据及其输入因为我想在我用于项目的其他几个 .ps1 文件中使用配置文件的数据,所以我开始将内容移到全局文件中。ps1 文件.一切都很好,除了我无法再显示哈希表。
这是我的全局变量。ps1 :
# Wshell popup
$wshell = New-Object -ComObject Wscript.SHell
# read config
function readcfg
{
Get-Content -Path $cfg | foreach-object -begin { $conf = @{ } } -process {`
$key = [regex]::split($_, ':');
if (($key[0].CompareTo("") -ne 0) -and ($key[0].StartsWith("[") -ne $True))`
{ $conf.Add($key[0], $key[1]) }
}
}
这是我的设置 GUI 中的部分,如果按下“确定”按钮,就会执行该部分:
$cfgData = "
[Account]
Screds:" + $Admaccount.text + "
Spw:" + $PW + "
[Domainconfig]
Sdomain:" + $domain.text + "
SSearchBase:" + $searchBase.text + "
SdeactivatedUsers_OU:" + $deactivatedUsersOU.text + ""
Out-File -filepath $cfg -inputobject $cfgData -Force
Start-Sleep -s 1
$wshell.Popup("Settings saved:`nAccount: " + $conf.Screds + "`nDomain: " + $conf.Sdomain + "`nSearchBase: " + $conf.SSearchBase + "`ndeactivatedUsersOU " + $conf.SdeactivatedUsers_OU + "", 0, "Yarr...!", 0x0)
如果我将 get-Content 移出 readcfg 函数,我可以再次显示这些值。但这当然不是解决方案,因为如果更改设置并且弹出窗口再次出现,它将显示旧数据。
我在这里错过了什么?
如 Kayasax 在评论中所述,这只是一个 "out of scope" 问题。
将函数和散列表声明为全局后,它们再次完美运行。
# read config
function global:readcfg
{
Get-Content -Path $cfg | foreach-object -begin { $global:conf = @{ } } -process {`
$key = [regex]::split($_, ':');
if (($key[0].CompareTo("") -ne 0) -and ($key[0].StartsWith("[") -ne $True))`
{ $global:conf.Add($key[0], $key[1]) }
}
}