Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to invoke the powershell command with "format-list" and "out-file" pipeline from c#?
    primarykey
    data
    text
    <p>Hi I'm working on a C# program to call exchange 2010 powershell cmdlets in remote runspace. The ps command is: </p> <blockquote> <p>"Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File 'C:\db.txt' -Encoding UTF8 -Width 8192".</p> </blockquote> <p>My code is similar to:</p> <pre><code> static int Main(string[] args) { const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"; const string COMMAND = "Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File 'C:\db.txt' -Encoding UTF8 -Width 8192"; System.Uri serverUri = new Uri("http://EX2010SVR1/powershell?serializationLevel=Full"); PSCredential creds = (PSCredential)null; // Use Windows Authentication WSManConnectionInfo connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds); try { using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo)) { rs.Open(); PowerShell psh = PowerShell.Create(); psh.Runspace = rs; psh.AddCommand(COMMAND); Collection results = psh.Invoke(); rs.Close(); } } catch (Exception ex) { System.Console.WriteLine("exception: {0}", ex.ToString()); } return 0; } </code></pre> <p>When I run the c# program on Win2008 R2 which is hosting exchange 2010 server, I always get exception:</p> <pre><code> System.Management.Automation.RemoteException: The term 'Format-List' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings) at System.Management.Automation.PowerShell.Invoke() at RemotePS.Program.Main(String[] args) </code></pre> <p>The program is working fine without "Format-List" and "Out-File" pipelines. The entire command is also working fine in exchange 2010 management shell. I also confirmed it's powershell 2.0 on the system.</p> <p>Could any one help to figure out what's going on? Any help is much appreciated.</p> <p>Tom</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload