locked
Executing Powershell script with parameters remotely RRS feed

  • Question

  • I am trying to have one of our APP servers to serve as a central PS script server.  Basically to have all the scripts we use run from a central point.

    I can run PS remotely etc but I'm having trouble passing parameters to a script the is running remotely.

    For example to run this script locally:

    Powershell.exe -file '.\Cleanup.ps1' -Path "D:\Shares\NZArchive" -Days 30 -Exclude "D:\Shares\NZArchive\CST"

    When running remotely though, say using 'Invoke-Command' how do I pass those same parameters to the script?

    Monday, August 19, 2013 3:13 AM

Answers

  • You can use the -ArgumentList parameter to Invoke-Command, but the arguments have to be passed by position, not by name.  Your Cleanup.ps1 script might need an update to make sure all the arguments can be passed by position, and the Invoke-Command call will need to make sure the order is right.  For example:

    # Assuming that the order of arguments is Path,Days,Exclude:
    Invoke-Command -FilePath '.\Cleanup.ps1' -ComputerName 'SomeComputer' -ArgumentList 'D:\Shares\NZArchive', 30, 'D:\Shares\NZArchive\CST'
    #

    • Proposed as answer by Al Dunbar Monday, August 19, 2013 2:54 PM
    • Marked as answer by mhouston100 Monday, August 19, 2013 9:41 PM
    Monday, August 19, 2013 3:30 AM

All replies

  • You can use the -ArgumentList parameter to Invoke-Command, but the arguments have to be passed by position, not by name.  Your Cleanup.ps1 script might need an update to make sure all the arguments can be passed by position, and the Invoke-Command call will need to make sure the order is right.  For example:

    # Assuming that the order of arguments is Path,Days,Exclude:
    Invoke-Command -FilePath '.\Cleanup.ps1' -ComputerName 'SomeComputer' -ArgumentList 'D:\Shares\NZArchive', 30, 'D:\Shares\NZArchive\CST'
    #

    • Proposed as answer by Al Dunbar Monday, August 19, 2013 2:54 PM
    • Marked as answer by mhouston100 Monday, August 19, 2013 9:41 PM
    Monday, August 19, 2013 3:30 AM
  • From other reading also I've confirmed, no named arguments... that's a shame, I love powershell but it has some really weird limitations sometimes...
    Monday, August 19, 2013 9:42 PM
  • You could get around this limitation by using System Frontier (without the Invoke-Command option). You would still run your scripts from a central location, but using a web interface. You'd also get the added benefit of having role based access control to determine who can run what scripts and against which machines.
    Saturday, October 19, 2013 2:52 PM