Wednesday, June 13, 2018

RVTools Export

Awhile back at work we had an outage on our main vCenter server (prior to the HA setup on 6.5) and we had to track down which host the VM running on and it took us a bit of time to track that down from various different tools that reported something different. Not to mention that our business has multiple vCenter servers, so it proved a bit challenging.

That is where I started to use RVtools to export all the information. With the beginnings of getting to work, opening RVtools, and doing an export. Soon this became very boring and tedious. So I looked at scripting it. The first version ran once for each vCenter server and did an export to a separate file which was not sustainable. The final version would create four separate files, merge them, and then send via email. It proved helpful (at least to me) and expanded to the health and host report from that.

One thing to mention was that I am always looking to better the process, and have a little intervention as possible. So I didn't want multiple folders or files from months ago laying around. Enter the batch script that runs the process and cleans up any file or folder that is older than two weeks old. This part is at the end of the script.

So to recap, the final script exports all the VM information from the vCenter servers and merges them into one csv file, as well as exports host and health information. The only information that gets emailed out is the VM information, but the other gets stored for reference purposes. Then cleans up any older information that is not needed.

The script is referenced below, it is just a simple batch script that runs locally on a machine with the RV tools executable file and can be easily moved around to different machines. At some point I will upload the everything (including the scheduled task) that will do it all. Below is the script for the export, if there are any questions feel free to reach out to me.

@echo on
rem #########################
rem Name RVToolsBatch
rem By swinnie
rem Date November 2017
rem Version 3.9.5.0
rem #########################

rem ===================
rem Set ENV Variables 
rem ===================

for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
     set dow=%%i
     set month=%%j
     set day=%%k
     set year=%%l
)
set datestr=%day%_%month%_%year%
echo datestr is %datestr%

rem ===================
rem Set Directory 
rem ===================

cd /d C:\Tools\RVTools
mkdir .\exports\%datestr%
mkdir .\exports\%datestr%\hosts
mkdir .\exports\%datestr%\health

rem ===================
rem Start RVTools batch 
rem ===================

rem VM Export
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter01.company.com -c exportvinfo2csv -d .\exports\%datestr% -f vcenter01.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter01.company.com -c exportvinfo2csv -d .\exports\%datestr% -f vcenter02.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter03.inucn.com -c exportvinfo2csv -d .\exports\%datestr% -f vcenter03.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter04.in.lab -c exportvinfo2csv -d .\exports\%datestr% -f vcenter04.csv

rem Host Export
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter01.company.com -c exportvhost2csv -d .\exports\%datestr%\hosts -f vcenter01_hosts.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter02.company.com -c exportvhost2csv -d .\exports\%datestr%\hosts -f vcenter02_hosts.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter03.company.com -c exportvhost2csv -d .\exports\%datestr%\hosts -f vcenter03_hosts.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter04.company.com -c exportvhost2csv -d .\exports\%datestr%\hosts -f vcenter04_hosts.csv

rem Health Export
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter01.company.com -c exportvhealth2csv -d .\exports\%datestr%\health -f vcenter01_health.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter02.company.com -c exportvhealth2csv -d .\exports\%datestr%\health -f vcenter02_health.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter03.company.com -c exportvhealth2csv -d .\exports\%datestr%\health -f vcenter03_health.csv
"rvtools.exe" -u administrator@vsphere.local -p "_RVToolsPWD825HN/hashed=" -s vcenter04.company.com -c exporthealth2csv -d .\exports\%datestr%\health -f vcenter04_health.csv

rem ===================
rem Merging Files 
rem ===================
for /f "tokens=*" %%i in ('dir /b /o:d /A:-D ".\exports\%datestr%" ') do type ".\exports\%datestr%\%%i">> .\exports\%datestr%\vmExport.csv & echo.>> .\exports\%datestr%\vmExport.csv
for /f "tokens=*" %%i in ('dir /b /o:d /A:-D ".\exports\%datestr%\hosts" ') do type ".\exports\%datestr%\hosts\%%i">> .\exports\%datestr%\hosts\hostExport.csv & echo.>> .\exports\%datestr%\hosts\hostExport.csv
for /f "tokens=*" %%i in ('dir /b /o:d /A:-D ".\exports\%datestr%\health" ') do type ".\exports\%datestr%\health\%%i">> .\exports\%datestr%\health\healthExport.csv & echo.>> .\exports\%datestr%\hosts\healthExport.csv

rem =========
rem Send mail
rem =========
set SMTPserver="mail.company.com"
set SMTPport="25"
set Mailto="myemail@compaany.com"
set Mailfrom="fromemail@company.com"
set Mailsubject="vCenter Servers Export"
set AttachmentFile=".\exports\%datestr%\vmExport.csv"

rvtoolssendmail.exe /SMTPserver %SMTPserver% /SMTPport %SMTPport% /Mailto %Mailto% /Mailfrom %Mailfrom% /Mailsubject %Mailsubject% /Attachment %AttachmentFile%

rem ===================
rem Cleaning Up 
rem ===================
forfiles -p ".\exports" -d -14 -c "cmd /c IF @isdir == TRUE rd /S /Q @path"