我正在嘗試通過添加 Azure 帳戶 ID 在 Windows 10 中自定義我的 PS 提示。但是,它沒有按預期作業。
Azure 帳戶 ID 的輸出,結果如下:
[0.04 sec] > (Get-AzContext).Account.Id
david.maryo@jd.com
我希望上述命令的結果“Account.Id”應顯示在我的 PS 提示符中。但它沒有顯示必要的結果。有關資訊,請參閱下面的螢屏截圖。
當前 PS 提示的截圖:

Microsoft.PowerShell_profile.ps1
function prompt {
#Assign Windows Title Text
$host.ui.RawUI.WindowTitle = "Current Folder: $pwd"
#Configure current user, current folder and date outputs
$CmdPromptCurrentFolder = Split-Path -Path $pwd -Leaf
$CmdPromptUser = [Security.Principal.WindowsIdentity]::GetCurrent();
$Date = Get-Date -Format 'dddd hh:mm:ss tt'
# Test for Admin / Elevated
$IsAdmin = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
#Calculate execution time of last cmd and convert to milliseconds, seconds or minutes
$LastCommand = Get-History -Count 1
if ($lastCommand) { $RunTime = ($lastCommand.EndExecutionTime - $lastCommand.StartExecutionTime).TotalSeconds }
if ($RunTime -ge 60) {
$ts = [timespan]::fromseconds($RunTime)
$min, $sec = ($ts.ToString("mm\:ss")).Split(":")
$ElapsedTime = -join ($min, " min ", $sec, " sec")
}
else {
$ElapsedTime = [math]::Round(($RunTime), 2)
$ElapsedTime = -join (($ElapsedTime.ToString()), " sec")
}
#Decorate the CMD Prompt
Write-Host ""
Write-Host "Azure: (Get-AzContext).Account.Id"
Write-host ($(if ($IsAdmin) { 'Elevated ' } else { '' })) -BackgroundColor DarkRed -ForegroundColor White -NoNewline
Write-Host " USER:$($CmdPromptUser.Name.split("\")[1]) " -BackgroundColor DarkBlue -ForegroundColor White -NoNewline
If ($CmdPromptCurrentFolder -like "*:*")
{Write-Host " $CmdPromptCurrentFolder " -ForegroundColor White -BackgroundColor DarkGray -NoNewline}
else {Write-Host ".\$CmdPromptCurrentFolder\ " -ForegroundColor White -BackgroundColor DarkGray -NoNewline}
Write-Host " $date " -ForegroundColor White
Write-Host "[$elapsedTime] " -NoNewline -ForegroundColor Green
return "> "
} #end prompt function
uj5u.com熱心網友回復:
Tomalak在評論中暗示了這個問題(并顯示了一個替代解決方案),但讓我把它拼出來:
為了:
嵌入運算式或命令的輸出-
(Get-AzContext).Account.Id在你的情況下 -在內部"...",一個可擴展的(雙引號)字串,您需要將其包含在
$(...)子運算式運算子中:
Write-Host "Azure: $((Get-AzContext).Account.Id)"
有關 PowerShell 的可擴展字串(字串插值)的全面概述,請參閱此答案。
通常,只有以下字符是內部的元字符"..."- 所有其他字符都被視為文字(這解釋了為什么您在提示字串中逐字看到): (Get-AzContext).Account.Id)
`(反引號)PowerShell 的轉義字符 - 請參閱概念about_Special_Characters幫助主題。- 如果加倍,也
":""轉義一個",相當于`"
- 如果加倍,也
$,變數參考的開始(例如,$HOME)或子運算式($(...),如上)。
為了完整起見,以下是替代解決方案:
# String concatenation - the expression must be enclosed in (...)
Write-Host ('Azure: ' (Get-AzContext).Account.Id)
# The -f (string-formatting operator)
Write-Host ('Azure: {0}' -f (Get-AzContext).Account.Id)
# Tomalak's alternative, which relies on Write-Host space-concatenating
# its individual arguments.
# Note how a single expression can act as a command argument as-is.
Write-Host 'Azure:' (Get-AzContext).Account.Id
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/439101.html
標籤:视窗 电源外壳 windows-10
上一篇:使搜索運算式不貪婪
下一篇:避免在cmd輸出結束時換行?
