在 Windows 10 中自定义 PS 提示显示意外结果
Customizing PS Prompt in Windows 10 shows unexpected Result
我正在尝试通过添加 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
Tomalak 在评论中暗示了这个问题(并显示了一个替代解决方案),但让我把它拼出来:
顺序:
嵌入表达式或命令的输出 - (Get-AzContext).Account.Id
,在你的例子中 - inside "..."
, 一个 expandable (double-quoted) string,
您需要将其包含在$(...)
中,subexpression operator:
Write-Host "Azure: $((Get-AzContext).Account.Id)"
有关 PowerShell 的可扩展字符串(字符串插值)的全面概述,请参阅 this answer。
一般来说,在"..."
中只有以下字符是元字符 - 所有其他字符都被视为literals(这解释了为什么您在提示字符串中看到 verbatim (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
我正在尝试通过添加 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
Tomalak 在评论中暗示了这个问题(并显示了一个替代解决方案),但让我把它拼出来:
顺序:
嵌入表达式或命令的输出 -
(Get-AzContext).Account.Id
,在你的例子中 - inside"..."
, 一个 expandable (double-quoted) string,您需要将其包含在
$(...)
中,subexpression operator:
Write-Host "Azure: $((Get-AzContext).Account.Id)"
有关 PowerShell 的可扩展字符串(字符串插值)的全面概述,请参阅 this answer。
一般来说,在"..."
中只有以下字符是元字符 - 所有其他字符都被视为literals(这解释了为什么您在提示字符串中看到 verbatim (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