poisedforflight
poisedforflight

Reputation: 167

Need Help - First Function

I'm trying to write my first function and am having some issues. When I run the below I get no output. I feel like I'm missing something obvious but I'm not sure what.

function findModifiedFiles {
    [CmdletBinding()]
    param (
        [string]$dir,
        [int]$days
    )
    Process {
        Write-Host "Directory: " $dir
        Write-Host "Days: "$days
    }
}

Output:

enter image description here

Upvotes: 2

Views: 41

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25021

You ultimately need to load your function and then call the function to receive any output. Since your function is defined in a file, one way to load the function is by dot sourcing the file. Then you can simply call your function.

. .\modfilesTest.ps1
findModifiedFiles -dir c:\temp -days 7

An alternative is to not use a function at all just run the script with parameters. If we edit your file to contain the following, we can just call the script afterwards.

# modfilesTest.ps1 Contents
[CmdletBinding()]
param (
    [string]$dir,
    [int]$days
)
Process {
    Write-Host "Directory: " $dir
    Write-Host "Days: "$days
}

Now call the script with your parameters.

.\modfilesTest.ps1 -dir c:\temp -days 7

A third alternative is to just paste a function definition into your console. At that point, the function is loaded into your current scope. Then you can just call the function.

Upvotes: 2

Related Questions