IPAddress

This topic contains 5 replies, has 2 voices, and was last updated by  Nicholas J Crooks 1 week ago.

  • Author
    Posts
  • #102502

    Nicholas J Crooks
    Participant

    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!

    Thanks!

    function set-ip {
    
        [CmdletBinding()]
        PARAM(
        [parameter(mandatory=$true,
        Position=0,
        HelpMessage="Enter the ip address you want, like 192.168.1.11")]
        [String]$ip,
        
        [parameter(mandatory=$true,
        HelpMessage="Enter the ip of the gateway, like 192.168.1.1 or 10.10.10.254")]
        [String]$gateway
        
        )
         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
    
         }
    
    }
    
    
  • #102505

    Don Jones
    Keymaster

    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.

  • #102508

    Nicholas J Crooks
    Participant

    Ok!

    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.

    set-ip
    cmdlet set-ip at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    ip: 192.168.1.11
    gateway: 192.168.1.1
    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
    
    
    
  • #102511

    Don Jones
    Keymaster

    Ah.

    $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.

  • #102512

    Nicholas J Crooks
    Participant

    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.

  • #102569

    Nicholas J Crooks
    Participant

    Thanks again Don!

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

You must be logged in to reply to this topic.