我需要在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{}
}
更新:
在Bacon Bits的帮助下,我成功了。不过,最终的脚本与我最初发布的有点不同。这是最后一个过程块:
Process
{
Invoke-Command -FilePath $configurationFile -ComputerName 'localhost';
Invoke-Expression -Command "$configurationName -ConfigurationData $configurationData";
Start-DscConfiguration -Path ".$configurationName" -ComputerName $computerName -Verbose -Wait
}
参数是Invoke-Command
中的一个单独选项。尝试:
Invoke-Command -Command "$configurationFile" -ArgumentList "-ConfigurationData $configurationData";
您可能还需要将-Command
更改为-FilePath
。