0

In a powershell cmdlet that I am writing in C#, I need to get the name of the script that has called me.

I have derived my cmdlet class from PSCmdlet; there is a ton of information attached to this class at runtime, but I don't see where I can get the information I am looking for.

Is it possible to get the script name? If so, where does it live?

Tim Dowty
  • 1,460
  • 13
  • 9

2 Answers2

2

The automatic variable $MyInvocation should contain the name of the script in the InvocationName property.

mjolinor
  • 66,130
  • 7
  • 114
  • 135
0

Thanks, mjolinor... put me on the right path.

MyInvocation.InvocationName gives you the name of the command that the cmdlet was invoked under, but the calling script name is right close by...

I found what I was looking for here (from within the PSCmdlet-derived class):

var callingScript = MyInvocation.ScriptName;

It contains the full path of the script that called the cmdlet.

Tim Dowty
  • 1,460
  • 13
  • 9