Automation

 View Only
  • 1.  Trying to export VMs and their associated Datastore names to CSV

    Posted Feb 22, 2024 04:30 PM

    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



  • 2.  RE: Trying to export VMs and their associated Datastore names to CSV

    Posted Feb 22, 2024 05:00 PM

    Try with

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


  • 3.  RE: Trying to export VMs and their associated Datastore names to CSV

    Posted Feb 22, 2024 05:24 PM
      |   view attached

    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



  • 4.  RE: Trying to export VMs and their associated Datastore names to CSV
    Best Answer

    Posted Feb 22, 2024 05:32 PM

    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]
    





  • 5.  RE: Trying to export VMs and their associated Datastore names to CSV

    Posted Feb 22, 2024 05:39 PM

    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"



  • 6.  RE: Trying to export VMs and their associated Datastore names to CSV

    Posted Feb 22, 2024 06:06 PM

    Also with the updated line in my previous reply?



  • 7.  RE: Trying to export VMs and their associated Datastore names to CSV

    Posted Feb 22, 2024 06:09 PM

    BOOM! That works! You rock LucD!

     

    Thank you!