user16004320
user16004320

Reputation:

call function from param powershell

I have a couple of functions I want to build out in a script file.

How can I call the functions from the command line using arguments? I know how to do this for variables but not for functions.

Here is my code so far:

param (
    $test
)

function Powershellversion{
    $PSVersionTable.PSVersion
}

$test = Powershellversion

I want something like this:

> powershelltests.ps1 Powershellversion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      1      3                  

Instead I get this:

> powershelltests.ps1 Powershellversion
# Nothing shows up

Thank you for your time,

Upvotes: 0

Views: 75

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

If you want to invoke a function based on a variable containing the function name, use the invocation operator &:

param(
  [string]$command
)

function Get-PowerShellVersion {
  return $PSVersionTable
}

& $command

At which point you can run:


> .\powershellscript.ps1 Get-PowerShellVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      1      3                  

Upvotes: 1

Related Questions