1.    04 Sep 2009 #1
    ge Guest

    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

  2.    04 Sep 2009 #2
    Marco Shaw [MVP] Guest

    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

    > 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


Similar Threads
Thread Forum
Command line execution in Powershell
I've written a script to build parameters to pass to a command that will be executed via the command line. The trouble I'm having is finding a...
PowerShell
noob: PS script fails, but runs from command line
I want to call RoboCopy from a PS script. When I run this line from the PS command line, it works great: robocopy C:\my\directory...
PowerShell
Running a command from inside a script, command line is corrupted
Hello, I am trying to give Powershell a chance, since it seems like it may have some promise. I have this powershell script that needs to run...
PowerShell
Call PS Script with Environment Variable in Path via Command Line
Hi, I have a very simple script in my documents that just echos it;s arguments. I.e <user profile>\My Documents\test.ps1--> echo $args I...
PowerShell
Running a Command line script from within powershell.
I have a 3rd party command line tool I need to run from within a powershell script. What is the current method for doing this? I need the calling...
PowerShell
Script from command-line
Ok... I feel as if this worked for me before. From Cmd.exe: powershell "c:\test test\test.ps1" doesn't work. It tells me it doesn't know...
PowerShell
Powershell.exe command-line syntax
Howdy, Hoping to try and accomplish something like this: powershell.exe -command "& {c:\script.ps1; if (!$?) {read-host - prompt "An error...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • PC Help Forum
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 21:19.
    .