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