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

    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 08, 2013 5:26 PM

All replies

  • Hi,

    Thanks for the question.

    By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command.

    More details:

    Write-Verbose

    http://technet.microsoft.com/en-us/library/hh849951.aspx

    Thanks.


    Best Regards
    Jeremy Wu

    Monday, September 09, 2013 8:05 AM
  • Thank you for the reply, but

    I used the below method,

    const string getverbose = "("`$verbosepreference='continue'; write-verbose 42")";

    command.AddScript(string.Format(getverbose));

    I want to use streams.verbose to get the verbose. That's where I'm stuck and does not how to implement it.

    Monday, September 09, 2013 8:15 AM
  • Hi,

    after setting the $verbosePreference and the Invoke() call you should be able to read the verbose stream like so:

    foreach (var i in powershell.Streams.Verbose)
    {
       Console.WriteLine("verbose output: {0}" , i);
    }

    Monday, September 09, 2013 8:41 AM
  • After running the below command, powershell.stream.verbose is 0; that means nothing is in verbose. Is the below command sequence correct?

    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();

    Monday, September 09, 2013 2:00 PM
  • Hi,

    I'm not a c# dev. but this is a simple working example:

    Runspace runspace = RunspaceFactory.CreateRunspace();
    var ps1 = PowerShell.Create();
    ps1.Runspace = runspace;
    ps1.AddCommand("Get-Process");
    runspace.Open();
    foreach (PSObject result in ps1.Invoke())
    {
       Console.WriteLine(
               "output: {0,-24}{1}",
               result.Members["ProcessName"].Value,
               result.Members["Id"].Value);
    }
    ps1.AddScript("$verbosepreference='continue'; write-verbose 42");
    ps1.Invoke();
    foreach (var i in ps1.Streams.Verbose)
    {
        Console.WriteLine("verbose output: {0}" , i);
    }
    runspace.Close();
    Console.ReadLine();

    Monday, September 09, 2013 3:25 PM
  • this is the output , I get I use count.

     $ps.Streams.Verbose.Count
    0

    Monday, September 09, 2013 6:30 PM
  • If I use the code from Dirk_74 I get the right verbose result!

    You are writing $ps.Streams.Verbose.Count. $ is PowerShell code not C# code!

    Are you on the right track?

    Please show complete code! Not needles fragments! We are no visionarys and dont have a crystal ball!


    Please click “Mark as Answer” if my post answers your question and click “Vote As Helpful” if my Post helps you.
    Bitte markiere hilfreiche Beiträge von mir als “Als Hilfreich bewerten” und Beiträge die deine Frage ganz oder teilweise beantwortet haben als “Als Antwort markieren”.
    My PowerShell Blog http://www.admin-source.info
    [string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
    German ? Come to German PowerShell Forum!

    Tuesday, September 10, 2013 5:55 AM
  • Hi Peter,

    I had given my complete code in very 1st post; Pls let me know, if I have any other details.

    Thank you.

    Saturday, September 14, 2013 6:00 PM
  • Hi KSK2000,

    Firstly, please try

    const string getverbose = "("$verbosepreference='continue'; write-verbose 42")";

    instead of

    const string getverbose = "("`$verbosepreference='continue'; write-verbose 42")";

    in your code and see how it works.

    Also, you can try Dirk_74’s code and check if it helps.

    Thanks.


    Best Regards
    Jeremy Wu

    Thursday, September 19, 2013 7:03 AM
  • Thank you for the help Jeremy.

    I already tried ; did not help.

    Please tell me whether this code/sequence is correct,

    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();

    Thursday, September 19, 2013 4:31 PM
  • Can you try just try to replace 

    const string getverbose = "("$verbosepreference='continue'; write-verbose 42")";

    with

    const string getverbose = "$verbosepreference='continue'";

    Also it should in the same run space. When you are creating Powershell.Create second time you are creating a new session where your prevision session variables dont hold good.

     
                   
    const string getverbose = "$verbosepreference='continue'"; 
                    ps.AddScript(string.Format(getverbose));
                    ps.Invoke();
                    ps.Commands.Clear();
                    const string importModule = "Import-Module NetSecurity"; 
                                  ps.AddScript(string.Format(importModule));
                    ps.Invoke();
    
                    foreach (var v in ps.Streams.Verbose)
                    {
                        Console.WriteLine(v.Message);
                    }

    Tuesday, March 25, 2014 8:22 PM