using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Host; using System.Management.Automation.Runspaces; using System.Text; namespace colas_correo { public partial class MainForm : Form { public MainForm() { InitializeComponent(); // ubica la ventana a la derecha y al medio this.StartPosition = FormStartPosition.Manual; this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, (Screen.PrimaryScreen.WorkingArea.Height - this.Height)/2); // Comienzo Timer timer1 = new Timer(); timer1.Interval = 60000; //60 segundos de intervalo de actualización timer1.Enabled = true; timer1.Tick += new System.EventHandler (OnTimerEvent); saca_datos(); leyenda_form.Text = "Última actualización: " + DateTime.Now.ToLongTimeString(); } public void OnTimerEvent(object source, EventArgs e) { saca_datos(); leyenda_form.Text = "Última actualización: " + DateTime.Now.ToLongTimeString(); } private void saca_datos() { lista_form.Items.Clear(); // edita la siguiente línea con el nombre DNS de tu servidor de correo string servidor = "el_nombre_de_tu_servidor_exchange"; RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); PSSnapInException snapInException = null; PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException); Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig); myRunSpace.Open(); Pipeline pipeLine = myRunSpace.CreatePipeline(); // comando powershell Command myCommand = new Command("Get-Queue"); //parámetros powershell CommandParameter param_server = new CommandParameter("server",servidor); myCommand.Parameters.Add(param_server); //mete en la tubería de ejecución pipeLine.Commands.Add(myCommand); //ejecución Collection commandResults = pipeLine.Invoke(); //resultados string cola = ""; string mensajes = ""; foreach (PSObject cmdlet in commandResults) { cola = cmdlet.Properties["NextHopDomain"].Value.ToString(); mensajes = cmdlet.Properties["MessageCount"].Value.ToString(); //saca datos en lista ListViewItem i = new ListViewItem(); i.Text = cola; i.SubItems.Add(mensajes); lista_form.Items.Add(i); } } } }