0

I have a powershell function that takes a parameter like this:

    function whatever {
    param (
    [parameter(Mandatory=$true)]
    [Net.IPAddress]
    $ip1,

    [parameter(Mandatory=$true)]
    [Net.IPAddress]
    $ip2
    )
    }

I am getting an error calling

    whatever("1.1.1.1","2.2.2.2")

The error I get is

Cannot process argument transformation on parameter 'ip1'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Net.IPAdd ress".

I have also tried setting a var to be something like

    $ipaddr=[Net.IPAddress]("1.1.1.1")

but it yields the same error.

Any help would be great

0

2 Answers 2

4

You are passing an array of string to your function arguments are passed using spaces in powershell whatever "1.1.1.1" "2.2.2.2"

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

Comments

4

Rerun is correct. This is a common tripping point when learning PowerShell because calling functions in most other languages requires parenthesis.

In addition to rerun's answer, you can also call your function with named parameters like this:

whatever  -ip2 2.2.2.2 -ip1 1.1.1.1

If you noticed I swapped the order of the two parameters. This is the advantage of named parameters, the order does not matter. rerun's example uses positional parameters, in which the order does matter.

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.