0

Doing a setup script I want the user to type in a new static IP address for this specific NIC (there is already a static IP on this NIC, so the script needs to change it). But I only get this error message when I give a different IP than the currently defied IP

Set-NetIPAddress : No matching MSFT_NetIPAddress objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetIPAd
dress class on the  CIM server: SELECT * FROM MSFT_NetIPAddress  WHERE ((IPAddress LIKE 'XX.XX.XX.XX')) AND ((InterfaceIndex = 7
)). Verify query parameters and retry.
At line:9 char:1
+ Set-NetIPAddress -InterfaceIndex 7 -IPAddress $SDDIPAddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MSFT_NetIPAddress:String) [Set-NetIPAddress], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound,Set-NetIPAddress

If I try to change to the address that is already on the card, the script goes through. I can change the mask to whatever I want without problems with the script as it is.

I have isolated the few lines of script I need to activate this issue

[String]$SDDIPAddress = "SDD IPaddress"
[String]$SDDMask = "Bits in SDD netmask"

$SDDIPAddress = Read-Host -Prompt 'IP'
$SDDMask = Read-Host -Prompt 'Bits in mask'

Set-NetIPAddress -InterfaceIndex 7 -IPAddress $SDDIPAddress
Set-NetIPAddress -InterfaceIndex 7 -PrefixLength $SDDMask

Does anyone see the obvious I don't??

Im expecting a script that will change the IP address to the address given by the user

2 Answers2

1

This threw me off when I first tried it too. You want to use the New-NetIPAddress cmdlet to set the IP, otherwise it tries to set settings on an existing IP address. For example:

New-NetIPAddress -InterfaceIndex $Index -IPAddress $IP -PrefixLength $Prefix -DefaultGateway $Gateway

I keep this function around that will let you tab complete the adapter name when setting an IP:

function Set-IPAddress {
    [CmdletBinding()]
    Param(
        [string]$IP,
        [string]$SubnetMask,
        [string]$Gateway
    )
 
    DynamicParam {
            # Set the dynamic parameters' name
            $ParameterName = 'Adapter'
            
            # Create the dictionary 
            $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

            # Create the collection of attributes
            $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            
            # Create and set the parameters' attributes
            $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParameterAttribute.Mandatory = $true
            $ParameterAttribute.Position = 1

            # Add the attributes to the attributes collection
            $AttributeCollection.Add($ParameterAttribute)

            # Generate and set the ValidateSet 
            $arrSet = Get-NetAdapter | Select-Object -ExpandProperty Name
            $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)

            # Add the ValidateSet to the attributes collection
            $AttributeCollection.Add($ValidateSetAttribute)

            # Create and return the dynamic parameter
            $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
            $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
            return $RuntimeParameterDictionary
    }

    begin {
        # Bind the parameter to a friendly variable
        $Adapter = $PsBoundParameters[$ParameterName]
        $intMask = [system.net.ipaddress]::parse($SubnetMask).GetAddressBytes()|% -b {$i=0;$mask=0} -p {$mask+=$_*[math]::max(1,(256 -shl ((2-$i)*8)));$i++} -e {$mask}
        $Prefix = [convert]::ToString($intMask,2).Trim('0').Length
    }

    process {
        $Index = Get-NetAdapter -Name $Adapter |% ifIndex
        New-NetIPAddress -IPAddress $IP -InterfaceIndex $Index -PrefixLength $Prefix -DefaultGateway $Gateway
    }

}

I'll be honest, I don't remember if I wrote it or stole it, but it works great for me. The dynamic parameter that lets you tab complete the adapter is pretty handy.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Thx for getting back to me.. I now see that I was unclear in my question. When I write that I want the script to set a new IP address, I should have wrote change from the static IP address the adapter has, to a new one that is the one the user enters. When I try to change the IP with the NeNetIPAddress Cmdlet, as you probably also know, I only get an error message that there is an address on the NIC already Thanks for sharing the script anyhow, sure looks nice.. – Vegard Hals Jul 26 '23 at 06:32
0

I found an answer that solved my challenge. This guy learned me that the old IP must be deleted before changing to a new one. https://4sysops.com/archives/set-an-ip-address-and-configure-dhcp-with-powershell/

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 01 '23 at 11:12