VMware Cloud Community
jward_
Contributor
Contributor
Jump to solution

Trying to export VMs and their associated Datastore names to CSV

Trying to export VMs and their associated Datastore names to CSV but for the datastore I just extract the "Array name" from the Datastore name. The array name always starts with "P" and has 2 numbers after but the number of characters before, differers.

Please help for the sake of my head (banging on desk)!!

Example:

ServerA   NPR_Cluster5_P55_200

ServerB   PR_Cluster12_P77_350

I want:

ServerA   P55

ServerB.  P77

 

$Report=@()

 

$clusters = Get-Cluster | where {$_.Name -like "*BAK*"}

foreach ($cluster in $clusters){

 

 

$VMs = $cluster | Get-VM | where {$_.Guest -like "*Windows*"}

 

 

foreach ($VM in $VMs){

 

$datastore = $VM | Get-Datastore | Select-Object -ExpandProperty name -First 1

$VMGuest = $VM.ExtensionData.Guest.GuestFullName

 

 

$Details ="" | Select VMName,Cluster,Datastore

$Details.VMName =$VM.Name

$Details.Cluster = $VM.VMHost.Parent

$Details.Datastore = $datastore

$Report +=$Details

}

}

 

$Report | Export-Csv -NoTypeInformation C:\temp\r_VM_to_Array.csv

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm confused, what is the datastore name?
Is it "ServerA   NPR_Cluster5_P55_200" or "NPR_Cluster5_P55_200"?
If it is the latter then the line should be

        $Details.Datastore = $datastore.Split('_')[2]




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Tags (1)
6 Replies
LucD
Leadership
Leadership
Jump to solution

Try with

        $Details.Datastore = "$($datastore.Split(' ')[0]) $($datastore.Split('_')[2])"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jward_
Contributor
Contributor
Jump to solution

LucD,

Thank you! That kinda works. It still has the datastore name in front of the Array. Any way we can remove the Datastore name from the output and leave the Array?

 

See screenshot

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm confused, what is the datastore name?
Is it "ServerA   NPR_Cluster5_P55_200" or "NPR_Cluster5_P55_200"?
If it is the latter then the line should be

        $Details.Datastore = $datastore.Split('_')[2]




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tags (1)
jward_
Contributor
Contributor
Jump to solution

the Datastore name is "NPR_Cluster5_P55_200" 

I am trying to just get the Array from the name, which is "P55"

your edit did that but it also left the datastore name at the beginning which looked like this "NPR_Cluster5_P55_200 P55"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Also with the updated line in my previous reply?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jward_
Contributor
Contributor
Jump to solution

BOOM! That works! You rock LucD!

 

Thank you!

Reply
0 Kudos