PowerShell在Invoke-Command上发送Argumentlist

Dar*_*te1 5 powershell argument-passing invoke-command

如何使用哈希表中收集的参数与ArgumentListon 一起使用Invoke-Command

$CopyParams = @{
    Source      = 'E:\DEPARTMENTS\CBR\SHARE\Target'
    Destination = 'E:\DEPARTMENTS\CBR\SHARE\Target 2'
    Structure   = 'yyyy-MM-dd'
}
Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList @CopyParams
Run Code Online (Sandbox Code Playgroud)

无论我尝试什么,它总是抱怨'来源':

Cannot validate argument on parameter 'Source'. The "Test-Path $_" validation script for the argument with
 value "System.Collections.Hashtable" did not return true. Determine why the validation script failed
Run Code Online (Sandbox Code Playgroud)

这篇博客谈到了类似的问题,但我无法让它发挥作用.

对于一个简单的Copy-Item内部Invoke-Command例子来说也是如此:

Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock {Copy-Item} -ArgumentList @CopyParams

Invoke-Command : Missing an argument for parameter 'ArgumentList'. Specify a parameter of type 'System.Obj
ect[]' and try again.
At line:11 char:89
+ ... ck {Copy-Item} -ArgumentList @CopyParams
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助.

Ben*_*Ben 5

One-liner,用于转换远程脚本以接受来自散列的命名参数.

给定一个你想要调用的脚本块:

$Options = @{
    Parameter1 = "foo"
    Parameter2 = "bar"
}

Invoke-Command -ComputerName REMOTESERVER -ArgumentList $Options -ScriptBlock {
    param(
        $Parameter1,
        $Parameter2
    )
    #Script goes here, this is just a sample
    "ComputerName: $ENV:COMPUTERNAME"
    "Parameter1: $Parameter1"
    "Parameter2: $Parameter2"
} 
Run Code Online (Sandbox Code Playgroud)

你可以像这样转换它

Invoke-Command -Computername REMOTESERVER -ArgumentList $Options -ScriptBlock {param($Options)&{
    param(
        $Parameter1,
        $Parameter2
    )
    #Script goes here, this is just a sample
    "ComputerName: $ENV:COMPUTERNAME"
    "Parameter1: $Parameter1"
    "Parameter2: $Parameter2"
} @Options}
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?基本上我们已经包装了原始脚本块,如下所示:

{param($Options)& <# Original script block (including {} braces)#> @options }
Run Code Online (Sandbox Code Playgroud)

这使得原始脚本块成为匿名函数,并创建具有参数的外部脚本块,该参数$Options除了调用内部脚本块之外什么都不做,传递@options给splat散列.


Dar*_*te1 1

我找到了一个解决方法,但是您必须确保Advanced function位于模块文件中的文件已在本地会话中预先加载。所以它可以在远程会话中使用。我为此编写了一个小辅助函数。

Function Add-FunctionHC {
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param(
        [String]$Name
    )
    Process {
        Try {
            $Module = (Get-Command $Name -EA Stop).ModuleName
        }
        Catch {
            Write-Error "Add-FunctionHC: Function '$Name' doesn't exist in any module"
            $Global:Error.RemoveAt('1')
            Break
        }
        if (-not (Get-Module -Name $Module)) {
            Import-Module -Name $Module
        }
    }
}

# Load funtion for remoting
Add-FunctionHC -Name 'Copy-FilesHC'

$CopyParams = @{
    Source      = 'E:\DEPARTMENTS\CBR\SHARE\Target\De file.txt'
    Destination = 'E:\DEPARTMENTS\CBR\SHARE\Target 2'
}

$RemoteFunctions = "function Copy-FilesHC {${function:Copy-FilesHC}}" #';' seperated to add more

Invoke-Command -ArgumentList $RemoteFunctions -ComputerName 'SERVER' -Credential $Cred -ScriptBlock {
    Param (
        $RemoteFunctions
    )
    . ([ScriptBlock]::Create($RemoteFunctions))
    $CopyParams = $using:CopyParams
    Copy-FilesHC @CopyParams
}
Run Code Online (Sandbox Code Playgroud)

最大的优点是您不需要在脚本中复制完整的函数,它可以保留在模块中。因此,当您将模块中的某些内容更改为函数时,它也将在远程会话中可用,而无需更新脚本。