powershell - 如何扩展 Write-Host 或双引号中的成员变量?

标签 powershell

我已经编写了一个 PS 脚本,出于诊断目的,我使用 Write-Host 将消息回显到屏幕。只要我必须像这样扩展普通变量就可以了

Write-Host "Hello World, $name"

当我尝试回显某些成员变量时出现问题,如下所示

Write-Host "Hello World, $Person.Name"

这不会按预期扩展。接下来的解决方法是使用 temp 变量 如下

$personName  = $Person.Name
Write-Host "Hello World, $personName"

是否有一种优雅的方法可以在不使用临时变量的情况下执行此操作?

最佳答案

如果要在双引号字符串中使用属性访问,则需要一个子表达式:

"Foo $($bar.Property)"

关于powershell - 如何扩展 Write-Host 或双引号中的成员变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6034543/

相关文章:

powershell - 如何使用包含$ value的模式选择字符串

json - 使用 PowerShell for Packer 编辑 JSON

powershell - 在powershell控制台中插入空行

Powershell循环和替换(从文本文件读取)

powershell - 将object []传递到PowerShell中的函数中

windows - 将参数传递给 powershell 脚本

powershell - 将多个域添加到 azure 网站

sql-server - 确定给定命名空间是否存在 wmiobject 类

powershell - 为什么 Set-SQSQueueAttribute 失败并显示 'InvalidOperationException: Invalid value for the parameter Policy' 错误?

Azure powershell : find which principal created or modified a given principal