2014-06-23 57 views
1

我正試圖監視我的Wifi適配器的吞吐量數字在系統托盤上;像這樣。 enter image description herePowershell可以將計數器值放入系統托盤中嗎?

我想通了靜態PowerShell的查詢

((Get-Counter '\\mullick1\network interface(intel[r] centrino[r] advanced-n 6205)\bytes total/sec').countersamples).cookedvalue*8/102400000*100

但我怎麼能得到連續進料,如何把它放在系統盤? 我在Diskled軟件中找到了備用解決方案。但它沒有顯示實際價值。

+3

你需要創建一個WinFroms GUI,所以你可以使用['NotifyIcon']實例(http://msdn.microsoft.com/en-us/library/system.windows.forms .notifyicon.aspx)。然後使用計時器獲取計數器的新值並更新圖標。 – Richard

+0

@Richard我不太熟悉.Net編程。我是否需要編寫和編譯諸如[this](http://www.codeproject.com/Articles/74/Adding-Icons-to-the-System-Tray)? –

+2

看看[這篇博客文章](http://www.sapien.com/blog/2012/05/08/spotlight-on-the-notifyicon-control/)。 –

回答

1

這是在通知圖標上呈現(更新)文本的腳本。

如你所願自定義「Get-NotifyIconText」函數。

#Requires -Version 3.0 

function Get-NotifyIconText { 
    [DateTime]::Now.Second.ToString() 
# ((Get-Counter '\\mypc\network interface(Intel[R] 82579V Gigabit Network Connection)\bytes total/sec').countersamples).cookedvalue*8/102400000*100 
} 

Add-Type -ReferencedAssemblies @("System.Windows.Forms"; "System.Drawing") -TypeDefinition @" 
    using System; 
    using System.Drawing; 
    using System.Windows.Forms; 
    public static class TextNotifyIcon 
    { 
     // it's difficult to call DestroyIcon() with powershell only... 
     [System.Runtime.InteropServices.DllImport("user32")] 
     private static extern bool DestroyIcon(IntPtr hIcon); 

     public static NotifyIcon CreateTrayIcon() 
     { 
      var notifyIcon = new NotifyIcon(); 
      notifyIcon.Visible = true; 

      return notifyIcon; 
     } 

     public static void UpdateIcon(NotifyIcon notifyIcon, string text) 
     { 
      using (var b = new Bitmap(16, 16)) 
      using (var g = Graphics.FromImage(b)) 
      using (var font = new Font(FontFamily.GenericMonospace, 8)) 
      { 
       g.DrawString(text, font, Brushes.Black, 0, 0); 

       var icon = b.GetHicon(); 
       try 
       { 
        notifyIcon.Icon = Icon.FromHandle(icon); 
       } finally 
       { 
        DestroyIcon(icon); 
       } 
      } 
     } 
    } 
"@ 

$icon = [TextNotifyIcon]::CreateTrayIcon() 
while ($true) { 
    $text = Get-NotifyIconText 
    [TextNotifyIcon]::UpdateIcon($icon, $text) 
    [Threading.Thread]::Sleep(1000) 
} 
+0

退出時圖標不會消失。 –