Skip to content

Instantly share code, notes, and snippets.

@fr0gger03
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fr0gger03/b9136768a85a7d1b4321 to your computer and use it in GitHub Desktop.
Save fr0gger03/b9136768a85a7d1b4321 to your computer and use it in GitHub Desktop.
PowerCLI: script to map Virtual Machine to underlying VMFS Datastore (block only)
# you must connect and authenticate to a vCenter server
# use Connect-VIServer to do so
# establish Export file name
$csvname = Read-Host 'Please provide the file path and name for your CSV export'
# Gather information on all datacenters and datastores, then enter loop
Get-Datacenter | Get-Datastore | Foreach-Object{
# capture datastore name in variable
$ds = $_.Name
# capture state of datastore in variable
$accessible=$_.State
# for each datacenter, get the virutal machine, select specific properties,
# and export it all to a CSV file
$_ | Get-VM | Select-Object @{n=‘VMName’;e={$_.Name}},@{n=’PowerState';e={$_.PowerState}},@{n=‘ESXi Host Name’;e={$_.VMhost}},@{n=‘DataStore’;e={$ds}},@{n=‘Accessible’;e={$accessible}},@{n=’Type’;e={$_.Type}} | Export-CSV $csvname -NoTypeInformation –Append
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment