Tim Dowty
Tim Dowty

Reputation: 1740

How to determine the name of the script that called my cmdlet?

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?

Upvotes: 0

Views: 256

Answers (2)

Tim Dowty
Tim Dowty

Reputation: 1740

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.

Upvotes: 0

mjolinor
mjolinor

Reputation: 68273

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

Upvotes: 2

Related Questions