Quantcast
Channel: VMware Communities : Discussion List - All Communities
Viewing all articles
Browse latest Browse all 178257

Powershell script help - creating a VM inventory

$
0
0

Hi - I need to create a very specific output for VM inventory purposes.

I have, by hook and by crook, coppled together a script that works well for my needs, except for one issue I cannot resolve.

My VMware estate is ESX 5.1 and multi regional with multi VC instances; and the aim is to schedule this script to output the same info from all VCs at regular intervals

 

My issue is with reporting on the VM guest disk info

The script runs okay and collects from the first cluster where the max number of disks per VM guest is 2.

The next cluster has VM guests with more than 2 disks, but the number of disks reported on will be 2 (this is becuase of cluster 1 having a max of 2 disks per VM).

The script works accurately if I run it against each cluster spearately eg the 3 disk cluster reports 3 disks if running the script against that cluster only - but I cannot get it to run accuratley when colecting from all the clusters

Any help will be greatly appreciated.

 

Apologies for those script/programming experts as I am sure this is crude and clunky, but like I said it almost works for me.

Also ignore all the # out commands this, for me is always an evolving, dynamic script.

 

The section in question is

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# Get Disk Usage Details

$DiskNum = 0 

  Foreach ($disk in $VM.Guest.Disk){

$Details | Add-Member -name "Disk$($DiskNum)path" -MemberType NoteProperty -value $Disk.DiskPath

$Details | Add-Member -Name "Disk$($DiskNum)Capacity(MB)" -MemberType NoteProperty -Value ("{0:n0}" -f ([Math]::Round($disk.Capacity/ 1MB)))

$Details | Add-Member -Name "Disk$($DiskNum)FreeSpace(MB)" -MemberType NoteProperty -Value ("{0:n0}" -f ([Math]::Round($disk.FreeSpace / 1MB)))

$DiskNum ++

  }

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

FULL SCRIPT:

 

Measure-Command {
$VMInventory = @()
$VCs =  "VC1", "VC2"

foreach ($VC in $VCs){

connect-viserver $VC -User "-adm-"  -Password ""

$Clusters = Get-Cluster
foreach ($Cluster in $Clusters) {
trap {
Out-File 'F:\PowerGUI\ScriptDumpFolder\VMInventory-ERRORLOG.txt';
  continue
}
$ESXhosts = $Cluster | get-vmhost
$firstESXhost = $ESXhosts[0]
$HardwareSpec = $firstESXhost | Get-View
$VMGet =  $cluster |Get-VM | Get-View


  foreach ($VM in $VMGet) {
 

# Array object content
   $Details = New-object PSObject
  $Details | Add-Member -Name Cluster -Value $Cluster.Name -Membertype NoteProperty
  #$Details | Add-Member -Name VMsPoweredOn -Value $CountVMsOn.Count -Membertype NoteProperty
  #$Details | Add-Member -Name VMsPoweredOff -Value $CountVMsOff.Count -Membertype NoteProperty
  # $Details | Add-Member -Name Name -Value $VM.Summary.Guest.HostName -Membertype NoteProperty
  $Details | Add-Member -Name Name -Value $VM.Config.Name -Membertype NoteProperty
  $Details | Add-Member -Name PowerState -Value $VM.Summary.Runtime.PowerState -Membertype NoteProperty
  $Details | Add-Member -Name GuestOS -Value $VM.Summary.Guest.GuestFullName -Membertype NoteProperty
  #$Details | Add-Member -Name ToolsActive -Value $VM.Guest.ToolsStatus -Membertype NoteProperty
  #$Details | Add-Member -Name ToolsStatus -Value $VM.Guest.ToolsVersionStatus -Membertype NoteProperty
  #$Details | Add-Member -Name ToolsVer -Value $VM.Guest.ToolsVersion -Membertype NoteProperty

 
  # Get VM RAM details >>> NOT Usage <<<
  #$vRAM = $VMGET | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average
$Details | Add-Member -Name "Allocated_vRAM" -MemberType NoteProperty -Value ([INT] $VM.Summary.Config.MemorySizeMB )
#$Details | Add-Member -Name "vRAM_AverageUsage%-24hours" -MemberType NoteProperty -Value ([INT]$vRAM.Average)

# Get VM CPU Usage
# $vCPU = $VMGET | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average
$VMNumSockets = $VM.Summary.Config.NumCpu
# $VMNumvCores = $VM.Config.ExtraConfig | where {$_.Key -eq "cpuid.coresPerSocket"}
     $VMNumvCores = $VM.Config.Hardware.NUMCoresPerSocket
$Details | Add-Member -Name "NumberOf_vSockets" -MemberType NoteProperty -Value $VMNumSockets
$Details | Add-Member -Name "NumberOf_vCores" -MemberType NoteProperty -Value $VMNumvCores
$Details | Add-Member -Name "NumberOf_vCPU" -MemberType NoteProperty -Value ([Math]::Round($VMNumSockets * $VMNumvCores))
$Details | Add-Member -Name "AllocatedGHz-vCPU" -MemberType NoteProperty -Value ("{0:n0}" -f (([Math]::Round($($VMNumSockets * $VMNumvCores) * $HardwareSpec.Hardware.CpuInfo.Hz)/ 1MB)))
# $Details | Add-Member -Name "vCPU_AverageUsageGHz-24hours" -MemberType NoteProperty -Value ([INT] $vCPU.Average)

# Get Disk Usage Details
$DiskNum = 0 
  Foreach ($disk in $VM.Guest.Disk){
$Details | Add-Member -name "Disk$($DiskNum)path" -MemberType NoteProperty -value $Disk.DiskPath
$Details | Add-Member -Name "Disk$($DiskNum)Capacity(MB)" -MemberType NoteProperty -Value ("{0:n0}" -f ([Math]::Round($disk.Capacity/ 1MB)))
$Details | Add-Member -Name "Disk$($DiskNum)FreeSpace(MB)" -MemberType NoteProperty -Value ("{0:n0}" -f ([Math]::Round($disk.FreeSpace / 1MB)))
$DiskNum ++
  }
  $VMInventory += $Details
    }
 
   }
  
  disconnect-viserver $vc -Confirm:$false

  }

}

$VMInventory  | Export-CSV 'F:\PowerGUI\ScriptDumpFolder\130617_SIv2_E1_VMReport.csv' -NoTypeInformation
    # $VMInventory  | Export-CSV 'F:\PowerGUI\ScriptDumpFolder\130614_SIv1_E1_VMReport.csv' -NoTypeInformation


Viewing all articles
Browse latest Browse all 178257

Trending Articles



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