Search code examples
powershellpowershell-cmdlet

How to print the powershell cmdlet arguments inside the cmdlet class


Could someone help me log the actual PowerShell command issued along with arguments.

I have a cmdlet implemented in C# as follows

class GetMyCommand : PSCmdlet
{
    // parameter definitions
    ...

    void processRecord()
    {
        // Here I want print actual arguments passed to the cmdlet.
    }
}

if someone calls

Get-MyCommand -Name Hello -File "test.txt"

I just want to log the command in the processRecord function.

 "Get-MyCommand -Name Hello -File "test.txt""

Clearly I know the cmdlet name, but not sure how to print the exact string of arguments.

Environment.CommandLine works inside regular exes, but how to do the same in cmdlets

Thanks


Solution

  • Use the MyInvocation.Line property on PSCmdlet e.g.:

    void ProcessRecord() {
        var line = this.MyInvocation.Line
    }