开发者

How to expand member-variables in Write-Host or double quotes?

开发者 https://www.devze.com 2023-03-06 08:35 出处:网络
I have written a PS script and for diagnos开发者_JS百科tic purpose am echoing messages to screen using Write-Host. This is fine as long as I have to expand normal variable like

I have written a PS script and for diagnos开发者_JS百科tic 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?


If you want to use property access within double-quoted strings, you need a subexpression:

"Foo $($bar.Property)"


Try this:

$dir = ls
Write-Host "Dir elements:" $dir.Length
0

精彩评论

暂无评论...
验证码 换一张
取 消