Search code examples
functionpowershellparameterscall

PowerShell running functions from .ps1 with multiple parameters


I'm trying to run a function that is located within a .ps1 file. The function accepts two parameters that could be either a string or int. Here is my code:

Filename: SetFarmProp.ps1

Function SetFarm ($property_name, $property_value) `
{
    $farm = Get-SPFarm

    $farm.Properties.Add($property_name, $property_value)

    $farm.properties
}

When I go into my PowerShell session and type in

.\SetFarmProp.ps1
SetFarm "testkey" "testvalue1"

I get an error saying that the "SetFarm" is not a recognized name of a cmdlet, function, script file, or operable program.


Solution

  • Try dot sourcing:

    . .\SetFarmProp.ps1
    SetFarm "testkey" "testvalue1"
    

    Or just:

    .\SetFarmProp.ps1 "testkey" "testvalue1"
    

    If you modify your .ps1 file as:

    param ($property_name, $property_value)
    {
        $farm = Get-SPFarm
    
        $farm.Properties.Add($property_name, $property_value)
    
        $farm.properties
    }