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?
create $name $server $username $password. You will need your function defined in the code before the function call happens. With that said, sinceVeeamScriptonly has one parameter, onlycreatewill be bound in your script call because of the unquoted spaces.