PowerCLI

 View Only
Expand all | Collapse all

Unable to change IP Address on VMs

  • 1.  Unable to change IP Address on VMs

    Posted Jan 11, 2023 09:38 AM

    Hi,

    I am unable to change the IP address. Subnet, Gateway and DNS on multiple VMs using below as I am getting error

    $WPassword = "Password@123"
    $pass = ConvertTo-SecureString -AsPlainText $WPassword -Force
    $GC = New-Object System.Management.Automation.PSCredential (".\Admin", $pass)

    # Defines Variables
    $VMName = "App02"
    $IP = "192.168.2.52"
    $SNM = "255.255.254.0"
    $GW = "192.168.2.1"
    $DNS1 = "192.168.2.5"
    $DNS2 = "192.168.2.6"

    $code = @'
    $wmi={Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"}
    New-NetIPAddress -InterfaceIndex $wmi.Index - IPAddress $IP -PrefixLength $SNM -DefaultGateway $GW
    '@

    # Starts the setup of the IP, Subnetmask, Gateway and DNS
    $vm = Get-VM -Name $vmName

    $sInvoke = @{
    VM = $vm
    GuestCredential = $GC
    ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)
    ScriptType = 'powershell'
    }
    Write-Host "Setting IP address for $VMname" -ForegroundColor Yellow
    Invoke-VMScript @sInvoke
    Write-Host "Setting IP address completed." -ForegroundColor Green
    Get-VM $VMname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName pg3568 -Confirm:$false

     

    Output error

    ScriptOutput
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    | = : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
    | spelling of the name, or if a path was included, verify that the path is correct and try again.
    | At line:1 char:4
    | + & {={Get-WmiObject win32_networkadapterconfiguration -filter "ipenabl ...
    | + ~
    | + CategoryInfo : ObjectNotFound: (=:String) [], CommandNotFoundException
    | + FullyQualifiedErrorId : CommandNotFoundException
    |
    | New-NetIPAddress : Cannot process argument transformation on parameter 'InterfaceIndex'. Cannot convert value ".Index"
    | to type "System.UInt32". Error: "Input string was not in a correct format."
    | At line:2 char:34
    | + New-NetIPAddress -InterfaceIndex .Index -IPAddress 192.168.2.52 -Pref ...
    | + ~~~~~~
    | + CategoryInfo : InvalidData: (:) [New-NetIPAddress], ParameterBindingArgumentTransformationException
    | + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-NetIPAddress
    |
    |
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

     



  • 2.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 09:43 AM

    In the $code you have to "escape" the $wmi variable with a back-tick, otherwise it will be substituted by the  ExpandString method.

     

    $code = @'
    `$wmi={Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"}
    New-NetIPAddress -InterfaceIndex `$wmi.Index - IPAddress $IP -PrefixLength $SNM -DefaultGateway $GW
    '@@

     

     



  • 3.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 10:07 AM

    LucD,

    now getting the below error

    ScriptOutput
    ---------------------------------------------------------------------------------------------------------------------------------------
    | New-NetIPAddress : Cannot process argument transformation on parameter 'InterfaceIndex'. Cannot convert value ".Index"
    | to type "System.UInt32". Error: "Input string was not in a correct format."
    | At line:2 char:34
    | + New-NetIPAddress -InterfaceIndex .Index -IPAddress 192.168.2.52 -Pref ...
    | + ~~~~~~
    | + CategoryInfo : InvalidData: (:) [New-NetIPAddress], ParameterBindingArgumentTransformationException
    | + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-NetIPAddress
    |
    |

     



  • 4.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 10:20 AM

    You have to use the back tick on all variables that you don't want to be substituted.
    Also the 2nd occurrence of the $wmi variable (I updated the code above)



  • 5.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 11:50 AM

    LucD,

    I tried, still getting errors, so I have modified as below and it is working now. Currently all the variables for IP, Subnet, Gateway and DNS are inside the code. now I would like to know, how can I define those variables from outside of the code ?

    $code = @'
    # Defines Variables
    $VMName = "App02"
    $IP = "192.168.2.52"
    $SNM = "255.255.254.0"
    $GW = "192.168.2.1"
    $DNS1 = "192.168.2.5"
    $DNS2 = "192.168.2.6"
    # Retrieve the network adapter that you want to configure
    $adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
    # Remove any existing IP, gateway from our ipv4 adapter
    If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
    $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
    }
    If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
    $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
    }
    # Configure the IP address and default gateway
    $adapter | New-NetIPAddress `
    -AddressFamily $IPType `
    -IPAddress $IP `
    -PrefixLength $SNM `
    -DefaultGateway $GW
    # Configure the DNS client server IP addresses
    $adapter | Set-DnsClientServerAddress -ServerAddresses ($DNS1,$DNS2)
    '@

    $vm = Get-VM -Name $vmName

    $sInvoke = @{
    VM = $vm
    GuestCredential = $GC
    ScriptText = $code
    ScriptType = 'powershell'
    }

    Invoke-VMScript @sInvoke

     



  • 6.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 11:57 AM

    What error do you mean by "I tried, still getting errors"?
    The method to declare those variables outside the code block is the method in your original script.
    You just have to escape the variables in the code block that you don't want to be substituted.



  • 7.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 12:12 PM

    LucD,

    for the first script, After making the suggested changes,  I am getting the below error

    ScriptOutput
    -----------------------------------------------------------------------------------------------------------------------------
    | New-NetIPAddress : Invalid parameter InterfaceIndex 0
    | At line:2 char:1
    | + New-NetIPAddress -InterfaceIndex $wmi.Index -IPAddress 192.168.2.52 - ...
    | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | + CategoryInfo : InvalidArgument: (MSFT_NetIPAddress:ROOT/StandardCimv2/MSFT_NetIPAddress) [New-NetIPAddr
    | ess], CimException
    | + FullyQualifiedErrorId : Windows System Error 87,New-NetIPAddress
    |
    |
    -----------------------------------------------------------------------------------------------------------------------------

     

     



  • 8.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 12:41 PM

    But that is not caused by the variable substitution.
    Why do you place the Get-WmiObject inside curly braces?
    You are in fact assigning a code block to the $wmi variable

     

     

     

    $code = @'
    `$wmi=Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
    New-NetIPAddress -InterfaceIndex `$wmi.Index -IPAddress $IP -PrefixLength $SNM -DefaultGateway $GW
    '@
    

     

     

     

     



  • 9.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 12:59 PM

    LucD,

    After making the change, I am getting another error now

    $code = @'
    `$wmi=Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
    New-NetIPAddress -InterfaceIndex `$wmi.Index -IPAddress $IP -PrefixLength $SNM -DefaultGateway $GW
    '@

    ScriptOutput
    -------------------------------------------------------------------------------------------------------------------------------
    | New-NetIPAddress : Element not found.
    | At line:2 char:1
    | + New-NetIPAddress -InterfaceIndex $wmi.Index -IPAddress 172.26.20.25 - ...
    | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | + CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:ROOT/StandardCimv2/MSFT_NetIPAddress) [New-NetIPAddre
    | ss], CimException
    | + FullyQualifiedErrorId : Windows System Error 1168,New-NetIPAddress
    |
    |
    -------------------------------------------------------------------------------------------------------------------------------

     



  • 10.  RE: Unable to change IP Address on VMs
    Best Answer

    Posted Jan 11, 2023 01:25 PM

    That is because the Index property of the Win32_NetworkAdapterConfiguration class is not the InterfaceIndex.
    With the Win32_NetworkAdapter class, you can find the InterfaceIndex.

    I'm wondering where you get this kind of code.

    $code = @'
    `$netConfig = Get-WmiObject win32_networkadapterconfiguration -Filter "ipenabled = 'true'"
    `$netAdapter = Get-WmiObject win32_networkadapter -Filter "index = `$(`$netConfig.Index)"
    New-NetIPAddress -InterfaceIndex `$netAdapter.InterfaceIndex - IPAddress $IP -PrefixLength $SNM -DefaultGateway $GW
    '@
    

     



  • 11.  RE: Unable to change IP Address on VMs

    Posted Jan 11, 2023 02:16 PM

    Perfect...Thank you very much LucD. That worked now without any errors