直接展开参数(w/o 将它们保存在临时变量中)

Splatting Arguments directly (w/o saving them in a temp variable)

我得到了一个哈希表,其中包含各种 (S)FTP-连接的连接数据。感谢模块 "WinSCP" 我可以轻松地为 (S)FTP-Transfers

创建会话

目前我将 Hastable-result 保存在一个临时变量中 $arguments 并在之后使用此变量进行拼接

即使只有一行代码可以省去:有没有办法避免变量$arguments


示例哈希表:

$preferences = @{
    "FTPUser" = @{
        HostName   = "ftp.domain.com"
        PortNumber = 21
        Protocol   = "FTP"
    }
    "SFTPUser"       = @{
        HostName                             = "sftp.otherdomain.com"
        GiveUpSecurityAndAcceptAnySshHostKey = $true
    }
}

函数 Get-FtpSession 当前温度变量:

function Get-FtpSession ($user) {
    $arguments = $preferences["$user"]
    $session = New-WinSCPSession @arguments
    return $session
}

我认为我可以使用这样的东西(不起作用):

$session = New-WinSCPSession @($preferences["$user"])

P.S:我知道这个问题有点无意义,但我仍然想知道是否可以解决

如果您愿意修改该 cmdlet,可以通过将 ValueFromPipelineByPropertyName 开关添加到它的参数来实现类似的效果。因此,您可以从管道输入参数。

function foo { [CmdletBinding()]param(
    [parameter(ValueFromPipelineByPropertyName=$true)][String]$arg1,
    [parameter(ValueFromPipelineByPropertyName=$true)][String]$arg2
    )

    Write-Host ($arg1 + " " + $arg2 + "!")
}

[pscustomobject]@{arg1="hello"; arg2="there"} | foo