开发者

PowerShell: function interpolation

开发者 https://www.devze.com 2023-02-09 02:56 出处:网络
I\'m a PowerShell beginner. What is the reason that one should use $() to get Write-Host to evaluate this function?

I'm a PowerShell beginner. What is the reason that one should use $() to get Write-Host to evaluate this function?

I could not 开发者_如何学Gofind a reason for this from the documentation.

PS C:\Temp> Write-Host [math]::round($diff.TotalMinutes, 2)
[math]::round 751681,102679735 2

___________________________________________________________________________________________________________

PS C:\Temp> Write-Host $([math]::round($diff.TotalMinutes, 2))
751681,1


You can remove the dollar sign and use just

Write-Host ([math]::round($diff.TotalMinutes, 2))

The brackets are needed for the parser so that first the expression is evaluated and then bound to parameter -Object. There are some rules when parser expects that the string will be expression and when it treats it as a string and passes it to the command without evaluating. More info can be found in PowerShell in Action.

Dollar sign is needed if there are more expressions separated by ;

Write-Host $(get-date; 1; "test")
0

精彩评论

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