private void ListServiceControllers() { ImpersonationUtil.Impersonate(); if (textBoxServer.Text.Any()) { var searchFor = this.textBoxFilterServices.Text?.ToUpper(); string[] separator = { " OR " }; var searchItems = searchFor.Split(separator, StringSplitOptions.RemoveEmptyEntries); if (searchItems.Any()) { var result = GetServiceControllers(textBoxServer.Text, searchItems); this.dataGridView1.DataSource = result .Distinct() .OrderBy(v => v.DisplayName) .ToArray(); } else { this.dataGridView1.DataSource = GetServiceControllers(textBoxServer.Text); } for (int index = 0; index < this.dataGridView1.ColumnCount; index++) { this.dataGridView1.Columns[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; } } }
private void ListMSMQ() { ImpersonationUtil.Impersonate(); var connectionOptions = new ConnectionOptions { Username = AdminUser, Password = AdminPassword, Authentication = AuthenticationLevel.PacketPrivacy, Impersonation = ImpersonationLevel.Impersonate }; var path = $@"\\{this.textBoxMsmqServer.Text}\root\CIMv2"; var scope = new ManagementScope(path, connectionOptions); scope.Connect(); string queryString = String.Format("SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue"); if (this.textBoxMsmqFilter.Text.Any()) { var filterByName = $@"{textBoxMsmqFilter.Text}"; queryString = $"SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue WHERE Name like '%{filterByName}%'"; } var query = new ObjectQuery(queryString); var searcher = new ManagementObjectSearcher(scope, query); var queues = searcher.Get(); var servername = this.textBoxMsmqServer.Text.Substring(0, this.textBoxMsmqServer.Text.IndexOf('.')); var list = new List <msmq>(); foreach (var queue in queues) { // 'FormatName: Direct = os:uabe0map07.uabeprd.com\prd.ql.irsmes.bodgenerator_trigger' var msmq = new msmq(); msmq.QueueName = $"{queue.GetPropertyValue("Name")}"; msmq.MessagesInQueue = Convert.ToInt32(queue.GetPropertyValue("MessagesInQueue")); msmq.MessagesinJournalQueue = Convert.ToInt32(queue.GetPropertyValue("MessagesinJournalQueue")); msmq.BytesinQueue = $"{queue.GetPropertyValue("BytesinQueue")}"; msmq.BytesinJournalQueue = $"{queue.GetPropertyValue("BytesinJournalQueue")}"; if (msmq.QueueName.IndexOf("private", 0, StringComparison.OrdinalIgnoreCase) < 0 && msmq.QueueName.StartsWith(servername, StringComparison.OrdinalIgnoreCase)) { var messageQ = new MessageQueue($"FormatName:Direct=os:{msmq.QueueName}"); var myFilter = new MessagePropertyFilter(); myFilter.ClearAll(); myFilter.ArrivedTime = true; messageQ.MessageReadPropertyFilter = myFilter; try { var allMessages = messageQ.GetAllMessages(); msmq.OldestArrivedTime = allMessages.OrderBy(v => v.ArrivedTime).Select(v => v.ArrivedTime).FirstOrDefault(); msmq.NewestArrivedTime = allMessages.OrderBy(v => v.ArrivedTime).Select(v => v.ArrivedTime).LastOrDefault(); var thresHold = DateTime.Now.AddMinutes(-1); msmq.MessagesInQueueLongerThan1Minutes = allMessages.Where(v => v.ArrivedTime < thresHold).Count(); } catch { // Problaby security related. Cannot read messages from queue } } list.Add(msmq); } this.dataGridViewMSMQ.DataSource = list.OrderBy(v => v.QueueName).ToArray(); for (int index = 0; index < this.dataGridViewMSMQ.ColumnCount; index++) { this.dataGridViewMSMQ.Columns[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; } }