Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

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

Reply
 
Old 09-04-2009   #1 (permalink)
ge


 
 

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

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.

My System SpecsSystem Spec
Old 09-04-2009   #2 (permalink)
Marco Shaw [MVP]


 
 

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

You can use the $myinvocation variable.

A detailed post here:
http://poshoholic.com/2008/03/18/pow...shell-scripts/

Marco

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

> 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.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
noob: PS script fails, but runs from command line PowerShell
Running a command from inside a script, command line is corrupted PowerShell
Running a Command line script from within powershell. PowerShell
Script from command-line PowerShell
Powershell.exe command-line syntax PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46