c#中的powers powershell命令如何
目标是获取Exchange 2010站点的最小数据库,因此我尝试从c#运行以下powershell命令,
Get-MailboxDatabase -server Exchange2010 -Status | select-object Name,DatabaseSize
我正在努力的问题是 – 如何管道Select子句命令。
这是我的尝试,
WSManConnectionInfo wsConnectionInfo = new WSManConnectionInfo(new Uri("https://" + ExchangeSite + "/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", getCredential()); wsConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; wsConnectionInfo.SkipCACheck = true; wsConnectionInfo.SkipCNCheck = true; rsRemoteRunspace = RunspaceFactory.CreateRunspace(wsConnectionInfo); rsRemoteRunspace.Open(); Pipeline pipeLine = rsRemoteRunspace.CreatePipeline(); Collection DatabaSize = null; Command myCommand = new Command("Get-MailboxDatabase"); myCommand.Parameters.Add("Server", "Exchange2010"); myCommand.Parameters.Add("Status", null); Command myCommand2 = new Command("Select-Object"); myCommand.Parameters.Add("Name"); myCommand.Parameters.Add("DatabaseSize"); pipeLineMB.Commands.Add(myCommand); pipeLineMB.Commands.Add(myCommand2); DatabaSize = pipeLine.Invoke();
但我得到了,
"A parameter cannot be found that matches parameter name 'Name'."
请记住,我不能使用SnapIn,因为代码必须在Exchange服务器上执行cmdlet的客户端计算机上运行。
欢迎任何建议。
编辑
我应用了yamen建议的修复程序,并且可以调用该命令但是当我尝试获取该值时,我得到:“对象引用未设置为对象的实例。”
请注意,我可以获取Servername和Name的值,但它在DatabaseSize中失败,所以我猜测’Status’标志没有正确设置,因为这个标志是启用此值的标志。
你的意思是:
Command myCommand2 = new Command("Select-Object"); myCommand2.Parameters.Add("DatabaseSize");
而不是这个:
Command myCommand2 = new Command("Select-Object"); myCommand.Parameters.Add("DatabaseSize");
注意第二行的myCommand2
?
无论如何,您可能会发现您实际使用的参数是Property
viz:
myCommand2.Parameters.Add("Property", "DatabaseSize");
不止一个:
var props = new string[] { "DatabaseSize", "ServerName", "Name" }; myCommand2.Parameters.Add("Property", props);
我只是试过这个,有一个类似的场景
dir | select Name
它不起作用。 给我同样的错误,说’Name’不是一个有效的参数。 然后我尝试了以下,它的工作原理
dir | select -first 3
翻译成
Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); Command dir = new Command("dir"); pipeline.Commands.Add(dir); Command select = new Command("select"); select.Parameters.Add("first", 3); pipeline.Commands.Add(select);
我想你需要找到DatabaseSize
值的参数名称。
- 易失性IEnlistmentNotification和TransactionScope.AsyncFlowEnabled = true
- 如何将ConfigurationManager.AppSettings与自定义部分一起使用?
- C#通用约束问题
- 如何知道谁杀了我的线程
- 当鼠标位于数据网格中时,显示数据网格行中每个项目的工具提示
- generics和System.Collections
- 可移植类库,相当于Dispatcher.Invoke或Dispatcher.RunAsync
- 将XML反序列化为对象 – XML文档中存在错误(0,0)
- C#LINQ Orderby – true / false如何影响orderby?