shreddish
shreddish

Reputation: 1764

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.

Upvotes: 1

Views: 14302

Answers (1)

CB.
CB.

Reputation: 60918

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
}

Upvotes: 4

Related Questions