挖掘PowerShell中的Console界面

来源:互联网 发布:医疗人工智能招聘 编辑:程序博客网 时间:2024/05/04 01:31
在我们使用PowerShell的时候,最常见到的必定是我们的PowerShell Console界面,比如你会看到默认深蓝色的底色,白色的字体以及其它各种你在Console界面中所见到的一切。

今天我们就来挖掘下PowerShell中Console界面的一些特性。我们可以使用如下System.Console类在PowerShell中查看下控制台的一些支持属性:

PS C:\Users\Administrator> [System.Console].Getmembers()|Foreach{$_.name}get_IsInputRedirectedget_IsOutputRedirectedget_IsErrorRedirectedget_Inget_Outget_Errorget_InputEncodingset_InputEncodingget_OutputEncodingset_OutputEncodingget_BackgroundColorset_BackgroundColorset_ForegroundColorget_ForegroundColorset_BufferHeightget_BufferHeightget_BufferWidthset_BufferWidthget_WindowHeightset_WindowHeightget_WindowWidthset_WindowWidthget_LargestWindowWidthget_LargestWindowHeightset_WindowLeftget_WindowLeftset_WindowTopget_WindowTopget_CursorLeftset_CursorLeftget_CursorTopset_CursorTopset_CursorSizeget_CursorSizeget_CursorVisibleset_CursorVisibleset_Titleget_Titleget_KeyAvailableget_NumberLockget_CapsLockget_TreatControlCAsInputset_TreatControlCAsInputBeepBeepClearResetColorMoveBufferAreaMoveBufferAreaSetBufferSizeSetWindowSizeSetWindowPositionSetCursorPositionReadKeyReadKeyadd_CancelKeyPressremove_CancelKeyPressOpenStandardErrorOpenStandardErrorOpenStandardInputOpenStandardInputOpenStandardOutputOpenStandardOutputSetInSetOutSetErrorReadReadLineWriteLineWriteToStringEqualsGetHashCodeGetTypeIsInputRedirectedIsOutputRedirectedIsErrorRedirectedInOutErrorInputEncodingOutputEncodingBackgroundColorForegroundColorBufferHeightBufferWidthWindowHeightWindowWidthLargestWindowWidthLargestWindowHeightWindowLeftWindowTopCursorLeftCursorTopCursorSizeCursorVisibleTitleKeyAvailableNumberLockCapsLockTreatControlCAsInputCancelKeyPress</span>

这里我们使用了一个小技巧来获得关于[System.Console]类下面的成员,我们就拿其中的几项举例说明吧,比如CapsLock,这个成员类型是用于判断键盘是否开启了大写,我们可以按如下的方法使用:

<span style="color:#000000;">If([System.Console]::CapsLock){    Write-Host "Caps lock key is enabled." }Else{    Write-Host "Caps lock key is disabled."}


我们也可以设置Console界面的背景色,通过使用BackgroundColor成员属性来设置,但是如何知道PowerShell支持哪些颜色呢?下面的方法可以帮助你:)

PS C:\Users\Administrator> [System.Console]::BackgroundColor|Get-Member -Static -MemberType property   TypeName: System.ConsoleColorName        MemberType Definition----        ---------- ----------Black       Property   static System.ConsoleColor Black {get;}Blue        Property   static System.ConsoleColor Blue {get;}Cyan        Property   static System.ConsoleColor Cyan {get;}DarkBlue    Property   static System.ConsoleColor DarkBlue {get;}DarkCyan    Property   static System.ConsoleColor DarkCyan {get;}DarkGray    Property   static System.ConsoleColor DarkGray {get;}DarkGreen   Property   static System.ConsoleColor DarkGreen {get;}DarkMagenta Property   static System.ConsoleColor DarkMagenta {get;}DarkRed     Property   static System.ConsoleColor DarkRed {get;}DarkYellow  Property   static System.ConsoleColor DarkYellow {get;}Gray        Property   static System.ConsoleColor Gray {get;}Green       Property   static System.ConsoleColor Green {get;}Magenta     Property   static System.ConsoleColor Magenta {get;}Red         Property   static System.ConsoleColor Red {get;}White       Property   static System.ConsoleColor White {get;}Yellow      Property   static System.ConsoleColor Yellow {get;}

我们使用Get-Member方法获得出所有静态的属性,这就是PowerShell所支持的全部的背景色了。



0 0
原创粉丝点击