Powershell 显示服务器上所有用户的系统托盘图标

Powershell 显示服务器上所有用户的系统托盘图标,powershell,Powershell,我在脚本中添加了一段代码,在脚本运行期间(大约3-5小时)显示一个系统托盘图标 我希望登录到该服务器的其他用户能够看到该图标(这意味着脚本仍在运行),因此不会再次运行脚本 如何做到这一点 (这是显示系统托盘图标的代码剪报): 为此,您需要编写一个Windows服务应用程序,该应用程序实际上代表用户执行工作,然后在登录时在交互式会话中启动一个隐藏的GUI应用程序,该应用程序可以与服务通信,以允许用户运行脚本或显示它当前正在运行另一个脚本,通过系统托盘图标,您可以看到想法。然而,我担心这会使脚本复杂

我在脚本中添加了一段代码,在脚本运行期间(大约3-5小时)显示一个系统托盘图标

我希望登录到该服务器的其他用户能够看到该图标(这意味着脚本仍在运行),因此不会再次运行脚本

如何做到这一点

(这是显示系统托盘图标的代码剪报):


为此,您需要编写一个Windows服务应用程序,该应用程序实际上代表用户执行工作,然后在登录时在交互式会话中启动一个隐藏的GUI应用程序,该应用程序可以与服务通信,以允许用户运行脚本或显示它当前正在运行另一个脚本,通过系统托盘图标,您可以看到想法。然而,我担心这会使脚本复杂化(相当简单),所以我会问,你将如何处理这个问题本身?(登录到此服务器的其他用户需要知道脚本正在运行,而不是再次运行)这将是我的方法。您还可以使用全局变量来检测机器上的另一个进程是否已经在运行脚本,然后退出而不是继续运行
$code = @"
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace System
{
public class IconExtractor
{

 public static Icon Extract(string file, int number, bool largeIcon)
 {
  IntPtr large;
  IntPtr small;
  ExtractIconEx(file, number, out large, out small, 1);
  try
  {
   return Icon.FromHandle(largeIcon ? large : small);
  }
  catch
  {
   return null;
  }

 }
 [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
 private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);

}
}
'@

Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = [System.IconExtractor]::Extract("shell32.dll", 24, $true) 
$objNotifyIcon.Text = "Script is running..."
$objNotifyIcon.Visible = $True