[玩转系统] 更新了 PowerShell 中的控制台图形
作者:精品下载站 日期:2024-12-14 07:40:45 浏览:13 分类:玩电脑
更新了 PowerShell 中的控制台图形
前几天,杰出工程师兼 PowerShell 教父 Jeffrey Snover 发表了一篇关于 Write-Host 罪恶的博客文章。他的观点是 Write-Host 是一个特殊情况的 cmdlet,这一点得到了许多人的同意。在他的文章中,他提到控制台绘图作为示例。我今年早些时候写了一个这样的脚本。斯诺弗先生的帖子引起了人们对我的帖子的一些新关注,我意识到它需要一点润色。
这是该脚本的修订版本。
#requires -version 3.0
Function Out-ConsoleGraph {
<#
.Synopsis
Create a console-based chart
.Description
This command takes objects and creates a horizontal bar graph based on the
property you specify. The property should return a numeric value. This command
does NOT write anything to the pipeline. All output is to the PowerShell host.
The default behavior is to use the same color, Green, for all graphed values.
But you can specify conditional coloring using -HighColor, -MediumColor and
-LowColor. If you specify one you must specify all three. The maximum available
graph value is divided into thirds. The top third will be considered high, the
next third medium and the rest low.
The final option is to send the graph results to Out-Gridview. But, you cannot
use conditional formatting nor specify a graph color. However, the grid view will
include the property value.
This command should work in both the Windows PowerShell console and PowerShell
ISE. There is no guarantee it will work with any other PowerShell host.
.Parameter Property
The name of the property to graph.
.Parameter CaptionProperty
The name of the property to use as the caption. The default is Name.
.Parameter Title
A string for the title of your graph. The default is <Property> Report - <date>
.Parameter DefaultColor
The default console color to use for the graph
.Parameter HighColor
The console color to use for the top 1/3 of graphed values.
.Parameter MediumColor
The console color to use for the middle 1/3 of graphed values.
.Parameter LowColor
The console color to use for the bottom 1/3 of graphed values.
.Parameter ClearScreen
Clear the screen before displaying the graph. The parameter has an alias of cls.
.Parameter GridView
Create a graph using Out-Gridview. The parameter has an alias of ogv
.Example
PS C:\> Get-Process | Out-ConsoleGraph -property WorkingSet -clearscreen
.Example
PS C:\> $computer=$env:computername
PS C:\> Get-CimInstance Win32_logicaldisk -filter "drivetype=3" -computer $computer |
out-ConsoleGraph -property Freespace -Title "FreeSpace Report for $computer on $(Get-Date)"
This example assumes the computer has more than one logical disk.
.Example
PS C:\> get-vm | where state -eq 'running' | out-consolegraph -Property MemoryAssigned -GraphColor Red
Get all running virtual machines using the Hyper-V Get-VM cmdlet and display a graph
depicting MemoryAssigned.
.Example
PS C:\> "chi-dc01","chi-dc02","chi-dc04","chi-fp02" | foreach -Begin {cls} {
$computer=$_
Get-CimInstance win32_logicaldisk -filter "deviceID='C:'" -ComputerName $computer |
Select Caption,PSComputername, @{Name="PercentFree";Expression={ [int](($_.FreeSpace/$_.Size)*100)}}
} | Out-ConsoleGraph -property PercentFree -title "Freespace Report - $(Get-Date)" -CaptionProperty PSComputername -HighColor DarkGreen -MediumColor White -LowColor Red
This example will create a console graph showing percent free space on Drive C: for several computers.
.Example
PS C:\> get-process | where {$_.cpu} | out-consolegraph CPU -high Red -medium magenta -low yellow
.Example
PS C:\> get-process | where {$_.cpu} | out-consolegraph CPU -high Red -medium magenta -low yellow -verbose 4>&1>verb.txt
Run the previous example to create a conditional color chart and send verbose data to a text file.
.Example
PS C:\> get-process | where {$_.cpu} | Sort CPU -descending | Out-Consolegraph CPU -Caption ID -Grid
Very similar to previous example except output is to Out-Gridview and the process ID is
displayed instead of the process name.
.Example
PS Scripts:\> (dir *.txt).where{$_.length -gt 50KB} | ocg length
This is a PowerShell 4.0 example that will create a console graph for all .txt
files in the Scripts folder greater than 50KB in size. this example is using the
optional ocg alias for Out-ConsoleGraph.
.Example
PS C:\> $PSDefaultParameterValues.Add("Out-ConsoleGraph:HighColor","Red")
PS C:\> $PSDefaultParameterValues.Add("Out-ConsoleGraph:MediumColor","Yellow")
PS C:\> $PSDefaultParameterValues.Add("Out-ConsoleGraph:LowColor","Green")
PS C:\> (dir c:\scripts\*.txt).where{$_.length -gt 50KB} | Add-member -membertype Aliasproperty -Name Size -value Length -passthru | ocg Size -Title "C:\Scripts Text Report $((Get-Date).ToShortDateString())"
This command creates default parameter values for conditional colors so you don't
have to specify them. It also creates an alias property of Size for the Length
property and uses that instead. The .Where syntax requires PowerShell 4.0.
.Link
Write-Host
Out-Gridview
.Link
https://jdhitsolutions.com/blog/2013/12/updated-console-graphing-in-powershell
.Inputs
Object
.Outputs
None
.Notes
Version: 3.1
Updated: 12/9/2013
Author : Jeffery Hicks (https://jdhitsolutions.com/blog)
Discover and Learn PowerShell:
-> Learn Windows PowerShell 3 in a Month of Lunches
-> Learn PowerShell Toolmaking in a Month of Lunches
-> PowerShell in Depth: An Administrator's Guide
-> PowerShell Deep Dives
#>
[cmdletbinding(DefaultParameterSetName="Single")]
Param (
[parameter(Position=0,Mandatory=$True,HelpMessage="Enter a property name to graph")]
[ValidateNotNullorEmpty()]
[string]$Property,
[parameter(Position=1,ValueFromPipeline=$True)]
[object]$Inputobject,
[string]$CaptionProperty="Name",
[string]$Title="$Property Report - $(Get-Date)",
[Parameter(ParameterSetName="Single")]
[ValidateNotNullorEmpty()]
[Alias("graphColor")]
[System.ConsoleColor]$DefaultColor="Green",
[Parameter(ParameterSetName="Conditional",Mandatory=$True)]
[ValidateNotNullorEmpty()]
[System.ConsoleColor]$HighColor,
[Parameter(ParameterSetName="Conditional",Mandatory=$True)]
[ValidateNotNullorEmpty()]
[System.ConsoleColor]$MediumColor,
[Parameter(ParameterSetName="Conditional",Mandatory=$True)]
[ValidateNotNullorEmpty()]
[System.ConsoleColor]$LowColor,
[alias("cls")]
[switch]$ClearScreen,
[Parameter(ParameterSetName="Grid")]
[Alias("ogv")]
[switch]$GridView
)
Begin {
Set-StrictMode -Version latest
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
Write-Verbose -Message "Parameter set $($pscmdlet.ParameterSetName)"
#get the current window width so that our lines will be proportional
$Width = $Host.UI.RawUI.BufferSize.Width
Write-Verbose "Width = $Width"
#initialize an array to hold data. We will process all the data at the end.
$data=@()
if ($pscmdlet.ParameterSetName -eq 'Grid') {
Write-Verbose "Initializing gvData"
$gvData = @()
}
} #begin
Process {
#get the data from the pipelined input and add it to the array
$data += $Inputobject
} #end process
End {
#get largest property value
Write-Verbose "Getting largest value for $property"
Try {
<#
Modified this original line per Lee Holmes to handle piped objects that
might not have the same property such as Directory and File.
$largest = $data | sort $property | Select -ExpandProperty $property -last 1 -ErrorAction Stop
#>
$largest = $data | Foreach-Object { $_.$property } | Sort-Object | Select-Object -last 1
Write-Verbose $largest
}
Catch {
Write-Warning "Failed to find property $property"
#bail out of the command
Return
}
If ($largest) {
#get length of longest object property used for the caption so we can pad
#This must be a string so we can get the length
Write-Verbose "Getting longest value for $CaptionProperty"
$sample = $data |
Sort-object -Property @{Expression={($_.$CaptionProperty -as [string]).Length}} |
Select-Object -last 1
Write-Verbose ($sample | out-string)
[int]$longest = ($sample.$CaptionProperty).ToString().length
Write-Verbose "Longest caption is $longest"
#get remaining available window width, dividing by 100 to get a
#proportional width. Subtract 4 to add a little margin.
$available = ($width-$longest-4)/100
Write-Verbose "Available value is $available"
#calculate high, medium and low ranges based on available
$HighValue = ($available*100) * 0.6666
$MediumValue = ($available*100) * 0.3333
#low values will be 1 to $MediumValue
Write-Verbose "High value will be $HighValue"
Write-Verbose "Medium value will be $MediumValue"
if ($ClearScreen) {
Clear-Host
}
Write-Host "`n$Title`n"
foreach ($obj in $data) {
#define the caption
[string]$caption = $obj.$captionProperty
<#
calculate the current property as a percentage of the largest
property in the set. Then multiply by the remaining window width
#>
if ($obj.$property -eq 0) {
#if property is actually 0 then don't display anything for the graph
[int]$graph=0
}
else {
$graph = (($obj.$property)/$largest)*100*$available
}
if ($graph -ge 2) {
[string]$g=[char]9608
}
elseif ($graph -gt 0 -AND $graph -le 1) {
#if graph value is >0 and <1 then use a short graph character
[string]$g=[char]9612
#adjust the value so something will be displayed
$graph=1
}
Write-Verbose "Graph value is $graph"
Write-Verbose "Property value is $($obj.$property)"
#send to Out-Gridview if specified
if ($pscmdlet.ParameterSetName -eq "Grid") {
#add each object to the gridview data array
$gvHash = [ordered]@{
$CaptionProperty = $caption
$Property = ($g*$graph)
Value = $obj.$Property
}
$gvData += New-Object -TypeName PSObject -Property $gvHash
}
Else {
Write-Host $caption.PadRight($longest) -NoNewline
#add some padding between the caption and the graph
Write-Host " " -NoNewline
if ($pscmdlet.ParameterSetName -eq "Single") {
$GraphColor = $DefaultColor
}
else {
#using conditional coloring based on value of $graph
if ($Graph -ge $HighValue) {
$GraphColor = $HighColor
}
elseif ($graph -ge $MediumValue) {
$GraphColor = $MediumColor
}
else {
$GraphColor = $LowColor
}
} #else console
Write-Host ($g*$graph) -ForegroundColor $GraphColor
} #not gridview
} #foreach
#add a blank line
Write-Host `n
} #if $largest
if ($pscmdlet.ParameterSetName -eq "Grid") {
Write-Verbose "Sending data to Out-Gridview"
$gvData | Out-GridView -Title $Title
}
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
} #end Out-ConsoleGraph
#define an optional alias
Set-Alias -Name ocg -Value Out-ConsoleGraph
除了添加 Set-StrictMode 并修改一些 IF 语句来测试 ParameterSetName 而不是变量之外,我没有进行太多结构更改。使用 StrictMode,这是一件好事,但在我的早期版本中引起了问题。我还浏览并添加了一些新示例,包括一些 PowerShell 4.0。
该功能仍至少需要 PowerShell 3.0。但它允许你做这样的事情:
$computers = "chi-dc01","chi-dc02","chi-dc04","chi-fp02","chi-core01.globomantics.local","chi-app01"
$computers | foreach -Begin {cls} {
$computer=$_
Get-CimInstance win32_logicaldisk -filter "deviceID='C:'" -ComputerName $computer |
Select Caption,SystemName, @{Name="PercentFree";Expression={ [int](($_.FreeSpace/$_.Size)*100)}}
} | Out-ConsoleGraph -property PercentFree -title "Globomantics Freespace Report - $((Get-Date).ToShortDateString())" -CaptionProperty SystemName -HighColor DarkGreen -MediumColor magenta -LowColor Red
你所能做的就是看看这个,但有时,这就是你所需要的。
猜你还喜欢
- 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