0

When running my PowerShell script I want to call a function depending on the first argument which is passed along. For this I use a switch.

function veeamScript([string]$command) {
    switch($command)
    {
        install
        {
            install #calls a function which doesn't need arguments
        }
        create
        {
            create($name, $server, $username, $password)
        }
        Default
        {
            echo "The help text"
        }
    }
}

scriptName($command)

If I call the script like this

scriptName.ps1 create myname myserver theusername thepassword

It should calls this function

function create($name, $server, $username, $password) {
    $check=checkIfInstalled # This calls another function which works and is either true or false

    if ($check -eq $true)
    {
        echo "Name: $name"
        echo "Server: $server"
        echo "Username: $username"
        echo "Password: $password"
        ...
    } else
    {
        echo "ERROR ..."
    }
}

However the echoes are all "empty" (e.g. after the Name: ).

It looks like the arguments aren't being passed through the switch, much less to the function. I added an echo within the switch before the function is being called and the echo is also empty.

create
        {
            echo "$name" # Also tried it without the double quotation marks, didn't work
            create($name, $server, $username, $password)
        }

Does anyone know how I can call my script, let the switch decide which function is called (depending on the first argument) and pass on the rest of the arguments?

1
  • 1
    The proper way to call a function is to use space as the delimiter between the function and its parametesr. This allows the items to be parsed properly --> create $name $server $username $password. You will need your function defined in the code before the function call happens. With that said, since VeeamScript only has one parameter, only create will be bound in your script call because of the unquoted spaces. Commented Feb 1, 2021 at 15:59

1 Answer 1

2

Use the $args automatic variable and the @ splat operator:

# scriptName.ps1

# split arguments into first,rest
$command,$actualArguments = $args

# check that command name is valid
if($command -in 'create','install')
{
  # user provided a valid command name, let's execute it with the remaining args
  & $command @actualArguments
}
else
{
  # throw an error or show usage text
  "Usage: ..."
}

Now it will work with both named and positional parameter arguments, and you don't even need a switch :-)

For more information about splatting, see the about_Splatting help topic

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.