Get-DATASTORE

The script below is a basic example to collect all available information from a VMware platform, The script will connect to a VMware vCenter and export the information to a “.CSV” file format replacing the delimiter with a “^” symbol.Get-DATASTORE

Get-DATASTOREGet-DATASTOREGet-DATASTOREGet-DATASTOREGet-DATASTORE

The first part of the script will Load the required VMware Powershell snap-in’s.

foreach ($snapin in $LoadedSnapins){
$snapin
if ($snapin.name -eq “VMware.VimAutomation.Core”)
{$moduleLoaded = “TRUE”}
}
if ($moduleLoaded -ne “TRUE”){Add-PSSnapin VMware.VimAutomation.Core}

Then the next line will set the powershell configuration, Ignoring any certificates and set the user scope.

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false

In the next part of the script we define the script Variables and set the Powershell commands into the variables, in the below we are extracting the information in a specific order, this is to enable an import into a MS SQL Database to help plan capacity.

$Servers = @(“Your vCenter FQDN or IP Address“) # accepts multiple inputs use a comma to separate the VMware vCenter Servers

$user = “Domain\ReadOnlyServiceAccount

 

$Pass = “!!!YourPassword!!!

$getdatastore = “Get-Datastore | select FileSystemVersion,DatacenterId,Datacenter,ParentFolderId,ParentFolder,DatastoreBrowserPath,FreeSpaceMB,CapacityMB,Accessible,Type,StorageIOControlEnabled,CongestionThresholdMillisecond,State,CapacityGB,FreeSpaceGB,Name,Id,Uid”

#Define the Output Directory
$dirRoot = “Somewhere local to your laptop, server\”

We now move on to connecting to the VMware vCenter Server and executing the powershell script.

foreach ($Server in $Servers){
$VIConnection = Connect-VIServer -Server $Server -user $user -Password $Pass
$getVMdatastoreFilePath = $dirRoot + “getVMdatastore_” + $VIConnection.name + “.csv”
# Invoke Script
invoke-expression $getdatastore | export-csv $getVMdatastoreFilePath -delimiter ^ -notype
# Take out the quotation marks in the CSV file.
(get-content $getVMdatastoreFilePath) | % {$_ -replace ‘”‘, “”} | out-file $getVMdatastoreFilePath -Fo -En ascii
#Disconnect from the VCenter
Disconnect-VIServer * -Confirm:$False
}

And that’s it you will now have a CSV file with the required VM Guest information, with a delimiter “^”, this file can then be used to populate a Database to track capacity.

 

 

#Load Powershell snapins
foreach ($snapin in $LoadedSnapins){
$snapin
if ($snapin.name -eq “VMware.VimAutomation.Core”)
{$moduleLoaded = “TRUE”}
}
if ($moduleLoaded -ne “TRUE”){Add-PSSnapin VMware.VimAutomation.Core}
#Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope User -Confirm:$false
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false
#Create list of vCenter Servers to connect to
$Servers = @(“vCenter FQDN or IP Address“) # accepts multiple inputs use a comma to separate the VMware vCenter Servers
$user = “Domain\ServiceAccount”
$Pass = “!SecurePassword!”#Place CMDLET into a variable.
$getdatastore = “Get-Datastore | select FileSystemVersion,DatacenterId,Datacenter,ParentFolderId,ParentFolder,DatastoreBrowserPath,FreeSpaceMB,CapacityMB,Accessible,Type,StorageIOControlEnabled,CongestionThresholdMillisecond,State,CapacityGB,FreeSpaceGB,Name,Id,Uid”

#Define the Output Directory
$dirRoot = “Share:\SomeLocation\”

#Connect to vCenter servers in a different loop and collect information from CMDLET
foreach ($Server in $Servers){
$VIConnection = Connect-VIServer -Server $Server -user $user -Password $Pass
$getVMdatastoreFilePath = $dirRoot + “getVMdatastore_” + $VIConnection.name + “.csv”
# Invoke Script
invoke-expression $getdatastore | export-csv $getVMdatastoreFilePath -delimiter ^ -notype
# Take out the quotation marks in the CSV file.
(get-content $getVMdatastoreFilePath) | % {$_ -replace ‘”‘, “”} | out-file $getVMdatastoreFilePath -Fo -En ascii
#Disconnect from the VCenter
Disconnect-VIServer * -Confirm:$False
}