Вопрос

I have a custom C# PowerShell Cmdlet (inheriting from the Cmdlet base class) and I want to be able to identify if the "-Verbose" parameter was specified when running the Cmdlet. I realize that WriteVerbose will output when the -Verbose parameter is specified, but I would like to actually do some other code when -Verbose is specified (i.e. not output the Console.Write values when -Verbose is specified).

Thanks,

John

Это было полезно?

Решение

Check the cmdlet's bound parameters like so:

if (this.MyInvocation.BoundParameters.ContainsKey("Verbose"))
{
}

Другие советы

After much digging about, this works for me. Visual Studio 2013, Powershell 3.0 C# cmdlet using the PsCmdlet namespace. import-module .\mytest.dll, then mytest -verbose

blnVerbose = this.MyInvocation.Line.ToLower().Contains("-verbose");

If you are using the Microsoft.PowerShell.5.ReferenceAssemblies reference assembly the Verbose parameter is included in the Cmdlet base class. You can use WriteVerbose() to write when the switch is included. There is no need to handle it yourself any longer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top