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

[玩转系统] 看着我!

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

看着我!


[玩转系统] 看着我!

这是这个功能,然后我会解释如何使用它。

Function Invoke-Flasher {

<#
.Synopsis
Display a flashing message.

.Description
This command will present a flashing message that you can use at the end of a script or command to signal the user. By default the command will write a message to the console that alternates the background color between the current console color and a red. You can press any key to end the function and restore the original background color.

You can also use the -FullScreen switch which will alternate the background color of the entire PowerShell console. Be aware that you will lose any script output that was displayed on the screen.

This command will NOT work in the PowerShell ISE.
.Example
PS C:\> Get-Process | Sort WS -Descending | select -first 5 ; Invoke-Flasher

This is a command line example of what a basic script would look like. 
.Example
PS C:\> $data = get-eventlog Security ; Invoke-Flasher "Security logs retrieved." -fullscreen ; $data

This example uses the fullscreen parameter because the command output was saved to a variable.
.Notes
Last Updated: August 10, 2014
Version     : 0.9


Learn more:
 PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones2/)
 PowerShell Deep Dives (http://manning.com/hicks/)
 Learn PowerShell in a Month of Lunches (http://manning.com/jones3/)
 Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)


  ****************************************************************
  * 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.             *
  ****************************************************************

.Link
https://jdhitsolutions.com/blog/2014/08/look-at-me

.Link
Write-Host

.Inputs
None

.Outputs
None

#>

[cmdletbinding()]

Param(
[Parameter(Position=0)]
[string]$Text="The command has completed.",
[string]$Color = "red",
[Switch]$FullScreen
)

#save current background color
$bg = $host.ui.RawUI.BackgroundColor

$Running = $True

#set cursor position
$Coordinate = $host.ui.RawUI.CursorPosition

While ($Running) {

    if ($host.ui.RawUI.BackgroundColor -eq $bg) {
        $host.ui.RawUI.BackgroundColor = $color
        if ($FullScreen) { Clear-Host}
    }
    else {
        $host.ui.RawUI.BackgroundColor = $bg
       if ($FullScreen) { Clear-Host}
    }

    #set the cursor position
    $host.ui.rawui.CursorPosition=$Coordinate
    Write-Host "`n$Text Press any key to continue . . ."
    
    #see if a key has been pushed
    if ($host.ui.RawUi.KeyAvailable) {
        $key = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyUp,IncludeKeyDown")
        if ($key) {
          $Running = $False
        }
    } #if key available
      start-sleep -Milliseconds 500

} #while


$host.ui.RawUI.BackgroundColor = $bg

if ($FullScreen) { Clear-Host}

}

此函数仅适用于 PowerShell 控制台,不适用于 PowerShell ISE,因为它使用 $host.ui.rawui 中的 ReadKey() 方法来检测用户是否按下任何键。该函数的主要部分不断循环,直到按下某个键。每次主机 UI 的背景颜色都会在当前颜色和红色或您指定的任何控制台颜色之间切换。每次通过脚本都会写下您的文本和“按任意键继续”。我每次都使用主机的坐标属性写入屏幕上的同一位置,这样就不会滚动。

默认情况下,Write-Host 线将通过交替背景颜色来“闪烁”。或者您可以使用 -FullScreen 参数,该参数每次都会清除主机。如果您在脚本中使用此选项,请确保脚本的主要部分将数据保存在某处,因为您不会看到它。这是一个如何使用它的示例。

#dot source the function
. c:\scripts\Invoke-Flasher.ps1

cls
$computers = get-content computers.txt

$data =  Get-Process -computername $Computers | group Machinename -AsHashTable
$data.GetEnumerator() | foreach { $_.value | sort WS -Descending | Select -first 5 | format-table -group Machinename}

Invoke-Flasher

脚本的主要部分完成后,结果后会显示闪烁的消息。如果你想使用全屏方法,你可以尝试这样的方法:

#dot source the function
. c:\scripts\Invoke-Flasher.ps1

cls
$computers = get-content computers.txt

$data =  Get-Process -computername $Computers | group Machinename -AsHashTable
$output = $data.GetEnumerator() | foreach { $_.value | sort WS -Descending | Select -first 5 | format-table -group Machinename}

Invoke-Flasher -fullscreen

$output

当脚本的主要部分完成时,您将看到一个闪烁的屏幕,其中包含文本消息。按任意键,您就会得到结果。

我不得不说我对这个功能很感兴趣,并且已经可以想到一些改进它的方法。如果您有建议或觉得这有用,希望您告诉我。

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

取消回复欢迎 发表评论:

关灯