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

[玩转系统] 周五乐趣:随机 PowerShell 控制台

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

周五乐趣:随机 PowerShell 控制台


[玩转系统] 周五乐趣:随机 PowerShell 控制台

#requires -version 3.0

Function Set-RandomConsole {

<#
.Synopsis
Set the console foreground and background colors to random values.
.Description
This command will create a random foreground/background color combination for your PowerShell console. Use -Reset to restore settings to the first time you ran the command.

The command only works properly in the PowerShell console, NOT the PowerShell ISE.
.Parameter Reset
Reset console colors to original settings using the values in global variables $savedfg and $savedbg. You must run the command at least once to populate these variables.

.Example
PS C:\> set-randomconsole
Change console colors to a random setting. If this is the first time the command is run, current settings will be saved to global variables $savedfg and $savedbg.
.Example
PS C:\> set-randomconsole -reset
Console colors will be restored to original settings using the values in global variables $savedfg and $savedbg.
.Notes
Last Updated: July 8, 2014
Version     : 0.9

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************
.Inputs
NONE
.Outputs
NONE
#>

[cmdletbinding(SupportsShouldProcess)]
Param(
[switch]$Reset
)

Write-Verbose "Starting $($MyInvocation.Mycommand)"  

#Skip if running in the PowerShell ISE
if ($host.Name -match "ISE") {
    Write-Warning "This command only works in the PowerShell console."
    #bail out
    Return
}

#Save current colors if not already saved
if (-Not $global:savedbg) {
    #saved to a global variable
    Write-Verbose "Saving current background"
    $global:savedbg = $host.ui.RawUI.backgroundColor
}

if (-Not $global:savedfg) {
    #saved to a global variable
    Write-Verbose "Saving current foreground"
    $global:savedfg = $host.ui.RawUI.ForegroundColor
}

if ($reset) {
    Write-Verbose "Resetting console"
    $bg = $global:savedbg
    $fg = $global:savedfg
}
else {

    Write-Verbose "Generating random colors"
    #set background to a random color
    $bg = Get-Random ([enum]::GetNames([consolecolor]))
    
    #set foreground to a random color other than what is set as foreground
    $fg = Get-Random ([enum]::GetNames([consolecolor])) | Where {$_ -ne $bg} 

}

Try {
    Write-Verbose "Applying colors"
    Write-Verbose "Background = $bg"
    Write-Verbose "Foreground = $fg"
    #my code for -WhatiIf
    $msg = "Background = $bg / Foreground = $fg"
    if ($PSCmdlet.ShouldProcess( $msg  )) {
        $host.ui.rawui.backgroundcolor= $bg
        $host.ui.rawui.foregroundcolor = $fg

        #clear the host to apply the settings
        #comment this out if trying to trace using the verbose output
        Clear-Host
    } #whatif
}
Catch {
    Write-Warning "Failed to set console colors. Most likely you tried to reset without first changing anything."
}

Write-Verbose "Ending $($MyInvocation.Mycommand)"  

} #end function

#define an optional alias
Set-Alias -Name src -Value Set-RandomConsole

该函数在 PowerShell ISE 中无法正常工作,因此我在开头添加了一些代码来查看它是否在 ISE 中运行。如果是这样,该命令会显示警告并退出。在这种情况下,使用 Return 是完全可以接受的,因为我想在不执行任何操作的情况下返回管道。我本可以使用这样的命令:Return“此命令仅适用于 PowerShell 控制台。”但这会向管道写入一个字符串对象,并且我不希望任何东西进入管道。另外,我更喜欢对这样的消息使用 Write-Warning。

当您运行该命令时,它会测试全局范围中定义的一些变量 $savedfg 和 $savedbg 是否存在。您会注意到 global: 前缀的使用。如果未定义它们,则假设这是第一次运行该命令,并且将使用 $host.ui.rawui.foregroundcolor 和 $host.ui.rawui.backgroundcolor 的当前值定义变量价值观。稍后,如果您使用 -Reset,该命令将使用这些值来恢复您的原始设置。

否则,该命令将获取背景的 System.ConsoleColor 枚举的随机颜色,然后获取前景的另一个随机颜色,该颜色与随机选择的背景颜色不同。

当您查看代码时,您将看到我在 cmdletbinding 属性中指定该函数支持 ShouldProcess。也可以写成 [cmdletbinding(SupportsShouldProcess=$True)] 但这对我来说似乎多余。无论如何,进行更改的命令 $host.ui.rawui.backgroundcolor= $bg 本身并不知道有关 ShouldProcess 和 -WhatIf 的任何信息。但我可以使用 If 语句添加自己的代码。

 
$msg = "Background = $bg / Foreground = $fg"
    if ($PSCmdlet.ShouldProcess( $msg  )) {
        $host.ui.rawui.backgroundcolor= $bg
        $host.ui.rawui.foregroundcolor = $fg

        #clear the host to apply the settings
        #comment this out if trying to trace using the verbose output
        Clear-Host
    } #whatif

ShouldProcess() 方法的值是您看到的“...正在目标上执行操作...”消息的一部分的文本。文本是目标。

[玩转系统] 周五乐趣:随机 PowerShell 控制台

我所做的只是定义一个变量 $msg,以使代码行更易于阅读。正如您所看到的,添加您自己的 -WhatIf 支持并不困难。现在,如果您感到有点无聊,可以将其混合起来以获得新的视角。我什至添加了一个别名 src,以防您必须在控制台中输入而看不到您正在输入的内容。

祝周末愉快。

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

取消回复欢迎 发表评论:

关灯