Change Linux IP Address via PowerCLI

Davoud Teimouri

Professional blogger, vExpert 2015/2016/2017/2018/2019/2020/2021/2022/2023, vExpert NSX, vExpert PRO, vExpert Security, vExpert EUC, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

10 Responses

  1. darwa says:

    Typo (“Credntial”) in code.

  2. kulvinder says:

    Hello, I am trying to change the IP for RHEL VM and getting this error. Can you please advise.

    Invoke-VMScript : 2019-08-11 10:20:30 PM Invoke-VMScript Error occured while executing script on guest OS in VM ‘nicedep01’. Could not locate “Powershell” script interpreter in any of the expected locations. Probably you do not have enough permissions to
    execute command within guest.

  3. Kulvinder says:

    Hi Davoud

    I added this “-scripttype bash” and now I am able to proceed but now getting error.

    PS C:\WINDOWS\system32> Get-VM “$VM_dep01” | Invoke-VMScript -ScriptText ‘sed -ri “s|$vm_dep01_new_ip|$VM_dep01_org_ip|” /etc/sysconfig/network-scripts/ifcfg-ens160 && cat /etc/sysconfig/network-scripts/ifcfg-ens160′ -GuestCredential $credential -scripttype bash

    ScriptOutput
    ———————————————————————————————————————–| bash: -c: line 0: syntax error near unexpected token `;&’
    | bash: -c: line 0: `sed -ri “s|$vm_dep01_new_ip|$VM_dep01_org_ip|” /etc/sysconfig/network-scripts/ifcfg-ens160 && cat /etc/sysconfig/network-scripts/ifcfg-ens160′
    |
    ————————————————————————————————————-

    I removed “;&” and then it give me this error

    PS C:\WINDOWS\system32> Get-VM “$VM_dep01” | Invoke-VMScript -ScriptText ‘sed -ri “s|$vm_dep01_new_ip|$VM_dep01_org_ip|” /etc/sysconfig/network-scripts/ifcfg-ens160 & cat /etc/sysconfig/network-scripts/ifcfg-ens160’ -GuestCredential $credential -scripttype bash

    ScriptOutput
    ———————————————————————————————————————–| sed: -e expression #1, char 0: no previous regular expression
    | bash: amp: command not found

  4. kulvinder says:

    Hi Davoud, Thanks for your quick reply..

    After some quick tricks I am able to use that, but only when I pass the actual value and not as variable
    like sed -ri “s|192.168.0.25|192.168.20.5|

    I just seen the other link which you mentioned, But I am not sure how can I use that.
    Can you give some more details when you have time.

    I am trying to achieve this
    Change the hostname of 6 linux VM
    Change the IP, SM, DG and DNS for those 6 VM’s
    Add entry in /etc/fstab for mounting the NFS

    At present I have to do this multiple time as I have to create and destroy this 6 VM cluster very often
    Thanks a ton for all your Help

  5. Jon Tankersley says:

    I’ve not had a chance to try this, though I will…
    I was wondering about the sed syntax…. generally sed is
    s/this/that/
    substitute this with that.
    Your example has the newIP and oldIP in the wrong place.
    s|$NIPAddress|$OIPAddress|
    which SHOULD fail.

    Also, this assumes 1) eth0 and not another NIC, and doesn’t update /etc/hosts which may have the IP also.
    Lastly, this will generally require a few more steps… a reboot at the least since you are updating the fstab for possible hard coded IP addresses there (again, /etc/hosts would catch that).

Leave a Reply

Your email address will not be published. Required fields are marked *