Home > Software design >  How to call PowerShell function written in ps1 script?
How to call PowerShell function written in ps1 script?

Time:10-12

New to powershell, I want to write a PS function like this:

function InvokeTest {
    param (
        [string]$OutputPath,
        [string]$Version
    )
    $OutputPath
}

in terminal, I copied the function content to the prompt, then I can invoke the InvokeTest() direct with parameter -OutputPath. The result is ok.

PS D:\> function InvokeTest {
>>     param (
>>         [string]$OutputPath,
>>         [string]$Version
>>     )
>>     $OutputPath
>> }
PS D:\> 
PS D:\> InvokeTest -OutputPath "hello,world"
hello,world

I have a file named invokeTest.ps1 which contains only the function InvokeTest.

I know I can use ".\invokeTest.ps1" in terminal prompt to run the script but how can I specify the "outputpath" parameter ?

I tried these ways:

.\InvokeTest.ps1 -OutputPath "hello"
.\InvokeTest.ps1 InvokeTest -OutputPath "hello"

But they didn't work.

I know a way to solve this issue but it's not elegant,

function InvokeTest {
    param (
        [string]$OutputPath,
        [string]$Version
    )
    Write-Host $OutputPath,$Version
}
InvokeTest -OutputPath $args[0] -Version $args[1]

then use

PS D:\> .\InvokeTest.ps1 "hello,world","123"
hello,world 123 
PS D:\>

But if there are many parameters, one or more parameters may be optional, how can I specify parameters I really need to pass .

CodePudding user response:

In principle if you write functions you should store them in a module (*.psm1). Once you did this you can store the module in a location covered by the environment variable PsModulePath = no need to import the module. Otherwise you have to import the module:

import-module [path]

Once the module is loaded you can call all functions which it contains from the shell. You should always use named parameters, as you did, in functions. So the right call of the function would be:

invoketest -OutputPath [value] -Version [value]

About functions: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.2

About modules: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.2

About Parameters: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters?view=powershell-7.2

About PsModulePath: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepath?view=powershell-7.2

Also you should consider the format of functions:

[verb]-[noun]

To get a list of "approved" verbs:

get-verb

CodePudding user response:

You can make Function-File and call it like tmp1.ps1 -OutputPath 'Eshkere'

#FILE START
param (
    [string]$OutputPath="DefaultPath",
    [string]$Version="DefaultVersion"
)

Function Get-CapitalizedString([string]$Value) {
    return $Value.ToUpperInvariant()
}

    
Write-Host (Get-CapitalizedString -Value $OutputPath),$Version
#FILE END
  • Related