VMware Cloud Community
VMSavvy
Enthusiast
Enthusiast
Jump to solution

Datastore report..

Hi,

I'm running a script to get a list of datastores which has less than 5% free space and which are NOT accessible.. I'm not getting the desired output and not sure where I went wrong.. Please help!!

Here is the script that I'm using..

$Report =@()

Get-Datastore | Where {$_.Extensiondata.Summary.MultipleHostAccess} | Sort-Object Name

$Store1 = Get-View $_.ID | Where {("{0:N}" -f (100 * $_.FreeSpaceMB/$_.CapacityMB)) -le '5'}

foreach ($store in $Store1){

   $myObj = "" | Select-Object Name, PercFree, FileSystem

   $myObj.Name = $store.Name

   $myObj.PercFree = "{0:N}" -f (100 * $store1.FreeSpaceMB/$store1.CapacityMB)

   $myObj.FileSystem = $store.Type

   $Report4 += $myObj

}

if ($Store1) {$Report | ConvertTo-Html -title "Datastore less than 5% free space" -body  "<H2>Datastore less than 5% free space</H2>" | Out-File -Append  $filelocation}

else {ConvertTo-Html -title "Datastores less than 5% free space" -body  "<H3>No Datastores less than 5% free space</H3>" | Out-File -Append  $filelocation}

$Report = @()

Get-Datastore | Where {$_.Extensiondata.Summary.MultipleHostAccess} | Sort-Object Name

$Store2 = Get-View $_.ID | Where {$_.Accessible -ne "True"}

foreach ($store in $Store2){

   $myObj = "" | Select-Object Name, FileSystem

   $myObj.Name = $store.Name

   $myObj.FileSystem = $store.Type

   $Report += $myObj

}

if ($Store2) {$Report | ConvertTo-Html -title "Inaccessible Datastores" -body  "<H2>Inaccessible Datastores</H2>" | Out-File -Append  $filelocation}

else {ConvertTo-Html -title "Inaccessible Datastores" -body  "<H3>All Datastores Accessible</H3>" | Out-File -Append  $filelocation}

Thanks,

VMSavvy..

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Report2 should say

$VMHosts2 = Get-VMHost | Where {$_.Extensiondata.Runtime.InMaintenanceMode -eq "True"} | Sort-Object Name

And for $report4 you can do

$report4 = Get-VMHost | Get-VmHostService | Where-Object {$_.Key -eq "ntpd" -and !$_.Running} | %{$_.VMHost}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

There were some issues with that script.

Try it with this version

$fileLocation = "C:\test.html"
$Report =@()

$store1 = Get-Datastore | where {$_.Extensiondata.Summary.MultipleHostAccess -and ($_.CapacityMB -ne 0 -and $_.FreeSpaceMB/$_.CapacityMB -le 0.05)} | Sort-Object Name

if
($store1){
   
foreach ($store in $Store1){
       
$myObj = "" | Select-Object Name, PercFree, FileSystem
       
$myObj.Name = $store.Name
       
$myObj.PercFree = "{0:N}" -f (100 * $store.FreeSpaceMB/$store.CapacityMB)
       
$myObj.FileSystem = $store.Type
       
$Report += $myObj    }
   
$Report | ConvertTo-Html -title "Datastore less than 5% free space" -body "<H2>Datastore less than 5% free space</H2>" | Out-File $filelocation}
else {
   
ConvertTo-Html -title "Datastores less than 5% free space" -body "<H3>No Datastores less than 5% free space</H3>" | Out-File $filelocation}

$Report = @()

$store2 = Get-Datastore | where {$_.Extensiondata.Summary.MultipleHostAccess -and $_.Accessible -ne "True"} | Sort-Object Name

if
($store2){
   
foreach ($store in $Store2){
       
$myObj = "" | Select-Object Name, FileSystem
        $myObj.Name = $store.Name
       
$myObj.FileSystem = $store.Type
       
$Report += $myObj    }

   
$Report | ConvertTo-Html -title "Inaccessible Datastores" -body "<H2>Inaccessible Datastores</H2>" | Out-File -Append $filelocation}
else {
   
ConvertTo-Html -title "Inaccessible Datastores" -body "<H3>All Datastores Accessible</H3>" | Out-File -Append $filelocation
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I'm getting this output after running this script.. there are close to 70 datastores in the vcenter which I'm running this script for.. I'm sure there are datastores with less than 5% free space and also one inaccessible datastore currently.. so there's something wrong in the script..or a setting may be?? pls help..

1776490.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

As a quick check can you run

Get-Datastore | Select Name,@{N="Free%";E={$_.FreeSpaceMB/$_.CapacityMB}}

Are there any where Free% is 0.05 or less ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

I get a list of datastores now.. and yes there are datastores that are less than 0.05..like the one below..this is one of them from the report

FC-Corp3-CX4-1791-52-11                                                 0.0483202760950606

VMSavvy..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, can you try again with the updated script above ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

VMSavvy
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I'm now getting this error..

Attempted to divide by zero.
At :line:21 char:41
+ $store1 = $MHost | where {$_.FreeSpaceMB/ <<<< $_.CapacityMB -le 0.05} | Sort-Object Name

I think there is a function called Traperror to fix this right.. haven't used it till now.. can you get me the syntax or any other alternative to bypass this error?

VMSavvy..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Trapping the error won't realy help, since we're in the middle of a where-clause.

Strange that you seem to have datastores that say they have 0 capacity.

In any case, I added an additional condition to the where-clause that should handle this exception.

Can you try again with the updated code above ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

vmhyperv
Contributor
Contributor
Jump to solution

Hi LucD,

    Can you help me.i ran the script but its shows error when i tried to find out the  DS space less than 5 %

[vSphere PowerCLI] C:\tmp> .\Data-Store-Space-less5.ps1
Sort-Object : A positional parameter cannot be found that accepts argument '$nu
ll'.
At C:\tmp\Data-Store-Space-less5.ps1:5 char:162
+ $store1 = Get-Datastore | where {$_.Extensiondata.Summary.MultipleHostAccess
-and ($_.CapacityMB -ne 0 -and $_.FreeSpaceMB/$_.CapacityMB -le 0.05)} | Sort-O
bject <<<<  Nameif($store1){    foreach ($store in $Store1){        $myObj = ""
| Select-Object Name, PercFree, FileSystem        $myObj.Name = $store.Name
     $myObj.PercFree = "{0:N}" -f (100 * $store.FreeSpaceMB/$store.CapacityMB)
       $myObj.FileSystem = $store.Type        $Report += $myObj    }    $Report
| ConvertTo-Html -title "Datastore less than 5% free space" -body "<H2>Datasto
re less than 5% free space</H2>" | Out-File $filelocation}else {    ConvertTo-H
tml -title "Datastores less than 5% free space" -body "<H3>No Datastores less t
han 5% free space</H3>" | Out-File $filelocation}$Report = @()$store2 = Get-Dat
astore | where {$_.Extensiondata.Summary.MultipleHostAccess -and $_.Accessible
-ne "True"} | Sort-Object Name if($store2){    foreach ($store in $Store2){
    $myObj = "" | Select-Object Name, FileSystem        $myObj.Name = $store.Na
me        $myObj.FileSystem = $store.Type        $Report += $myObj    }    $Rep
ort | ConvertTo-Html -title "Inaccessible Datastores" -body "<H2>Inaccessible D
atastores</H2>" | Out-File -Append $filelocation}else {    ConvertTo-Html -titl
e "Inaccessible Datastores" -body "<H3>All Datastores Accessible</H3>" | Out-Fi
le -Append $filelocation}
    + CategoryInfo          : InvalidArgument: (:) [Sort-Object], ParameterBin
   dingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
   .Commands.SortObjectCommand

Thanks

Kumar

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if the copy/paste didn't work for you.

Try the attached script


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

Thanks LucD for your help. Its working now Smiley Happy

Kumar

0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

Hey Luc.. That worked.. I consolidated all these into a final version script.. but now ran myself into two issues..

I've attached the script here..

Issue 1: Script doesn't pull out the hosts in maintenance mode.. (Pls look for $Report2)

Issue 2: Script doesn't list out hosts with NTPD in stopped state.. (Pls look for $Report4)

Not sure if I can post this issue in this thread..but just did it..

Completing this will get my maintenance project go live which has a deadline of next week.. Please help!!

Thanks in advance!!

VMSavvy..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Report2 should say

$VMHosts2 = Get-VMHost | Where {$_.Extensiondata.Runtime.InMaintenanceMode -eq "True"} | Sort-Object Name

And for $report4 you can do

$report4 = Get-VMHost | Get-VmHostService | Where-Object {$_.Key -eq "ntpd" -and !$_.Running} | %{$_.VMHost}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Sorry for the advertising (well its free) but I also wanted to make you aware that this and other checks similar to this are also all available in the vCheck script - im not sure if you have seen this yet, it sounds like it may be of help to you....

http://www.virtu-al.net/featured-scripts/vcheck/

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

That worked Luc.. Thanks a lot!!

VMSavvy Smiley Happy

0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

Hey Alan,

Of course yes.. I know your script.. I used its earlier version in my previous org.. but here we have a big setup with 4vcenters and close to 225 hosts and 3500 VMs.. how much time do you think it will take to run in this kind of a big setup??

also my management needs some customized scripts to get them worked in our environment.. also I've taken up ownership of this project myself to learn more scripting.. thought its time to learn besides just using a script which is a go ready type.. :smileylaugh:

Your script is awesome.. a one stop shop for daily maintenance stuff..

VMSavvy..

0 Kudos