0

我正在编辑一个函数,它将直接在 VM 上调用命令。我一直遇到的问题是,如果有人将函数声明作为脚本块传递,我在调用 create 时会出错,因为params()它不在脚本块的顶部。

试图弄清楚我如何仍然可以set-fulllanguage先执行带有参数的函数。

function Invoke-DirectOnVM
{
    [CmdletBinding()]
    Param (
    [Parameter(Mandatory = $true)]
    [CloudEngine.Configurations.EceInterfaceParameters]
    $Parameters,

    [Parameter(Mandatory = $true)]
    [String[]]$VMNames,

    [Parameter(Mandatory = $true)]
    [Object]$VMCredential,

    [Parameter(Mandatory = $true)]
    [ScriptBlock]$ScriptBlock,

    [Object[]]$ArgumentList = $null
)
{
    Invoke-Command -VMName $localVMs -Credential $using:VMCredential -ScriptBlock ([ScriptBlock]::Create($("Import-Module OpenUpSession; Set-FullLanguage; `r`n" + $using:ScriptBlock)))
}
4

1 回答 1

0

从脚本块中删除$using:它应该可以正常工作。我冒昧地清理了一下代码。结果如下所示:

function Invoke-DirectOnVM
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory)]
        [CloudEngine.Configurations.EceInterfaceParameters]
            $Parameters,

        [Parameter(Mandatory)]
        [String[]]
            $VMNames,

        [Parameter(Mandatory)]
            $VMCredential,

        [Parameter(Mandatory)]
        [ScriptBlock]
            $ScriptBlock,

        [Parameter()]
        [Object[]]
            $ArgumentList = $null
    )

    $PSBoundParameters.Remove("ScriptBlock")

    Invoke-Command @PSBoundParameters -ScriptBlock ([ScriptBlock]::Create( "Import-Module OpenUpSession; Set-FullLanguage; `r`n" + $ScriptBlock ))
}
于 2018-04-26T21:58:37.960 回答