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

Jump to navigation Jump to search
m (→‎Script Extracts: Added Synrtax Highlighting)
Line 272: Line 272:
=== VM's with Datastores List ===
=== 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 }}
<source lang="powershell">
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
</source>


=== VM's with Host and Cluster List ===
=== VM's with Host and Cluster List ===
$vms = Get-VM | sort -property Name
<source lang="powershell">
foreach ($vm in $vms)
$vms = Get-VM | sort -property Name
{
foreach ($vm in $vms)
$vm | Get-Cluster | Select-Object @{Name="VM"; Expression={$vm.name}},@{Name="Current Host"; Expression={$vm.host}},Name
{
}
    $vm | Get-Cluster | Select-Object @{Name="VM"; Expression={$vm.name}},@{Name="Current Host"; Expression={$vm.host}},Name
}
</source>


=== VM's Inventory CSV ===
=== VM's Inventory CSV ===


<pre>
<source lang="powershell">
$start = Get-Date
$start = Get-Date


Line 338: Line 342:
$table | Format-Table
$table | Format-Table
$table | Export-Csv -path result.csv
$table | Export-Csv -path result.csv
</pre>
</source>




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