4

I have my custom function f which runs some stuff then calls a predefined function Invoke-WebRequest

I want to make f accepts all the arguments that Invoke-WebRequest does, and pass those arguments to Invoke-WebRequest later.

 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

Is there a fast way to achieve this? (Instead of declaring all the possible parameters in f)

mklement0
  • 382,024
  • 64
  • 607
  • 775
zeralight
  • 620
  • 1
  • 5
  • 19

2 Answers2

3

Although it's not the same as not declaring the parameters, you can generate the declarations by generating a proxy command:

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

The result is going to look like this:

[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

#>
briantist
  • 45,546
  • 6
  • 82
  • 127
3

If you don't need validation of arguments up front, the simplest way is to pass all arguments - reflected in automatic variable $Args - through via splatting, i.e., as @Args:

function f { Invoke-WebRequest @Args }

Note the limitations of this approach:

If you do need to make your function advanced, your best bet is to create a proxy function, as described in briantist's helpful answer, which passes values bound to declared parameters through via the automatic $PSBoundParameters variable.

mklement0
  • 382,024
  • 64
  • 607
  • 775