1

Exchange management requires you to pass $null as a parameter to remove forwarding from a mailbox:

Set-Mailbox -ForwardingAddress $null -DeliverToMailboxAndForward $false

This (IMHO) translates to:

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();

Unfortunately, the Invoke chokes on the "ForwardingAddress" setting: A parameter cannot be found that matches parameter name 'FowardingAddress'.

How can I pass a parameter that ends up as $null?

3
  • Have you tried just leaving it out? Commented Jan 20, 2015 at 18:42
  • @SteveMitcham - doesn't modify the forwarding if I don't set SOMETHING for ForwardingAddress Commented Jan 20, 2015 at 19:37
  • Sorry, misread the question. Commented Jan 20, 2015 at 19:44

2 Answers 2

8

If that parameter is of type string then you should be able to pass this value:

System.Management.Automation.Language.NullString.Value

or include

using System.Management.Automation.Language;

and then later

command.AddParameter("ForwardingAddress",NullString.Value);

Another option is to use $null via the AddScript method:

var script = String.Format("Set-Mailbox -Identity {0} -ForwardingAddress $null -DeliverToMailboxAndForward $false", task.TargetUser);
new PSCommand().AddScript(script);
Sign up to request clarification or add additional context in comments.

4 Comments

How would I put that in my c# code? I'm a bit perplexed.
System.Management.Automation.Language.NullString.Value
Sadly, it looked quite promising, but still comes back with the same error :(
Which turns out to be fat-finger syndrome on my part.
2

Passing null should work fine. As this question shows, C#'s nulls are mapped to $null.

Assuming the code you're showing is copy-pasted directly from your solution, I'm guessing the problem is that you're passing a value to the parameter "FowardingAddress", rather than "ForwardingAddress" (note the missing 'r'). A simple typo, not a type mismatch. :)

2 Comments

Urgh. Freeform parameter values are the death of me. Fixed! Thanks.
Good eye. I had to look at those two for about 30 seconds before I saw where the missing 'r' was supposed to be. Dang brain auto-correct. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.