Click here to Skip to main content
15,891,607 members
Home / Discussions / C#
   

C#

 
GeneralRe: Updating console textbox from multiple threads Pin
shivamkalra17-Jun-11 10:21
shivamkalra17-Jun-11 10:21 
QuestionHelp with c sharp and powershell exchange commands Pin
turbosupramk316-Jun-11 10:58
turbosupramk316-Jun-11 10:58 
AnswerRe: Help with c sharp and powershell exchange commands Pin
turbosupramk316-Jun-11 11:47
turbosupramk316-Jun-11 11:47 
AnswerRe: Help with c sharp and powershell exchange commands Pin
jschell16-Jun-11 12:07
jschell16-Jun-11 12:07 
GeneralRe: Help with c sharp and powershell exchange commands Pin
turbosupramk316-Jun-11 13:20
turbosupramk316-Jun-11 13:20 
AnswerRe: Help with c sharp and powershell exchange commands Pin
turbosupramk317-Jun-11 7:51
turbosupramk317-Jun-11 7:51 
GeneralRe: Help with c sharp and powershell exchange commands Pin
jschell17-Jun-11 11:40
jschell17-Jun-11 11:40 
GeneralRe: Help with c sharp and powershell exchange commands Pin
turbosupramk317-Jun-11 13:01
turbosupramk317-Jun-11 13:01 
Hi,

Here is what I have, and so far this is working. It is far from finished and sorry for all of the comments, but I just got it working. It works for get-mailbox -identity first.last, it works for get-alias and it works for test-servicehealth. It itterates through everything, but it is a start Smile | :)

I'm sorry for the post length, I thought that codeblock would limit the screen usage and put a scrollable window within the post. If anyone knows how to do that, please let me know.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using exchangePowerShell;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Management.Automation; // Add reference to System.Management.Automation
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;

namespace TempTest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //results = ems.RunspaceInvoke("Get-Mailbox bradley.wanzer");
            //results = ems.RunspaceInvoke("test-servicehealth -server ph-mb-05");

            //ICollection<PSObject> results2;
            //results2 = ems.RunspaceInvoke("test-servicehealth -server ph-mb-05").ToArray();

            //Command EMSCommand = new Command("test-servicehealth");

            //results = ems.RunspaceInvoke("test-servicehealth -server ph-mb-05");

            textBox1.Text = "";
            ExchangeManagementShellWrapper ems = ExchangeManagementShellWrapper.Instance;
            ICollection<PSObject> results;
            results = ems.RunspaceInvoke(textBox2.Text);
            try
            {
                foreach (PSObject item in results)
                {
                    List<string> propertyListTotal = new List<string>();
                    foreach (PSPropertyInfo itemSubProperty in item.Properties)
                    {
                        propertyListTotal.Add(itemSubProperty.ToString());
                    }

                    //List<string> propertyList2 = new List<string>();
                    foreach (string propertyListItem in propertyListTotal)
                    {
                        //string propertyL2 = propertyL.Substring(propertyL.IndexOf(" "));
                        //string propertyL3 = propertyL2.Remove(propertyL2.LastIndexOf(" "));
                        string propertyListItemTrimBack = propertyListItem.Remove(propertyListItem.LastIndexOf(" "));
                        string propertyListItemTrimFront = propertyListItemTrimBack.Substring(propertyListItemTrimBack.LastIndexOf(" "));

                        propertyListItemTrimFront = propertyListItemTrimFront.TrimStart(); // Trim any white space
                        propertyListItemTrimFront = propertyListItemTrimFront.TrimEnd(); // Trim any white space
                        PSPropertyInfo propertyInfo = (PSPropertyInfo)item.Properties[propertyListItemTrimFront];
                        if (propertyInfo != null)
                        {

                            if (!(propertyInfo.TypeNameOfValue == "System.String[]") && !(propertyInfo.TypeNameOfValue.Contains("System.Nullable")))
                            {
                                //MessageBox.Show("1. " + propL.Name.ToString() + ", 2. " + propL.Value.ToString());
                                if ((propertyInfo.Name != null) && (propertyInfo.Value != null))
                                {
                                    textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + propertyInfo.Value.ToString());
                                    textBox1.Text += System.Environment.NewLine;
                                }
                                else
                                {
                                    textBox1.Text += ("Property has a null value");
                                    textBox1.Text += System.Environment.NewLine;
                                }
                            }
                            if (propertyInfo.TypeNameOfValue == "System.String[]" && !(propertyInfo.TypeNameOfValue.Contains("System.Nullable")))
                            {
                                //string[] propertyLArr = Convert.(propL.Value.ToString();

                                IList propertyArrayList = (IList)propertyInfo.Value;
                                if (propertyArrayList.Count == 0)
                                {
                                    if ((propertyInfo.Name != null) && (propertyInfo.Value != null))
                                    {
                                        textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + propertyArrayList.Count);
                                        textBox1.Text += System.Environment.NewLine;
                                    }
                                    else
                                    {
                                        textBox1.Text += ("Property has a null value");
                                        textBox1.Text += System.Environment.NewLine;
                                    }
                                }

                                if (propertyArrayList.Count > 0)
                                {
                                    if ((propertyInfo.Name != null) && (propertyInfo.Value != null))
                                    {
                                        textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + propertyArrayList.Count);
                                        textBox1.Text += System.Environment.NewLine;
                                        foreach (string listItem in propertyArrayList)
                                        {
                                            //MessageBox.Show("1. " + propL.Name.ToString() + "2. " + listItem);
                                            textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + listItem);
                                            textBox1.Text += System.Environment.NewLine;
                                        }
                                    }
                                    else
                                    {
                                        textBox1.Text += ("Property has a null value");
                                        textBox1.Text += System.Environment.NewLine;
                                    }
                                }

                                //object systemStringArray = propL.Value;
                                //ArrayList arr2 = new ArrayList();
                                //arr2.Add(propL.Value);

                                //foreach (var arr2Value in arr2)
                                //{
                                //    MessageBox.Show("1. " + propL.Name.ToString() + "2. " + propL.Value.ToString());
                                //    MessageBox.Show(arr2Value.ToString());
                                //}
                            }
                        }
                        //propertyList2.Add(propL.ToString());
                    }
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }
        }
    }
}



using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation; // Add reference to System.Management.Automation
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;


namespace exchangePowerShell

{
   public sealed class ExchangeManagementShellWrapper
   {
      #region Variable Declaration
      private RunspaceConfiguration rc;         //RunspaceConfiguration
      private Runspace r;                       //Runspace
      private RunspaceInvoke ri;                //RunspaceInvoke
      private static readonly ExchangeManagementShellWrapper instance = new ExchangeManagementShellWrapper(); //Singleton instance
      #endregion
      private ExchangeManagementShellWrapper(){}

      public static ExchangeManagementShellWrapper Instance
      {
         get
         {
            return instance;
         }
      }

      private void InitializeRunspace()
      {
         //Setup Runspace environment for Exchange Management Shell
         AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolver);
         //Create RunspaceConfiguration
         rc = RunspaceConfiguration.Create();
         //Add PSSnapIn for Exchange 2007 and check for warnings
         PSSnapInException warning;
         PSSnapInInfo info = rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out warning);
         if (warning != null)
         {
            // A warning is not expected, but if one is detected
            // write the warning and return.
            System.Console.Write(warning.Message);
            return;
         }

         //Create and open runspace
         r = RunspaceFactory.CreateRunspace(rc);
         r.Open();
      }

      private void InitializeRunspaceInvoke()
      {
         //Create runspace if it has not already been created
         if (r == null) { InitializeRunspace(); }
         //Create RunspaceInvoke
         ri = new RunspaceInvoke(r);
      }

      public ICollection<PSObject> RunspaceInvoke(string EMSCommand)
      {
         //Create RunspaceInvoke if it has not already been created
         if (ri == null) { InitializeRunspaceInvoke(); }
         //Invoke the commmand and return the results
         return ri.Invoke(EMSCommand);
      }

      public ICollection<PSObject> RunspaceInvoke(string EMSCommand, IEnumerable input)
      {
         //Create RunspaceInvoke if it has not already been created
         if (ri == null) { InitializeRunspaceInvoke(); }
         //Invoke the commmand and return the results
         return ri.Invoke(EMSCommand, input);
      }

      public ICollection<PSObject> RunspaceInvoke(string EMSCommand, out IList errors)
      {
         //Invoke the RunspaceInvoke method with a null input
         return RunspaceInvoke(EMSCommand, null, out errors);
      }

      public ICollection<PSObject> RunspaceInvoke(string EMSCommand, IEnumerable input, out IList errors)
      {
         //Create RunspaceInvoke if it has not already been created
         if (ri == null) { InitializeRunspaceInvoke(); }
         //Invoke the commmand and return the results and errors
         return ri.Invoke(EMSCommand, input, out errors);
      }

      public ICollection<PSObject> PipelineInvoke(Collection<Command> EMSCommands, out PipelineReader<object> Errors)
      {
         //Create Runspace if it has not already been created
         if (r == null) { InitializeRunspace(); }
         //Create Pipeline and add the commands
         Pipeline pipeline = r.CreatePipeline();
         foreach (Command item in EMSCommands)
         {
            pipeline.Commands.Add(item);
         }
         //Invoke the commands and return the results and errors
         ICollection<PSObject> results = pipeline.Invoke();
         Errors = pipeline.Error;
         pipeline = null;
         return results;
      }

      public ICollection<PSObject> PipelineInvoke(Collection<Command> EMSCommands)
      {
         //Create Runspace if it has not already been created
         if (r == null) { InitializeRunspace(); }
         //Create Pipeline and add the commands
         Pipeline pipeline = r.CreatePipeline();
         foreach (Command item in EMSCommands)
         {
            pipeline.Commands.Add(item);
         }
         //Invoke the commands and return the results
         return pipeline.Invoke();
      }

      public ICollection<PSObject> PipelineInvoke(Command EMSCommand, out PipelineReader<object> Errors)
      {
         //Create Runspace if it has not already been created
         if (r == null) { InitializeRunspace(); }
         //Create Pipeline and add the command
         Pipeline pipeline = r.CreatePipeline();
         pipeline.Commands.Add(EMSCommand);
         //Invoke the command and return the results and errors
         ICollection<PSObject> results = pipeline.Invoke();
         Errors = pipeline.Error;
         pipeline = null;
         return results;
      }

      public ICollection<PSObject> PipelineInvoke(Command EMSCommand)
      {
         //Create Runspace if it has not already been created
         if (r == null) { InitializeRunspace(); }
         //Create Pipeline and add teh command
         Pipeline pipeline = r.CreatePipeline();
         pipeline.Commands.Add(EMSCommand);
         //Invoke the command and return the results
         return pipeline.Invoke();
      }

      public void Dispose()
      {
         //Close the Runspace and cleanup
         r.Close();
         ri = null;
         r = null;
      }

      private static System.Reflection.Assembly AssemblyResolver(object p, ResolveEventArgs args)
      {
         //Add path for the Exchange 2007 DLLs
         if (args.Name.Contains("Microsoft.Exchange"))
         {
            return Assembly.LoadFrom(Path.Combine("C:\\Program Files\\Microsoft\\Exchange Server\\bin\\", args.Name.Split(',')[0] + ".dll"));
         }
         else
         {
            return null;
         }
      }
   }
}

QuestionVoIP with C# on a 64Bit Windows 7 Pin
DJ-Boris16-Jun-11 9:44
DJ-Boris16-Jun-11 9:44 
AnswerRe: VoIP with C# on a 64Bit Windows 7 Pin
BobJanova17-Jun-11 0:21
BobJanova17-Jun-11 0:21 
QuestionFTP Problem Pin
Kevin Marois16-Jun-11 9:07
professionalKevin Marois16-Jun-11 9:07 
AnswerRe: FTP Problem [modified] Pin
Matt Meyer16-Jun-11 10:22
Matt Meyer16-Jun-11 10:22 
GeneralRe: FTP Problem Pin
Kevin Marois16-Jun-11 12:15
professionalKevin Marois16-Jun-11 12:15 
GeneralRe: FTP Problem Pin
Matt Meyer16-Jun-11 13:10
Matt Meyer16-Jun-11 13:10 
GeneralRe: FTP Problem Pin
Kevin Marois16-Jun-11 13:20
professionalKevin Marois16-Jun-11 13:20 
QuestionReverse (2-way) synchronization: DataGridView to Table (Database) Pin
Paladin200016-Jun-11 3:49
Paladin200016-Jun-11 3:49 
GeneralRe: Reverse (2-way) synchronization: DataGridView to Table (Database) Pin
Paladin200016-Jun-11 5:09
Paladin200016-Jun-11 5:09 
Questionhelp optimize functions Pin
Abbath134916-Jun-11 1:33
Abbath134916-Jun-11 1:33 
GeneralRe: help optimize functions Pin
Paladin200016-Jun-11 3:18
Paladin200016-Jun-11 3:18 
GeneralRe: help optimize functions Pin
Abbath134916-Jun-11 3:30
Abbath134916-Jun-11 3:30 
GeneralRe: help optimize functions Pin
Paladin200016-Jun-11 3:54
Paladin200016-Jun-11 3:54 
GeneralRe: help optimize functions Pin
V.16-Jun-11 3:55
professionalV.16-Jun-11 3:55 
GeneralRe: help optimize functions Pin
Paladin200016-Jun-11 4:12
Paladin200016-Jun-11 4:12 
GeneralRe: help optimize functions Pin
Abbath134916-Jun-11 4:48
Abbath134916-Jun-11 4:48 
AnswerRe: help optimize functions Pin
V.16-Jun-11 3:55
professionalV.16-Jun-11 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.