I\'m writing a PowerShell Cmdlet and using WriteDebug, but I want to write an object which requires an extra API call, and I\'d rather not make that call when debugging is t
To access the Debug flag from C# you should be able to do essentially the same thing as mjolinor suggests:
if (this.MyInvocation.BoundParameters.ContainsKey("Debug"))
{
... do something ...
}
However, note that you can specify the debug parameter with a value of false:
MyCmdlet -Debug:$false
To handle this case, you probably want to add something like this (from within the PSCmdlet):
bool debug = MyInvocation.BoundParameters.ContainsKey("Debug") &&
((SwitchParameter)MyInvocation.BoundParameters["Debug"]).ToBool();
In C# BoundParameters is a dictionary, so you could also use TryGetValue(), but note that the value of a switch (like Debug) is a SwitchParameter not a bool.
for further information see the following: