在 PowerShell 中动态使用变量

编程入门 行业动态 更新时间:2024-10-24 01:56:09
本文介绍了在 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:关键词]

本文发布于:2023-04-28 06:33:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   动态   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!