Difference between revisions of "Script Extracts and Examples (PowerCLI)"

Jump to navigation Jump to search
m
→‎Script Extracts: Added "VM's Inventory CSV"
m (→‎VM Datastores: Renamed to "Script Extracts" and added "List VM's with Host and Cluster")
m (→‎Script Extracts: Added "VM's Inventory CSV")
Line 131: Line 131:


== Script Extracts ==
== Script Extracts ==
=== List VM's with Datastores ===
=== VM's with Datastores List ===
List of Virtual Machines, and their datastores (with usage)
List of Virtual Machines, and their datastores (with usage)
  $datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
  $datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
Line 137: Line 137:
  get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
  get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"


 
=== VM's with Host and Cluster List ===
=== List VM's with Host and Cluster ===
  $vms = Get-VM | sort -property Name
  $vms = Get-VM | sort -property Name
  foreach ($vm in $vms)
  foreach ($vm in $vms)
Line 145: Line 144:
  }
  }


=== VM's Inventory CSV ===
<pre>
$start = Get-Date
# Create table for output
# Name DC OS UUID IP Cluster ESX's
$table = New-Object system.Data.DataTable "Results"
$col1 = New-Object system.Data.DataColumn Name,([string])
$col2 = New-Object system.Data.DataColumn DC,([string])
$col3 = New-Object system.Data.DataColumn OS,([string])
$col4 = New-Object system.Data.DataColumn UUID,([string])
$col5 = New-Object system.Data.DataColumn MgmtIP,([string])
$col6 = New-Object system.Data.DataColumn Cluster,([string])
#$col7 = New-Object system.Data.DataColumn ESXs,([string])
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
#$table.columns.add($col7)
$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
"Created table after $duration secs"
# Get VMs object
$vms = Get-VM | Sort -property Name
$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
"Got object list of VM's after $duration secs"
foreach ($vm in $vms)
{
$row = $table.NewRow()
$row.Name = (Get-VM -Name $vm).Name
$row.DC = (Get-Datacenter -VM $vm).Name
$row.OS = (Get-VMGuest -VM $vm).OSFullName
$row.UUID = %{(Get-View $vm.Id).config.uuid}
$row.MgmtIP =  [string]::join(" ", ((Get-VMGuest -VM $vm).IPAddress)) # Need to join potential list of IP's
$row.Cluster = (Get-Cluster -VM $vm).Name
$table.Rows.Add($row)
"Added row for $vm"
}
$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
"Populated table after $duration secs"


$table | Format-Table
$table | Export-Csv -path result.csv
</pre>




[[Category:VMware]]
[[Category:VMware]]

Navigation menu