0

I am trying to run Exchange cmdlets using System.Automation dll in C#.

In http://technet.microsoft.com/en-us/library/dd315325.aspx, they have said that for escaping single quotes, we basically need to append it with another single quote

For example,

[PS] C:\Windows\system32>Write-Host 'Live and let le''arn'
Live and let le'arn

However, when I try to do the same thing with my cmdlet,

New-Mailbox -Name 'user1.''.cn'

The new mailbox is actually created with name as user.''.cn. We would like it to be user.'.cn Code to execute this cmdlet is as follows:

AutomatedRunspace.Command command = new AutomatedRunspace.Command(cmdlet.Command);

foreach (CmdletParameter param in cmdlet.GetParameters())
{
    command.Parameters.Add(param.Name, param.Value);
}
pipeline.Commands.Add(command);

Is there anything we can do to correctly escape it?

Tilottama Gaat
  • 193
  • 3
  • 7

1 Answers1

0

When you're invoking cmdlets in C#, you need to also worry about the C# string quoting behavior. It's not clear in your question what exactly you're setting the Name parameter to in C# code. I would try this:

string nameArg = "user1.'.cn";

That is, the PowerShell API should be bypassing the parameter parsing phase since you're supplying the argument directly via the API.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369