如何从powershell函数内部运行参数传递的ps1脚本文件?

时间:2014-10-23 19:32:18

标签: powershell

我需要在PowerShell中编写一个接收一堆参数的函数,其中一个参数是ps1文件。我需要将此文件作为我的功能代码的一部分执行,但我不知道该怎么做。

这可能是一个非常愚蠢的细节,但我在试图搜索这个时失败了。

这是我目前的职能。我尝试在那里使用Invoke-Command,但它无效:

Function Start-Dsc {
    Param(
        [Parameter(Mandatory = $true)]
        [string] $configurationFile,

        [Parameter(Mandatory = $true)]
        [string] $configurationName,

        [Parameter()]
        [string] $configurationData,

        [Parameter(Mandatory = $true)]
        [string] $computerName
    )

    Begin {}
    Process 
    {
        Invoke-Command -Command "$configurationFile -ConfigurationData $configurationData";
        Start-DscConfiguration -Path ".\$configurationName" -ComputerName $computerName -Verbose -Wait
    }
    End{}
}

更新:

培根比特'帮助,我设法让它工作。最终的脚本与我最初发布的内容略有不同。这是最后的流程块:

Process 
{
    Invoke-Command -FilePath $configurationFile -ComputerName 'localhost';
    Invoke-Expression -Command "$configurationName -ConfigurationData $configurationData";
    Start-DscConfiguration -Path ".\$configurationName" -ComputerName $computerName -Verbose -Wait
}

1 个答案:

答案 0 :(得分:3)

参数是Invoke-Command中的单独选项。尝试:

Invoke-Command -Command "$configurationFile" -ArgumentList "-ConfigurationData $configurationData";

您可能还需要将-Command更改为-FilePath