问题
Hi I'm working on a C# program to call exchange 2010 powershell cmdlets in remote runspace. The ps command is:
"Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File 'C:\db.txt' -Encoding UTF8 -Width 8192".
My code is similar to:
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;
}
When I run the c# program on Win2008 R2 which is hosting exchange 2010 server, I always get exception:
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)
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.
Could any one help to figure out what's going on? Any help is much appreciated.
Tom
回答1:
I've got the same problem with the first embeded PowerShell I wrote. I look for a trace, but I can't find it anymore.
Here is something working for me that I adapt to your code :
static void Main(string[] args)
{
const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
const string COMMAND = @"get-process | format-List | Out-File -file c:\temp\jpb.txt";
System.Uri serverUri = new Uri("http://WM2008R2ENT/powershell?serializationLevel=Full");
PSCredential creds = (PSCredential)null; // Use Windows Authentication
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false,
"WM2008R2ENT",
5985,
"/wsman",
SHELL_URI,
creds);
try
{
using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo))
{
rs.Open();
Pipeline pipeline = rs.CreatePipeline();
string cmdLine;
cmdLine = string.Format("&{{{0}}}", COMMAND);
pipeline.Commands.AddScript(cmdLine);
Collection<PSObject> results = pipeline.Invoke();
rs.Close();
}
}
catch (Exception ex)
{
System.Console.WriteLine("exception: {0}", ex.ToString());
}
return;
}
Be carefull, I'am not using Exchange PowerShell
In the example I use pipeline, perhaps your problem comes from the way you pass the command.
回答2:
You can try to work with the 'Command'-Object.
Runspace rs = RunspaceFactory.CreateRunspace();
PowerShell ps = PowerShell.Create();
Pipeline pipeline = rs.CreatePipeline();
Command cmd1 = new Command("Get-MailboxDatabase");
cmd1.Parameters.Add("Server", "EX2010SVR1");
cmd1.Parameters.Add("Status");
pipeline.Commands.Add(cmd1);
Command cmd2 = new Command("Format-List");
cmd2.Parameters.Add("Property", "Identity, Guid, mounted, CircularLoggingEnabled, Recovery");
pipeline.Commands.Add(cmd2);
Command cmd3 = new Command("Format-List");
cmd3.Parameters.Add("FilePath", "C:\db.txt");
cmd3.Parameters.Add("Encoding", "UTF8");
cmd3.Parameters.Add("Width", "8192");
pipeline.Commands.Add(cmd3);
Collection<PSObject> output = pipeline.Invoke();
See also here: Invoking powershell cmdlets from C#
回答3:
I realize this is an old thread, but I wanted to present my findings, however short they are.
I ran into this same problem just recently with a colleague of mine. We managed to track the problem down to the missing runspaces. We also had to connect to the Microsoft.Exchange runspace and when we do it, the Format-List commandlet becomes unavailable. If we don't use the runspace, the commandlet works just fine.
We didn't get to solving it yet, but I intend to explore the possibility of using the RunspacePool instead of just Runspace, thus allowing the execution of both commandlets in the pipeline.
来源:https://stackoverflow.com/questions/6655651/how-to-invoke-the-powershell-command-with-format-list-and-out-file-pipeline