3

我有我的自定义函数f,它运行一些东西然后调用一个预定义的函数Invoke-WebRequest

我想让f接受Invoke-WebRequest所做的所有参数,然后将这些参数传递给Invoke-WebRequest

 f --UseBasicParsing -Uri https://google.com -UseBasicParsing  -Body @{'name'='user'} -ErrorOnAction Stop
 # Some processing is made
 # then, the following is executed
 Invoke-WebRequest -Uri https://google.com -UseBasicParsing -Body @{'name'='user'} -ErrorOnAction Stop

有没有一种快速的方法来实现这一目标?(而不是在f中声明所有可能的参数)

4

2 回答 2

3

如果您不需要预先验证参数,最简单的方法是$Args通过splatting传递所有参数(反映在自动变量中) ,即@Args

function f { Invoke-WebRequest @Args }

请注意此方法的局限性:

如果您确实需要使您的函数更高级,那么最好的办法是创建一个代理函数,如briantist 的有用答案中所述,该函数通过自动变量传递绑定到声明参数的值。$PSBoundParameters

于 2018-10-21T03:36:47.790 回答
3

虽然和不声明参数不一样,但是可以通过生成代理命令来生成声明:

[System.Management.Automation.ProxyCommand]::Create((Get-Command Invoke-WebRequest))

结果将如下所示:

[CmdletBinding(HelpUri='https://go.microsoft.com/fwlink/?LinkID=217035')]
param(
    [switch]
    ${UseBasicParsing},

    [Parameter(Mandatory=$true, Position=0)]
    [ValidateNotNullOrEmpty()]
    [uri]
    ${Uri},

    [Microsoft.PowerShell.Commands.WebRequestSession]
    ${WebSession},

    [Alias('SV')]
    [string]
    ${SessionVariable},

    [pscredential]
    [System.Management.Automation.CredentialAttribute()]
    ${Credential},

    [switch]
    ${UseDefaultCredentials},

    [ValidateNotNullOrEmpty()]
    [string]
    ${CertificateThumbprint},

    [ValidateNotNull()]
    [X509Certificate]
    ${Certificate},

    [string]
    ${UserAgent},

    [switch]
    ${DisableKeepAlive},

    [ValidateRange(0, 2147483647)]
    [int]
    ${TimeoutSec},

    [System.Collections.IDictionary]
    ${Headers},

    [ValidateRange(0, 2147483647)]
    [int]
    ${MaximumRedirection},

    [Microsoft.PowerShell.Commands.WebRequestMethod]
    ${Method},

    [uri]
    ${Proxy},

    [pscredential]
    [System.Management.Automation.CredentialAttribute()]
    ${ProxyCredential},

    [switch]
    ${ProxyUseDefaultCredentials},

    [Parameter(ValueFromPipeline=$true)]
    [System.Object]
    ${Body},

    [string]
    ${ContentType},

    [ValidateSet('chunked','compress','deflate','gzip','identity')]
    [string]
    ${TransferEncoding},

    [string]
    ${InFile},

    [string]
    ${OutFile},

    [switch]
    ${PassThru})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Invoke-WebRequest', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Microsoft.PowerShell.Utility\Invoke-WebRequest
.ForwardHelpCategory Cmdlet

#>
于 2018-10-20T15:09:15.580 回答