gpt4 book ai didi

powershell - 组合变量 - PowerShell

转载 作者:行者123 更新时间:2023-12-02 18:25:46 25 4
gpt4 key购买 nike

有没有办法组合两个字符串并将其作为当前变量并从中获取数据?我不确定是否还有其他方法可以做到这一点。如果有的话请告诉我。

这是我的代码。

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')
ForEach ($Applications in $Apps) {
$AppToBeInstalled = '$App' + $_
$AppExt = '$AppExt' + $_
Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

我想要什么。

Application Google Chrome to be installed with extension .exe
Application Mozilla to be installed with extension .msi

但我只得到空结果。

Application  to be installed with extension 
Application to be installed with extension

最佳答案

这是可行的,尽管非常不正统,您可以使用 Get-Variable使用动态名称获取变量:

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')

ForEach ($Applications in $Apps) {
$AppToBeInstalled = (Get-Variable App$Applications).Value
$AppExt = (Get-Variable AppExt$Applications).Value
Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

> Application Google Chrome to be installed with extension .exe
> Application Mozilla to be installed with extension .msi

更常见的方法可能是使用 hashtable :

$map = @{
"Google Chrome" = ".exe"
"Mozilla" = ".msi"
}

ForEach ($key in $map.Keys) {
$AppToBeInstalled = $key
$AppExt = $map[$key]
Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

> Application Google Chrome to be installed with extension .exe
> Application Mozilla to be installed with extension .msi

关于powershell - 组合变量 - PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70221324/

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