2011-06-09 21 views
2

我刚刚开始使用Powershell。我有一个bat文件,它简单地启动了下面的PowerShell脚本,然后重新显示我感兴趣的服务状态,每5秒钟一次。它工作正常(虽然我可以使用一些关于如何使这个更清洁的指针),除了每当重新绘制屏幕时都有一个简短的恼人的闪烁。所以,我想改变这一点,以便睡眠间隔为1秒或500毫秒,但只有当内容改变时才重新进行绘画。或者,如果无条件重绘dos屏幕更容易,而不会导致其闪烁,那么我也会对该解决方案感到满意。也请帮我清理代码。我非常害怕PowerShell中的函数,变量等,因为当我尝试使用C族/ Python语法和构造时,PS经常对我大叫一声。 PS与Python,Java等不同,我还没有弄清楚它的哲学。Windows Powershell - 如何仅在更改时才重新显示某些Windows服务的状态?

# When you run this script, it will show a simple window with the status of the services; 

# Do we want to XYZ as well? 
# To assign $true value, use: 
#PowerShell.exe .\ShowServices.ps1 -showXYZ:$true 
#param([switch]$showXYZ=$false) 
param([switch]$showXYZ=$true) 

# Build a regex for services 
$servicesRegex = "Microsoft.*|Network.*" 
if ($showXYZ -eq $true) { $servicesRegex = $servicesRegex + "|XYZ.*" } 

# Controlling the appearance of the window 
$pshost = get-host 
$pswindow = $pshost.ui.rawui 

$newsize = $pswindow.buffersize 
$newsize.height = 3000 
$newsize.width = 50 
$pswindow.buffersize = $newsize 

$newsize = $pswindow.windowsize 
$newsize.height = 10 
$newsize.width = 50 
$pswindow.windowsize = $newsize 

$global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() 
#$global:ComputerName = gc env:computername 
#$pswindow.WindowTitle = "Service statuses for {0} on {1}." -f $CurrentUser.Name, $ComputerName 
$pswindow.WindowTitle = "Service statuses for {0}." -f $CurrentUser.Name 

# Clear the screen once 
clear 

# Formatting details. 
[int]$global:len1 = 35 
[int]$global:len2 = 8 
[int]$global:sleepInterval = 5 #seconds - I want this to be more frequent, but not annoying. 

function printHeader 
{ 
    Write-Host("") # Blank line 
    [string]$line = "{0,-$global:len1} {1,-$global:len2}" -f "Service Name", "Status" 
    Write-Host $line 
    Write-Host("_" * $global:len1 + " " + "_" * $global:len2) 
} 

function printService($serviceObject) 
{ 
    [string]$foreColor = "yellow" # Default color, if neither Stopped nor Running 
    if ($serviceObject.status -eq "Stopped") {$foreColor = "red" } 
    if ($serviceObject.status -eq "Running") {$foreColor = "green" } 
    [string]$outStr = "{0,-$global:len1} {1,-$global:len2}" -f $serviceObject.displayname, $serviceObject.status 
    Write-Host $outStr -foregroundcolor $foreColor #-backgroundcolor white 
} 

# The meat of it. 
while($true) 
{ 
    printHeader 
    Get-Service | Where-Object {$_.name -match $servicesRegex} | ForEach-Object { printService($_) } 
    Start-Sleep -s $global:sleepInterval # Sleep x seconds 
    clear 
} 

回答

3

试着改变你的脚本的最后一部分,以这样的:

# The meat of it. 
$data = @() 
while($true) 
{ 
    $new = Get-Service | Where-Object {$_.name -match $servicesRegex} 
    if (Compare-Object $data $new -Property Status) { 
     $data = $new 
     clear 
     printHeader 
     $data | ForEach-Object { printService($_) } 
    } 
    Start-Sleep -s $global:sleepInterval # Sleep x seconds  
} 
+0

谢谢,我会检查了这一点的明天。大廓形图片! – 2011-06-10 02:11:00

+0

@奇异,你是一个赢家。任何改进我的代码总体的建议。 – 2011-06-10 14:35:24