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
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
Try with
$Details.Datastore = "$($datastore.Split(' ')[0]) $($datastore.Split('_')[2])"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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"
Also with the updated line in my previous reply?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
BOOM! That works! You rock LucD!
Thank you!