本文介绍了在 PowerShell 中动态使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我有一些预定义的变量,例如:
I have some predefined variables, eg:
$s1 = a
$s2 = b
...
$s[x] = x
我有一个脚本,它通过串行端口连接到设备,获取一些数据,拆分字符串并获取我需要的值.我需要使用它来获取预定义变量的字符串中的值.
I have a script that connects to a device via serial port, get some data, split the string and get a value I need. The value that I have from the string I needed to use it to get a predefined variable.
在 php 中,我这样做很容易:
In php I do it easily like this:
$var1 = a
$var2 = b
...
$var[x] = x
$i = something
echo ${"var_name$i"}
如何在 PowerShell 中执行相同操作?我试过了
How can I do the same in PowerShell? I tried
write-host $s$i
write-host $s{$i}
write-host ${s$i}
write-host ${"s$i"}
没有运气...我得到的要么是 $i
的值,要么是空白空间
with no luck...all I get is either the value of $i
or empty space
推荐答案
使用 Get-Variable
cmdlet:
Use the Get-Variable
cmdlet:
Write-Host $(Get-Variable "s$i" -ValueOnly)
这篇关于在 PowerShell 中动态使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论