在PowerShell中连接字符串和表达式结果

EMP*_*EMP 7 powershell string-concatenation

我想在PowerShell中写出当前进程ID.这有效:

$processId = $([System.Diagnostics.Process]::GetCurrentProcess()).Id
Write-Output "My process ID is $processId"
Run Code Online (Sandbox Code Playgroud)

但是,如果可能的话,我想在一行中完成.替换$([System.Diagnostics.Process]::GetCurrentProcess()).Id变量似乎不会评估表达式.

And*_*erd 12

'My process id is {0}' -f [System.Diagnostics.Process]::GetCurrentProcess().Id
Run Code Online (Sandbox Code Playgroud)

如果我们使用自动变量:

'My process id is {0}' -f $pid
Run Code Online (Sandbox Code Playgroud)


Kei*_*ill 9

这可能有点简单:

$pid
Run Code Online (Sandbox Code Playgroud)

要么

"My process id is $pid"
Run Code Online (Sandbox Code Playgroud)

有关自动变量执行的更多信息:

man about_automatic_variables
Run Code Online (Sandbox Code Playgroud)


Tru*_*ill 7

Write-Output "My process ID is $([System.Diagnostics.Process]::GetCurrentProcess().Id)"
Run Code Online (Sandbox Code Playgroud)

基本上你只需要在Id之后移动右括号.