2

I have a PowerShell module that contains a number of common management and deployment functions. This is installed on all our client workstations. This module is called from a large number of scripts that get executed at login, via scheduled tasks or during deployments.

From within the module, it is possible to get the name of the calling script:

function Get-CallingScript {
    return ($script:MyInvocation.ScriptName)
}

However, from within the module, I have not found any way of accessing the parameters originally passed to the calling script. For my purposes, I'd prefer to access them in the form of a dictionary object, but even the original command line would do. Unfortunately, given my use case, accessing the parameters from within the script and passing them to the module is not an option.

Any ideas? Thank you.

2 Answers 2

3

From about_Scopes:

Sessions, modules, and nested prompts are self-contained environments, but they are not child scopes of the global scope in the session.

That being said, this worked for me from within a module:

$Global:MyInvocation.UnboundArguments

Note that I was calling my script with an unnamed parameter when the script was defined without parameters, so UnboundArguments makes sense. You might need this instead if you have defined parameters:

$Global:MyInvocation.BoundParameters
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - the trick of using the global MyInvocation variable solved it for me, but not quite in the way you suggested. In my tests, the UnboundArguments and BoundParameters were always empty. However, the $global:MyInvocation.MyCommand value did get populated with the full command line, and the parameters could be extracted from there.
1

I can see how this in general would be a security concern. For instance, if there was a credential passed to the function up the stack from you, you would be able to access that credential.

The arguments passed to the current function can be accessed via $PSBoundParameters, but there isn't a mechanism to look at the call stack function's parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.