Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 179681

How to extend/optimize this multi vc datastore inventory script

$
0
0

Hi,

 

Trying to setup a script for datastore inventory. This is what i have so far.

I would like to extend this datastore inventory script.

      1.) How to add additional  properties to this script?
      $row.HostsConnected (i.e. as one joined string esxhost1|esxhost2|esxhost3),
      $row.VMsHosted (i.e. as one joined string VM1|VM2|VM3)
      $row.SiocEnabled (True/False),
      $row.PartofStorageCluster (True/False)
      $row.LUNUUID (yes LUNUUID (fast method there) of Datastore_Name processed during for each loop)

working on

vSphere 5.1.x infra

Powercli 5.1.x

 

Thanks is advance

 

 

# start script
$LogTimeStamp = (Get-Date).ToString("dd-MM-yyyy_HH_mm")
$ScanDate = Get-Date -format "yyyy-MM-dd"
$ScanTime = Get-Date -format "HH:mm"
$ScanDateTimeStamp= Get-Date -format "yyyy-MM-dd HH:mm:00.000"

Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false
Connect-Viserver "vCenter1.mydomain.corp", "vCenter2.mydomain.corp"

$currDir = Split-path -parent $MyInvocation.MyCommand.Definition
$outputFile = $currDir + "\All_VC" + "_" + $LogTimeStamp + "_datastore_inventory.csv"

if (Test-Path $outputFile)
{
    Remove-Item $outputFile
}

cls
$clusterColl = @(Get-Cluster) | Sort-Object Name

$i=0
$b=0
$b = $clusterColl.Count

$report = New-Object Collections.Generic.LinkedList[PSObject]
foreach($dc in get-datacenter) {
foreach($cluster in Get-Cluster -Location $dc) {
    $i++
    write-progress "Please wait....Datastore inventory in progress..." "Cluster ($i of $b) >> $cluster" 1 -perc (($i / $b)*100)
   
    $VMHost = $cluster | Get-VMHost | select -First 1
write-progress "Selected vSphere server: " "vSphere server >> $VMHost" 2 -perc 100 -parentid 1
$VMHost | Get-DataStore | Where-Object { $_.Name -notlike "*local*"} | Sort-Object Name | get-view | %{
            $row = "" | Select-Object Rec_ID, ScanDate, ScanTime, Infra, vCenter, Datacenter,
            Cluster, Datastore_Name, DeviceID, BlockSizeMB,Vmfsupgradable,Version,Type,
            MaxBlocks,URL, VMs, VMsHosted, Hosts, HostsConnected, Capacity_GB, Used_spaceGB, Free_spaceGB, PercentFree, PercentUsed, LUN_UUID, ScanDateTimeStamp
    
    
     $row.Rec_ID=""
            $row.ScanDate = $ScanDate
            $row.ScanTime = $ScanTime
     $row.ScanDateTimeStamp=$ScanDateTimeStamp
     $row.vCenter=$VMHost.extensiondata.Client.ServiceUrl.Split('/')[2].replace(":443","")

            Switch ($row.vCenter) {
  "vCenter1.mydomain.corp" {$row.Infra="TDA"}
  "vCenter1.mydomain.corp" {$row.Infra="PROD"}
     }

     $row.Datacenter = [string]$dc.Name


            $row.Cluster = [string]$Cluster.Name
            $row.Datastore_Name = [string]$($_.info.Name)

 
     if ($_.info.URL.StartsWith("netfs")) {
   # write-host "Exclude NFS datastores"
     }
     else
     {
             $row.Capacity_GB = [int]([math]::round($($_.info.vmfs.Capacity / 1024/1024/1024),2))
      $row.Free_spaceGB = [int]([math]::round($($_.info.Freespace / 1024/1024/1024),2))
      $row.Used_spaceGB = [int]([math]::round($row.Capacity_GB - $row.Free_spaceGB,2))
             $row.PercentFree = [int][math]::round(($row.Free_spaceGB / $row.Capacity_GB * 100),2)
             $row.PercentUsed = [int]100 - $row.PercentFree
      $row.VMs=[int]$($_.vm.count)
      $row.VMsHosted="..."
  $row.Hosts=[int]$($_.host.count)
      $row.HostsConnected="..."
  $row.DeviceID=[string]$($_.info.Vmfs.Extent[0].DiskName)
      $row.BlockSizeMB=[int]$($_.info.vmfs.BlockSizeMB)
      $row.Version=[string]$($_.info.vmfs.Version)
      $row.Type=[string]$($_.info.vmfs.Type)
      $Row.MaxBlocks=[int]$($_.info.vmfs.MaxBlocks)
      $Row.URL=[string]$($_.info.URL)
      $Row.vmfsupgradable=[bool]$($_.info.vmfs.vmfsupgradable)
  $Report.AddLast($row) | Out-Null
     }

       }
   }

}

$report | Sort-Object Free_spaceGB -Descending | Export-Csv -NoTypeInformation -Path $outputFile

# end script


Viewing all articles
Browse latest Browse all 179681

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>