Parameters with multiple arguments

Hello,

I'm trying to create a script which has parameters that can pass through multiple arguments.

For example, I have;

param ( [string]$variable
)

I want to be able to use $variable as an argument for multiple commands, if possible by running this command through the script;

Examples:

Script.ps1 -variable command start server
Script.ps1 -variable command stop server
Script.ps1 -variable command restart server

The 'command' part can also change, so could this be another argument as well?

I've delt with parameters before but always used them on a more basic approach, for example;

Script.ps1 -variable true -servername name

But the issue is that I have a variable which I want the user to be able to supply with multiple arguments depending on what they want to do.

Hope this makes sense!

Thanks in advance!

August 25th, 2015 8:34am

Hi Fred,

I think I need to be a bit clearer what I'm trying to achieve here for your understanding.

I'm trying to make a very user-friendly script that can control one of our programs and pass through commands to it. The programs commands are;

program.bat -command start cluster clustername
program.bat -command start member membername
program.bat -command stop cluster clustername
program.bat -command stop member membername

This is why I need to be able to run a single (initial command), for example -command (which I've now tried to a switch as a possible work-around). Then followed by arguments such as start/stop cluster clustername or start/stop member membername

We do currently have batch files which do this, they use the %1 %2 %3 %4 to pass the arguments through to the script which seems quite easy, however powershell's params seem a little more tricky to get this sort of thing working.

I tried the function method you suggested, but it didn't seem to pass the arguments when I ran script.ps1 Function-Name argument1 argument2 argument3

I hope this makes a little more sense!


Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 9:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics