如何扩展 Write-Host 或双引号中的成员变量?
我编写了一个 PS 脚本,出于诊断目的,我使用 Write-Host 将消息回显到屏幕。只要我必须像这样扩展普通变量就可以了
写入主机“Hello World,$name”
当我尝试回显某些成员变量时出现问题,如下所示
Write-Host "Hello World, $Person.Name"
这不会按预期扩展。接下来的解决方法是使用 temp 变量 如下所示,
$personName = $Person.Name
Write-Host "Hello World, $personName"
有没有一种优雅的方法可以在不使用临时变量的情况下执行此操作?
I have written a PS script and for diagnostic purpose am echoing messages to screen using Write-Host. This is fine as long as I have to expand normal variable like
Write-Host "Hello World, $name"
Problem starts when i try to echo some member variable as below
Write-Host "Hello World, $Person.Name"
This does not expand as expected. The work around that am following is, to use temp variable
as below
$personName = $Person.Name
Write-Host "Hello World, $personName"
Is there an elegant way of doing this with out the use of temp variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要在双引号字符串中使用属性访问,则需要一个子表达式:
If you want to use property access within double-quoted strings, you need a subexpression:
试试这个:
Try this: