Set-NetIPAddress on remote server appears to be querying what it should set

Welcome Forums General PowerShell Q&A Set-NetIPAddress on remote server appears to be querying what it should set

Viewing 5 reply threads
  • Author
    Posts
    • #214881
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      I am writing a Powershell script to document and enforce static IPs on a industrial controls network. Part of the heavy lifting involves the Set-NetIPAddress command, formatted as such:

      Set-NetIPAddress -AddressFamily IPv4 `
      -CimSession $ServerName `
      -InterfaceAlias $NICName `
      -IPAddress $IPAddress `
      -PrefixLength 24

      When the script executes, despite being in a try/catch, it barfs an error message into the ISE console instead of my log (which is also annoying but I’ll focus on the immediate problem here). Here is the error:

      Set-NetIPAddress : SERVERNAME: No matching MSFT_NetIPAddress objects found by CIM query for
      instances of the ROOT/StandardCimv2/MSFT_NetIPAddress class on the ERHIST2 CIM server:
      SELECT * FROM MSFT_NetIPAddress WHERE ((IPAddress LIKE ‘130.130.130.5’)) AND
      ((InterfaceAlias LIKE ‘PLC Network’)) AND ((AddressFamily = 2)). Verify query parameters
      and retry.

      My intended result is to set the IP of “PLC Network” on SERVERNAME to 130.130.130.5. Indeed, this command works if I execute it locally. For some reason the error seems to be telling me that it’s querying for a NIC named ‘PLC Network’ with an IP of ‘130.130.130.5’, which I will note that if this were already the case, there would be nothing for me to have this silly script do!

      So firstly, am I doing this incorrectly? If not, how do I go about asking someone to make the Set-NetIPAddress use -IPAddress as an input for setting the IP, rather than part of the query?

    • #215022
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      Take a look at this:

      https://powershell.org/forums/topic/all-i-want-to-do-is-assign-an-ip-address/

      Also, if you want your code to look nice and be functional, use splatting:

      $params = @{
          AddressFamily  = 'IPv4'
          CimSession     = $ServerName
          InterfaceAlias = $NICName
          IPAddress      = $IPAddress
          PrefixLength   = 24
          ErrorAction    = 'Stop'
      }
      
      Set-NetIPAddress @params
      

      There are many blogs on splatting, but the one I linked has other important things like realizing that $params is a hash table, so you can add, remove or change params dynamically. Lastly, your try catch is not working because you have have not set the ErrorAction to Stop to make the command terminating (shown in code above).

      • This reply was modified 1 month, 3 weeks ago by Rob Simmers.
    • #215958
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      I had seen that post, and unfortunately it ends with the OP simply saying “I had options wrong and it’s working”, without saying what options were wrong or what options work. I don’t see the answer to my issue in that post? Prior to posting this question I spent a couple days trying to get New-NetIPAddress to work, and mostly it gives me multiple IP addresses configured on the one adapter, which is exactly not what I want. Attempting to remove IP addresses on a remote adapter when we’re talking to that computer via the NIC we’re modifying also did not work out very well, (specifically trying to remove before creating new leaves one with a NIC having no IP, and then we’re back to manually configuring things on the remote system). Ultimately I determined Set-NetIPAddress is most likely the command I want, which is not what that topic ends up using (if I assume correctly?). Maybe I’m missing something tho.

      Cheers on the splatting tip. I do a lot of work in VBS, VBA and Bash scripting and I’m always breaking lines up with end-of-line characters. It’s nice that Powershell has something to wrap this up a little cleaner. The ErrorAction=Stop should also be useful since that’s exactly what I want it to do. Maybe this much, although not my primary question, might be enough to get me to the answer…

    • #215970
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      Used to do a lot of vbScript\VBA as well. You are referencing a specific NIC, so where are you getting that? It assumes there is a NIC with that name. If you do a Get-NetIPAddress, does that interface exist? If you look at the error, it is doing a WMI query and basically the query is NULL so it cannot set something that is NULL. You may want to try something like:

      $adapter = Get-NetIPAddress -InterfaceAlias 'Local*' -AddressFamily IPv4
      if ($adapter) {
          $adapter | Set-NetIPAddress -IPAddress x.x.x.x      
      }
      else {
          "No adapter found with search criteria"
      }
      
    • #216030
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      The NIC with that name exists on every server that connects to that network; each was manually named when we were making sure the right virtual NIC got plugged into the correct vswitch. I tried that last example, with -CimSession because this is updating IPs around the network:

      $adapter = Get-NetIPAddress -CimSession ‘erfix1’ -InterfaceAlias ‘PLC Network’
      if ($adapter) {
      $adapter | Set-NetIPAddress -IPAddress ‘130.130.130.238’
      }
      else {
      “No adapter found with search criteria”
      }

      Set-NetIPAddress : erfix1: No matching MSFT_NetIPAddress objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetIPAddress class on the erfix1 CIM server: SELECT * FROM
      MSFT_NetIPAddress WHERE ((IPAddress LIKE ‘130.130.130.238’)) AND ((InterfaceIndex = 20)) AND ((InterfaceAlias LIKE ‘PLC Network’)). Verify query parameters and retry.
      At line:3 char:16
      + $adapter | Set-NetIPAddress -IPAddress ‘130.130.130.238’
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:String) [Set-NetIPAddress], CimJobException
      + FullyQualifiedErrorId : CmdletizationQuery_NotFound,Set-NetIPAddress
      + PSComputerName : erfix1

    • #216048
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      I’m not able to spend a lot of time on this at the moment, but wanted to drop a few tips.

      1. If I recall correctly, the net cmdlets came out with version 4 (windows 8/2012) – can you confirm powershell on the remote system is at least version 4?
      2. Try getting the index of the appropriate NIC and use the Index to set. I’ve seen some weird issues due to language/regional settings.
      3. Sometimes when remoting you aren’t getting all the feedback – try doing these commands on an interactive session to the target machine – you may be surprised.
      4. While doing #2, verify the interface name/index is what you expect it to be. Do get-netipaddress and others interactively just as a sanity check to the info you are putting in/expecting.
      5. If your interactive commands work exactly as you want, you could consider turning that into a scriptblock and using PSRemoting to execute it remotely. There are plenty of cmdlets that don’t do remoting natively, and some that do native remote in a less efficient/more error prone manner to PSRemoting.

      Based on the error I’d guess the remote host powershell is -lte 3.

      I sure hope this helps!

Viewing 5 reply threads
  • You must be logged in to reply to this topic.