[玩转系统] 周五乐趣:随机 PowerShell 控制台
作者:精品下载站 日期:2024-12-14 07:42:52 浏览:13 分类:玩电脑
周五乐趣:随机 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() 方法的值是您看到的“...正在目标上执行操作...”消息的一部分的文本。文本是目标。
我所做的只是定义一个变量 $msg,以使代码行更易于阅读。正如您所看到的,添加您自己的 -WhatIf 支持并不困难。现在,如果您感到有点无聊,可以将其混合起来以获得新的视角。我什至添加了一个别名 src,以防您必须在控制台中输入而看不到您正在输入的内容。
祝周末愉快。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag