3

I'm trying to create a PowerShell function which wraps another function. I want my function to take a variable number of arguments and to pass those onto the wrapped function. Is there anyway to do this?

Specifically what I am trying to do is something like this:

function MakeRequest() {
    if ($session) {
        $resp = Invoke-WebRequest -WebSession $session $args
    } else {
        $resp = Invoke-WebRequest -SessionVariable session $args
    }
    $resp
}

1 Answer 1

6

You can use splatting for this:

function MakeRequest() {
    if ($session) {
        $resp = Invoke-WebRequest -WebSession $session @args
    } else {
        $resp = Invoke-WebRequest -SessionVariable session @args
    }
    $resp
}

Type this for more help:

help about_Splatting
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.