简体   繁体   English

需要帮助 - 第一个功能

[英]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:输出:

在此处输入图片说明

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.然后你就可以调用这个函数了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM