• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to pass the name of a PowerShell script as a command line para

#1
How to pass the name of a PowerShell script as a command line parameter to be
run by anther PowerShell script -- or -- How can I call another PowerShell
script from a PowerShell script, when the name of the script to be called is
not known until runtime.

Eg.

....
# If the specified command is a PowerShell script, run it with the
current server as its only argument.
if ((Get-Item $fileToRun).Extension -eq '.ps1') {
. {".\$fileToRun"} \\$svr
}
# If the specified command is anything other than a PowerShell script,
run it with the current server as its only argument.
else {
cmd /c $fileToRun \\$svr
....

I can get it to work if I hard code in the name of the PS script, but the PS
script name needs to be a variable since it will passed in as a command line
parameter at runtime. Any ideas appreciated.
 
M

Marco Shaw [MVP]

#2
You can use the $myinvocation variable.

A detailed post here:
http://poshoholic.com/2008/03/18/po...rect-invocation-in-shared-powershell-scripts/

Marco

"ge" <ge@xxxxxx> wrote in message
news:F9E62E48-EE3D-40B8-B1A5-F845B4B820C6@xxxxxx

> How to pass the name of a PowerShell script as a command line parameter to
> be
> run by anther PowerShell script -- or -- How can I call another PowerShell
> script from a PowerShell script, when the name of the script to be called
> is
> not known until runtime.
>
> Eg.
>
> ...
> # If the specified command is a PowerShell script, run it with the
> current server as its only argument.
> if ((Get-Item $fileToRun).Extension -eq '.ps1') {
> . {".\$fileToRun"} \\$svr
> }
> # If the specified command is anything other than a PowerShell
> script,
> run it with the current server as its only argument.
> else {
> cmd /c $fileToRun \\$svr
> ...
>
> I can get it to work if I hard code in the name of the PS script, but the
> PS
> script name needs to be a variable since it will passed in as a command
> line
> parameter at runtime. Any ideas appreciated.