如何调用PowerShell命令与"格式列表"和"从文件"从C#管道?

编程入门 行业动态 更新时间:2024-10-25 11:25:45
本文介绍了如何调用PowerShell命令与"格式列表"和"从文件"从C#管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨我工作的一个C#程序调用的Exchange 2010 PowerShell命令远程运行空间。 ps命令是:

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 |格式列表身份,GUID安装, CircularLoggingEnabled,恢复|出文件'C:\db.txt。-Encoding UTF8 -Width 8192

"Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File 'C:\db.txt' -Encoding UTF8 -Width 8192".

我的代码是类似于:

static int Main(string[] args) { const string SHELL_URI = "schemas.microsoft/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("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; }

当我运行的Win2008 R2的C#程序,托管Exchange 2010服务器,我总是得到异常:

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)

该计划是没有格式列表和OUT-文件管道工作的罚款。整个命令也在Exchange 2010管理外壳做工精细。我也证实了它的PowerShell的2.0系统。

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.

汤姆

推荐答案

我已经得到了与第一嵌入式PowerShell的我写了同样的问题。我找了痕迹,但我无法找到它了。

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 = "schemas.microsoft/powershell/Microsoft.PowerShell"; const string COMMAND = @"get-process | format-List | Out-File -file c:\temp\jpb.txt"; System.Uri serverUri = new Uri("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; }

要当心,我'不使用Exchange PowerShell中

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.

更多推荐

如何调用PowerShell命令与&QUOT;格式列表&QUOT;和&QUOT;从文件&QUOT;从C#管道?

本文发布于:2023-05-25 10:57:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管道   命令   格式   文件   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!