VMware Cloud Community
jschandler
Contributor
Contributor

PowerCli, vCloud Director Configure NIC, Import

Having problems getting my PowerCli script to fully import a VM as well as configuring the Organization VDC network within the VM guest.

Hoping someone could take a quick look at what I have below (Not working) and advise what code I'd need to add to fully configure the Network on this after a successful import into vDC

#Add correct Snapins for vCenter Server, and vCloud Director

Add-PSSnapin VMware.VimAutomation.Core

Add-PSSnapin vmware.vimautomation.cloud

clear

#Predefined Variables

$ciServerNED = 'ciserver1'

$ciServerMAR = 'ciserver2'

$viServerNED = 'viserver1'

$viServerMAR = 'viserver2'

$ciName = $viName

$vApp = $ciName

#Prompts

$viName = Read-Host "VM Guest?"

$org = Read-Host "VM Organization?"

$viServer = Read-Host "Virtual Center Name?"

$location = Read-Host "NED, or MAR?"

$ciNetwork = Read-Host "VDC_Network_Name?"

$ciNetwork2 = Read-Host "VDC_Network_Name2?"

$ciNetworkIP = Read-Host "Network 1 IP?"

$ciNetworkIP2 = Read-Host "Network 2 IP (Press Enter for NULL)?"

if ($location -eq 'NED')

{

$ciServer = $ciServerNED

$viServer = $viServerNED

}

else

{

$ciServer = $ciServerMAR

$viServer = $viServerMAR

}

#Connecting to Virtual Center, and vCloud Director

Connect-VIServer -Server $viServer

Connect-CIServer -Server $ciServer

#Import Process

Import-CIVApp -VM (Get-VM $viName) -OrgVdc $org -NoCopy:$True -RunAsync #-Confirm $false

Start-Sleep -s 30

$ciName | Get-CINetworkAdapter | Set-CINetworkAdapter -vappnetwork $ciNetwork -IPaddressAllocationMode:Pool -Connected:$True

Set-NetworkAdapter -NetworkAdapter $ciNetwork -WakeOnLan:$true -StartConnected:$true -Connected:$true

Start-Sleep -s 30

#Starting VM Guest through vCloud Director

Start-CIVM -VM $viName

#Disconnecting from vCenter, and vCloud Director

Disconnect-VIServer -Server $viServer -Confirm:$false

Disconnect-CIServer -Server $ciServer -Confirm:$false

#Pop-up indicating the script has finished

$a = new-object -comobject wscript.shell

$b = $a.popup("Script has completed",0,"Script has completed",1)

Reply
0 Kudos
3 Replies
JayhawkEric
Expert
Expert

I don't see where you are creating your vApp Network.  (New-CIVAppNetwork).  You'll need to do this before your line that is trying to connect to it.  This can be different depending on if it's a vApp Internal Network or connection to a Org VDC Network directly.  See New-CIVAppNetwork - vCloud Snapin Cmdlets Reference

I use the following in my VCD Import script to update the NIC on the VM's to connect to the vApp defined network.  I think the only thing you'll need to change is the $newVappName variable to that of your new vApp variable.

Get-CIVApp -Name $newVappName | Get-CIVM | Get-CINetworkAdapter | Set-CINetworkAdapter -IPAddressAllocationMode Pool -VAppNetwork (Get-CIVApp -Name $newVappName | Get-CIVAppNetwork) -Connected $true

-Eric

VCP5-DV twitter - @ericblee6 blog - http://vEric.me
Reply
0 Kudos
jschandler
Contributor
Contributor

Thanks Eric, although that does not enter in an IP Address.  That's what I'm truly struggling with.  It seems this maybe best done via GUI, instead of PowerCli.

Reply
0 Kudos
JayhawkEric
Expert
Expert

You'll want to enable guest customization in order to set the IP within the VM you are importing.  If it's a Windows VM I wouldn't allow it to change the SID though.

Give this a shot but change the code in the second to last line to command line changes for NIC within OS depending on OS version (example for Windows = netsh interface ip set address name="Local Area Connection " static $IP 255.255.255.0 192.168.1.1).  You may have to write this out to get the network interface name.

$IP = $viName.ExtensionData.GetNetworkConnectionSection().NetworkConnection[0].IpAddress

$CiVmGc = $viName.ExtensionData.Section | Where {$_.GetType() -like "*GuestCustomizationSection"}

$CiVmGc.Enabled = $true

$CiVmGc.ChangeSid = $false

$CiVmGc.CustomizationScript = [string]::join([environment]::NewLine, "Command line code to set IP address depending on OS via $IP variable")

$CiVmGc.UpdateServerData()

-Eric

VCP5-DV twitter - @ericblee6 blog - http://vEric.me
Reply
0 Kudos