I am attempting to write a function to quickly swap my ip address but have not had luck.

Please tear apart what I have written and let me know how I can improve!


function set-ip {

    HelpMessage="Enter the ip address you want, like")]
    HelpMessage="Enter the ip of the gateway, like or")]
     process {
     #This works fine from an interactive session on its own, and I want to try to get this automatically
     $target = (Get-NetIPInterface | ? {$_.ifAlias -like '*ethernet*' -and $_.AddressFamily -eq 'IPv4'})

     #Just a heads up
     Write-Verbose "Beginning work on the following adapter"
     start-sleep -Seconds 1
     #Goal was to spit out the variable, and check the result
     write-output "$target"
     Write-Warning "Confirm $target looks correct" -WarningAction Inquire
     #Ideally this should not require anymore input at this point
     New-NetIPAddress -IPAddress $ip -InterfaceAlias $target -DefaultGateway $gateway -PrefixLength 24



It’d help a bit if you could describe what’s not working the way you want, as I’m not able to run your code on my system at this time.


Thanks for looking!

I am copying the output below, I am interested in why write-output gives me the Name = “;A55?55” etc.

The error makes sense given the weird value of $target.

cmdlet set-ip at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
MSFT_NetIPInterface (Name = ";A55?55;", CreationClassName = "", SystemCreationClassName = "", SystemName = "")
WARNING: Confirm MSFT_NetIPInterface (Name = ";A55?55;", CreationClassName = "", SystemCreationClassName = "", SystemName = "") looks correct
New-NetIPAddress : Invalid parameter InterfaceAlias MSFT_NetIPInterface (Name = ";A55?55;", CreationClassName = "", SystemCreationClassName = "", SystemName = "")
At line:31 char:6
+      New-NetIPAddress -IPAddress $ip -InterfaceAlias $target -Default ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (MSFT_NetIPAddress:ROOT/StandardCimv2/MSFT_NetIPAddress) [New-NetIPAddress], CimException
    + FullyQualifiedErrorId : Windows System Error 87,New-NetIPAddress


$target = (Get-NetIPInterface | ? {$_.ifAlias -like '*ethernet*' -and $_.AddressFamily -eq 'IPv4'})

This only seems to “work fine” interactively because you’re perhaps not understanding what’s going in $target. It’s a NetIPInterface object; looking at the output of the command in the console is a little misleading because the console has to format the object into a text display, which can make you think you’ve got something you don’t. However, when you just try to use $target in Write-Output, you’re triggering a slightly different process, where it’s just showing you its Name property. Honestly, you should probably be doing Write-Verbose there, too - Write-Output is going to feed the “output” of your function, which isn’t what you want.

Where you’re using $target:

     New-NetIPAddress -IPAddress $ip -InterfaceAlias $target -DefaultGateway $gateway -PrefixLength 24

-InterfaceAlias doesn’t want an entire NetIPInterface object, but that’s what you’re feeding it. You probably need to use something like ($target.alias) or something. That is, rather than jamming the entire object and all its properties into -InterfaceAlias, you need to feed it the ONE property that contains the information the parameter needs.

That makes a lot of sense.

Thank-you very much for your time.

I will refine this, and once I am able to verify it works well I will put the results in a reply!

I can’t believe in my first post I was lucky enough to have you reply!

Thanks again.

Thanks again Don!

     write-output ($target.interfacealias)
     Write-Warning $($target.interfacealias) -WarningAction Inquire