当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 修饰你的提示

作者:精品下载站 日期:2024-12-14 07:43:00 浏览:12 分类:玩电脑

修饰你的提示


[玩转系统] 修饰你的提示

PS C:\> $function:prompt
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

PS C:\>

这个提示来自 PowerShell v4,但我很确定它与 v3 中使用的函数相同。 PowerShell v2 有不同的功能。

PS C:\> $function:prompt
$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + $(Get-Location) + $(if ($nestedpromptlev
el -ge 1) { '>>' }) + '> '
PS C:\>

您是否注意到新功能有一个帮助链接?尝试一下:

帮助提示-在线

您将获得 about_prompts 帮助主题的在线版本。提示功能的伟大之处在于你可以更改它。多年来我发布了各种提示。但这里还有 4 个供您尝试。这些提示应该在 v3 及更高版本中起作用。大多数功能都是对标准提示的简单添加,并且应该适用于控制台和 ISE。要尝试提示,您可以将该函数粘贴到 PowerShell 会话中。要使其“永久”,请将其插入到您的 PowerShell 配置文件脚本中。

包括 PowerShell 版本

Function Prompt {
"PS $($psversiontable.psversion.major) $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

}

此提示会将 PowerShell 主要版本插入到您的提示中。

[玩转系统] 修饰你的提示

包括管理员

Function Prompt {

$default = "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
 Write-Host "[ADMIN] " -NoNewline -ForegroundColor Red
 $default
 }
 else {
  $default
 }

# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

}

此提示将测试您是否以管理员身份运行,如果是,则会以红色文本插入 [ADMIN]。

[玩转系统] 修饰你的提示

包含计算机名

Function Prompt {
"[$($env:computername)] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
}

您喜欢远程会话向您显示所连接的计算机的方式吗?为什么不一直这样呢?我所做的只是从 Computername 环境变量中插入本地计算机名。

[玩转系统] 修饰你的提示

自动导出命令历史记录

Function Prompt {

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

#define a log file with the host name, a time stamp and process ID
# WindowsPowerShellISEHost_20140715_7388.log
# the log file will go in the Windows PowerShell directory

$log = "{0}_{1:yyyyMMdd}_{2}.log" -f $host.name.Replace(" ",""),(get-date),$PID
$logfile = Join-Path -path $home\Documents\WindowsPowerShell -childpath $log

#only insert history if it is different than the last command ran
$mycmd = (Get-History -Count 1).CommandLine
 if ($mycmd -ne (Get-Content -Path $logfile -Tail 1)) {
   $mycmd | Out-File -FilePath $logfile -Encoding ascii -Append
 }

# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml
}

最后一个版本对转录进行了一些改动。当您运行脚本时,您会得到命令和结果。但也许您想要的只是记录您运行的所有命令。当然,您可以在会话结束时导出命令历史记录,但您必须记住这样做,如果超过最大历史记录计数,您将错过命令。在此提示中,每次按 Enter 时,它都会获取您运行的最后一个命令并将其附加到日志文件中。该日志文件在您的 PowerShell 目录中创建,并使用 PowerShell 主机的命名格式(不带空格)、时间戳 (年月日) 和当前 PowerShell 会话的进程 ID。这允许您使用单独的日志保留多个 PowerShell 会话。日志文件仅记录与您上次运行的命令不同的命令。这还允许您无需执行任何操作即可按 Enter 键,并且不会填充日志。

如果您临时粘贴了这些提示功能之一,但不喜欢它,您只需重新启动 PowerShell 即可获得原始提示。或者您可以使用此功能来恢复它。

Function Restore-Prompt {

#reset the original prompt or whatever you want to use as your default
Function global:Prompt {

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

} #original prompt
 
}

如果您正在尝试提示,可以很方便地将其放入您的 PowerShell 配置文件中。 Restore-Prompt 只是在全局范围内定义了一个新的 Prompt 函数。我使用的是默认的 PowerShell 提示符,但您可以将其更改为您想要的任何内容。

如果您在提示符下做了一些很酷的事情,我希望您能分享。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯