none
Getting verbose loggin from Powershell command when it is run from c#? RRS feed

  • Question

  • Hi,

    I made a code to run powershell command from C# to collect list of mailboxes from office 365.

    The command works fine to collect the get-mailbox.

    I want to write verbose logging to a text file when the command is being executed.

    I made code as below, but it does not work, rather I do not know how to capture verbose logging. Please help me on the same.
    Runspace runspace = RunspaceFactory.CreateRunspace();
    PowerShell powershell = PowerShell.Create();
    PSCommand command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", new Uri("https://ps.outlook.com/powershell/;));
    command.AddParameter("Credential", credential);
    command.AddParameter("Authentication", "Basic");


    powershell.Commands = command;


    runspace.Open();

    powershell.Runspace = runspace;

    Collection<PSSession> result = powershell.Invoke<PSSession>();

    powershell = PowerShell.Create();
    command = new PSCommand();
    command.AddCommand("Set-Variable");
    command.AddParameter("Name", "ra");
    command.AddParameter("Value", result[0]);
    powershell.Commands = command;
    powershell.Runspace = runspace;
    powershell.Invoke();


    powershell = PowerShell.Create();
    command = new PSCommand();
    const string getverbose = "("`$verbosepreference='continue'; write-verbose 42")";
    command.AddScript(string.Format(getverbose));
    powershell.Commands = command;
    powershell.Runspace = runspace;
    powershell.Invoke();


    powershell = PowerShell.Create();
    command = new PSCommand();
    const string getMailBoxScript = "get-mailbox";
    command.AddScript(string.Format(getMailBoxScript));
    powershell.Commands = command;
    powershell.Runspace = runspace;
    powershell.Invoke();

    I already checked http://stackoverflow.com/questions/1146575/how-to-capture-a-powershell-cmdlets-verbose-output-when-the-cmdlet-is-programma, but i do not get much lead from it.


    Thanlk you in advance.

    Sunday, September 8, 2013 5:27 PM

All replies