如何通过C#在powershell中传递一个应为$ null的参数

dra*_*eed 1 c# powershell exchange-server-2010

Exchange管理要求您传递$ null作为参数以从邮箱中删除转发:

Set-Mailbox -ForwardingAddress $ null -DeliverToMailboxAndForward $ false

这(恕我直言)转换为:

command = new PSCommand().AddCommand("Set-Mailbox");
          command.AddParameter("Identity",task.TargetUser);
          command.AddParameter("FowardingAddress", null);
          command.AddParameter("DeliverToMailboxAndForward", false);
ps.Commands=command;
var results = ps.Invoke();
Run Code Online (Sandbox Code Playgroud)

不幸的是,"ForwardingAddress"设置上的Invoke chokes:找不到与参数名称'FowardingAddress'匹配的参数.

如何传递最终为$ null的参数?

Kei*_*ill 7

如果该参数的类型为string,那么您应该能够传递此值:

System.Management.Automation.Language.NullString.Value
Run Code Online (Sandbox Code Playgroud)

或包括

using System.Management.Automation.Language;
Run Code Online (Sandbox Code Playgroud)

然后是

command.AddParameter("ForwardingAddress",NullString.Value);
Run Code Online (Sandbox Code Playgroud)

另一种选择是$null通过以下AddScript方法使用:

var script = String.Format("Set-Mailbox -Identity {0} -ForwardingAddress $null -DeliverToMailboxAndForward $false", task.TargetUser);
new PSCommand().AddScript(script);
Run Code Online (Sandbox Code Playgroud)