Tuesday 10 September 2013

VMware PowerCli: How to generate CSV file with the Name ,IpAddress , Datastore for your entire Vcenter with PowerShell and Windows Task Scheduler


 Today I'm going to show you how to do a litle script with VMWARE PowerCli.
The script Below is written in PowerShell and will show you are to generate a report about Virtual Machines , IPaddress and Datastore space from your VCenter Server.

1)First you must install PowerCli (You must have a VMware account):




2) .You must desable the restriction in PowerCli Shell to execute scripts:
The value for Get-ExecutionPolicy is RemoteSigned. This value must be set to Unrestricted

Set-ExecutionPolicy Unrestricted

3)Create a "ScriptVM" file with ".ps1" extension, and add the following lines:

# ===========================================================================
# NAME: ScriptVM.ps1
# AUTHOR:  Lucky Chris ATTOH-TOURE
# DATE  : 2013.09.13
# COMMENT: Get VMs, DataStore, IP Address and Space
# REQUIRES: VMware PowerCLI
# ===========================================================================
#Prendre toutes les Vms 
$vm = Get-VM * 
#Créer un tableau d'objets 
$objects = @() 
#Pour Chaque VM donner le nom, le DataStore l'espace occupé et l'espace libre
 foreach ($vm.Name in $vm) { 
  $dataS = Get-DataStore -VM $vm.Name
 foreach ($dataS.Name in $dataS) {
      $vm.Name | Select IPAddress $row = New-Object PSObject -Property @{"NomVM"=$vm.Name; "IPAddress"=$vm.Guest.IPAddress[0]; "Datastore"=$dataS.Name; "FreeSpaceGB"=$dataS.FreeSpaceGB; "CapacityGB"=$dataS.CapacityGB} 
      $objects += $row 
  } #Compte le nombre de VMs $count = $count+1 
} #Affichage du nombre de machine virtuelle 
Write-Host "Nous avons au total: " $count " Machines Virtuelles" 
Write-Host "" 
#Affichage du tableau Report VM 
$objects | select-Object NomVM,IPAddress,Datastore,FreeSpaceGB,CapacityGB |Export-Csv "C:\test\ReportVsphere.txt" -NoTypeInformation -UseCulture

4)Now we are going to put this script into windows Task scheduler

Launch Windows Task Scheduler :

In Action tab, give the path of Powershell cli:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe (For me on Windows 7)

In argument field put:
"& 'C:\test\launchReport.ps1'"

 Or if you want th whole syntax:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "& 'C:\test\launchReport.ps1'"

The  launchReport.ps1 file will contain:
.\ScriptVM.ps1

This will generate an CSV file called "ReportVsphere.txt".

Output:

"NomVM";"IPAddress";"Datastore";"FreeSpaceGB";"CapacityGB"
"vCenter";"10.2.25.163";"PROD-VM-1";"309,2875";"1483,75"
"w2k8-Template";;"TEST-VM-1";"177,275";"2047,75"
"VM-PC-01";;"TEST-VM-1";"177,275";"2047,75" 

5)Now you can open this file with Excel and do what you want to finalize your Report!

No comments:

Post a Comment

HOWTO IMPORT OVA (from VMware) to PROXMOX 5.x

  HOWTO IMPORT OVA (from VMware) to PROXMOX 5.x         Hi All may be someone has faced to import an OVA (created with VMware) to PROX...