Virtual Geek

Tales from real IT system administrators world and non-production environment


August 4, 2016 09:26AM

Few days back I wrote CREATE VIRTUAL DATACENTER AND ADD ESXI HOST ON VCENTER SERVER using vSphere web client. That was completely graphical user interface GUI way to perform the task. Now I will be performing same task using VMware vSphere Powercli tool. I have downloaded it from and installed also before running powercli tool need to enable execution policy for running powershell ps1 files (set-executionpolicy unrestricted) or else you may end up with error POWERCLI INITIALIZE-POWERCLIENVIRONMENT.PS1 CANNOT BE LOADED BECAUSE RUNNING SCRIPTS IS DISABLED. It helps to run script and create vsphere environment in Powershell, in the background its importing all vmware modules.

Once all the necessary changes done Find Powercli and run it, and very first task is to connect Vcenter server and create connection the same way you connect through vSphere client or vsphere web client. First command I am running is Connect-VIserver, Green fields need to be changed as per one's environment., Once executed it popups for password if they are authenticated successfully I can see connection is made and connected to vCenter using provided username. (I got certificate warning but i can ignore it)

Connect-VIServer vCenterIP/Fqdn -Credential (Get-Credential Username) find and launch vmware vsphere powercli, connect-viserver credentials, vcenter

Next execute below command to create a new virtual datacenter, I am storing all the info in powershell variable $Datacenter so I can use it later, In case if I want to use existing datacenter I will run command $Datacenter = Get-Datacenter -Name MySecondDatacenter instead of below.

$Datacenter =  New-Datacenter -location (Get-Folder -NoRecursion) -Name MySecondDatacenter

Once new datacenter is created add esxi host to the datacenter using below command, I was prompted for root username and password. 

Add-VMHost -Name '' -Location $Datacenter -Credential (Get-Credential root) -Force -Confirm:$false

new-datacenter, get-folder, add-vmhost esxi powercli vmware

Once the task is successful I can login to vsphere web client and check the recent tasks and inventory object if everything is fine.

new-datacenter, get-folder, add-vmhost esxi powercli vmware vsphere web client tasks

Go Back