Reputation: 11
Hello Fellow programmers, can someone show me the way to pass -Confirm:$Y using a c# code. Here is my C# Code which doesn't accept the -Confirm parameter.
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("Remove-MailContact");
string DisplayName = "UniqueValue112";
command.AddParameter("Identity", DisplayName);
command.AddParameter("Confirm", true);
The equivalent powershell code is
Remove-MailContact -Identity $DisplayName -Confirm:$Y
Can someone tell me how to pass equivalent to -Confirm:$Y using C#?
Upvotes: 1
Views: 2681
Reputation: 73
You should use the following:
command.AddParameter("Confirm", new SwitchParameter(false));
This should work fine :)
Upvotes: 1
Reputation: 9266
You want to set the value of -Confirm
to false.
command.AddParameter("Confirm", false);
In PowerShell commands, -Confirm:$false
is the correct way to do this.
The reason -Confirm:$y
works is that $y
is (usually) an undefined variable, which evaluates to $null
, which becomes $false
when cast to Boolean. Obviously, this will fail in mysterious and hard-to-debug ways if some part of your script sets the variable $y
to a non-null value.
A note about the syntax: -Parameter:$value
is equivalent to -Parameter $value
for most PowerShell parameters. The former is required for [switch]
parameters such as -Confirm
, because they normally don't take a value, and thus will not bind to the next token on the command line.
Upvotes: 7