Export all VMs on specific datastores to CSV

JDMils 51 Reputation points
2022-12-21T07:12:41.427+00:00

This has been driving me batty for hours now and I have not gotten anywhere with it. I have a list of datastores which I want to extract a list of all VM names from and export the list to CSV.

Here's what I have:
Datastore Names are in a CSV file called "_SYD_Datastore_Names.csv"
Data format is:
Extracted volume name
DS1_t1_SYD_vmdata_0004
DS2_t1_SYD_vmdata_0017
DS3_t1_SYD_vmdata_0024

The output of the code is a separate CSV file per datastore which strangely contains the following data:
"Length"
"23"

Here are the values of my variables:
$DatastoreName
Value = DS1_t1_SYD_vmdata_0004
BaseType = System.Object

$oDatastore
Value =
Name FreeSpaceGB CapacityGB
---- ----------- ----------
DS1_t1_SYD_vmdata_0004 467.628 500.000
BaseType = VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.DatastoreImpl

$VMList
Value = DS1_t1_SYD_vmdata_0004
BaseType = System.Array

My code:
#
# This function shows the Open File dialog to the user so the user can
# select the import CSV file.
#
Function Get-FileName($InitialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog  
    $OpenFileDialog.initialDirectory = $initialDirectory  
    $OpenFileDialog.filter = "CSV (*.csv) | *.csv"  
    $OpenFileDialog.ShowDialog() | Out-Null  
    $OpenFileDialog.FileName  
    }  
  
# Calling the function:  
# -----------------------------------------------------------  
  
# Get the CSV import file name and path:  
$ImportCSVFile = Get-FileName  
  
# Check if the user cancelled the request.  
if ($ImportCSVFile -eq "")  
    { # They did!  
    Throw "No file selected. Ending script"  
    }  
  
# Load the file contents  
# Time to get going!  
$VMData = Import-CSV $ImportCSVFile -ErrorAction SilentlyContinue  
  
ForEach( $Datastore in $VMData)  
    {  
    Try  
        {  
        $DatastoreName = ($Datastore | Select -expandproperty "Extracted volume name")  
        $oDatastore = Get-Datastore -Name $DatastoreName  
        $VMList = Get-VM -Datastore $oDatastore  
        }  
    Catch  
        {  
        $VMList = "Datastore Not Found"  
        }  
    Finally  
        {  
        $FileName1 = "DS_" + $DatastoreName + ".csv"  
        $VMList | Export-Csv -Path $FileName1 -NoTypeInformation  
        }  
    }  
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-01-04T09:01:52.03+00:00

    Hi,

    The CSV file contains "Length" "23" because the string "Datastore Not Found" is written to it. You can modify Line 34 to $VMList = [pscustomobject]@{string="Datastore Not Found"}

    Best Regards,
    Ian Xue

    -----------------------------

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.