ESXi filesystem disk usage script

So what if you would like to get a report on filesystem disk usage on esxi box. For now afaik there is no cmdlet in powercli that could do that for us. So in order to get this data you can use plink to invoke command remotely via ssh

Frist, download plink.exe from putty website.

In this example i put plink.exe on c:\ drive

$str1=’c:\plink.exe -v -pw ”v53HYfsd#$%$%^^&*” -l root ‘
$str2=’ df -h’
$outfile=’c:\fsusage.csv’
“Host Size Used Avail Use Mounted” >> $outfile
foreach($esxentry in (Get-VMHost|?{$_.Powerstate -eq “PoweredOn”})){
$esxhost=”‘”+$esxentry.name+”‘”
$command=$str1+$esxhost+$str2
$esxentry.name >> $outfile
$result=Invoke-Expression -Command $command
foreach($resultline in 1..$result.length){
$result[$resultline] >> $outfile
}
}

So, assuming you will have plink on c:\ and you will be in powercli session while connected to VC server, you can just copy/paste those lines and you will receive csv ready file in $outfile location. After this go to excel and do Data->import from text-> Select “space” as delimeter, and you are don. After that you can also select the USE column, and do conditional formatting for example.

If you want to make report for specific hosts that are in some DC folder, use the -Location switch in this line

foreach($esxentry in (Get-VMHost -Location “specific DC”|?{$_.Powerstate -eq “PoweredOn”})){

Script will only print information for hosts that are powered and and that are not in maintenance mode. You can use this solution also in different ways. Change the $str2 variable to whatever command you feel like and you can format the output to adjust it to new command. in $str1 you can give password with $ sign for example as well. In the last foreach loop i am not taking the first line [0] because it is the header which i have included in the beginning so there and the csv file is more readable because of this.  This is great base for everybody, modify it as you want and have fun 😉

PS.  If you have established the connection to esxi hosts before you can give to plink option -batch, if not don’t use it, and you will be ask the question about ssh key fingerprint

I will also write other version of this script, that covers a case where you don’t use plain password in -pw,  but ssh keys. If you know better way of getting the filesystem disk usage leave a comment!

Advertisements