VMware Cloud Community
aravinds3107
Virtuoso
Virtuoso

Assign IP to NIC using Power CLI

I have a task to add a additional NIC to few VM's , i am running the below script to add the NIC card, i am wondering if we can also automate to assign the IP address to VM once the NIC;s are added.

Guest OS are mixture of W2K3 and W2K8 and they are all Powered ON

$vms = Import-CSV "C:\Scripts\VM.csv"
foreach ($vm in $vms){
Get-VM $vm.name | New-NetworkAdapter -Type Vmxnet3 -NetworkName "Internal" -StartConnected
}
If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
0 Kudos
4 Replies
RvdNieuwendijk
Leadership
Leadership

You can use the PowerCLI Set-VMGuestNetworkInterface cmdlet to assign an IP address to a NIC.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
herseyc
Enthusiast
Enthusiast

I do this when I provision lab environments for a class I teach.  I use the Invoke-VMScript and I loop through a csv file with the necessary info needed to configure each lab machine.

----

# Setting up the credentials for the host/VM

$HC = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials for ESX Host", "root", "")
$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "Administrator", "")

# Setting the IP address for the Local Area Connection for a VM

$netsh = "c:\windows\system32\netsh.exe interface ip set address ""Local Area Connection"" static $IP $SNM $GW 1"
Write-Host "Setting IP address for $VM..."
Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential $GC -ScriptType bat -ScriptText $netsh
Write-Host "Setting IP address completed."

----

Replace "Local Area Connection" with the name of the interface you are setting up.

$IP is the IP Address

$SNM is the Subnet Mask

$GW is the Default Gateway for the Interface

Hope this helps.

Hersey

vExpert 2013/2012 :: VCAP5-DCD/VCAP5-DCA/VCAP4-DCA/VCP5,4/EMCISA :: http://www.vhersey.com/ :: Follow me: http://twitter.com/herseyc
0 Kudos
herseyc
Enthusiast
Enthusiast

Good stuff Robert!

Didn't know about that cmdlet and it is a bit cleaner than the Invoke-VMScript.

Thanks for the tip.

Hersey

vExpert 2013/2012 :: VCAP5-DCD/VCAP5-DCA/VCAP4-DCA/VCP5,4/EMCISA :: http://www.vhersey.com/ :: Follow me: http://twitter.com/herseyc
0 Kudos
LucD
Leadership
Leadership

Be aware that Set-VMGuestNetworkInterface is using the invoke-VMScript cmdlet under the covers.

That means there is only support for alimited set of OS.

You can see the scripts in the Infrastructure\vSphere PowerCLI\Scripts folder.

In fact you can write your own script for an OS that is not in the list of supported OS.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos