Powershell可以将计数器值放入系统托盘中吗?

时间:2014-06-23 10:01:42

标签: windows powershell performancecounter system-tray

我正在尝试在系统托盘上以数字方式监控我的Wifi适配器的吞吐量;像这样。 enter image description here

我想出了静态的Powershell查询

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

但是如何获得连续进纸以及如何将其放在系统托盘上? 我在Diskled软件中找到了备用解决方案。但它没有显示实际价值。

1 个答案:

答案 0 :(得分: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)
}