PowerCLI

 View Only
Expand all | Collapse all

List Cluster, Host and Datastores

  • 1.  List Cluster, Host and Datastores

    Posted Dec 04, 2012 03:28 PM

    Hi

    I'm fairly new to PowerCLI. Is there a way to list Cluster, ESX Host and datastore in that order?

    Also does someone point me to some documentation on how to drill down to the .net objects that can be queried?

    Thanks



  • 2.  RE: List Cluster, Host and Datastores

    Posted Dec 04, 2012 04:32 PM

    You can use the PowerCLI Reference to drill down the objects.

    Select the All Types on the left, or go to a cmdlet and follow the Return Type.



  • 3.  RE: List Cluster, Host and Datastores

    Posted Dec 04, 2012 04:54 PM

    The following PowerCLI script lists the clusters, ESX hosts and connected datastores:

    & { foreach ($Cluster in (Get-Cluster)) {
        foreach ($VMHost in (Get-VMHost -Location $Cluster)) {
          $VMHost | Get-Datastore |
          Select-Object -Property @{Name="Cluster";Expression={$Cluster.Name}},
            @{Name="VMHost";Expression={$VMHost.Name}},
            @{Name="Datastore";Expression={$_.Name}}
        }
      }
    }
    
    



  • 4.  RE: List Cluster, Host and Datastores

    Posted Dec 04, 2012 05:30 PM

    It works good but I'm having trouble exporting to a csv file:

    & { foreach ($Cluster in (Get-Cluster)) {
        foreach ($VMHost in (Get-VMHost -Location $Cluster)) {
          $VMHost | Get-Datastore |
          Select-Object -Property @{Name="Cluster";Expression={$Cluster.Name}},
            @{Name="VMHost";Expression={$VMHost.Name}},
            @{Name="Datastore";Expression={$_.Name}} |
            Export-Csv -Path DatacenterInfo2.csv -NoTypeInformation -UseCulture
        }
      }
    }



  • 5.  RE: List Cluster, Host and Datastores
    Best Answer

    Posted Dec 04, 2012 05:34 PM

    The Export-CSV should be at the end of the script:

    & { foreach ($Cluster in (Get-Cluster)) {
        foreach ($VMHost in (Get-VMHost -Location $Cluster)) {
          $VMHost | Get-Datastore |
          Select-Object -Property @{Name="Cluster";Expression={$Cluster.Name}},
            @{Name="VMHost";Expression={$VMHost.Name}},
            @{Name="Datastore";Expression={$_.Name}}
        }
      }
    } | Export-Csv -Path DatacenterInfo2.csv -NoTypeInformation -UseCulture 
    
    



  • 6.  RE: List Cluster, Host and Datastores

    Posted Dec 04, 2012 05:44 PM

    That did it.

    Thanx



  • 7.  RE: List Cluster, Host and Datastores

    Posted Dec 12, 2012 09:48 PM

    Hi Robert

    I'm not sure if this could be done or not but is it possible to add another column something like this:

    @{Name="Not Presented";Expression={$_.Name}}

    If ($VMhost -ne Datastore) ("Not Present" = "No")

    Else

    If ($VMhost -eq Datastore) ("Not Present" = "Yes")



  • 8.  RE: List Cluster, Host and Datastores

    Posted Dec 13, 2012 09:01 PM

    I am not sure if this is what you want but the following script adds a column "Datastore Present"  which value is yes if the host has a datastore presented and no if the host has no datastores presented.

    & { foreach ($Cluster in (Get-Cluster)) {
        foreach ($VMHost in (Get-VMHost -Location $Cluster)) {
          $VMHost | Get-Datastore |
          Select-Object -Property @{Name="Cluster";Expression={$Cluster.Name}},
            @{Name="VMHost";Expression={$VMHost.Name}},
            @{Name="Datastore";Expression={$_.Name}},
            @{Name="Datastore Present";Expression={"Yes"}}
          $VMHost |
          Where-Object { -not ($_ | Get-Datastore) } |
          Select-Object -Property @{Name="Cluster";Expression={$Cluster.Name}},
            @{Name="VMHost";Expression={$VMHost.Name}},
            @{Name="Datastore";Expression={$null}},
            @{Name="Datastore Present";Expression={"No"}}
        }
      }
    } | Export-Csv -Path DatacenterInfo2.csv -NoTypeInformation -UseCulture 
    
    



  • 9.  RE: List Cluster, Host and Datastores

    Posted Dec 14, 2012 12:52 PM

    Hi Robert

    I was looking more along the lines of being able to report whether or not a Datastore is presented to all hosts in a Cluster.

    Again I'm not sure if this can done with PowerCLI code. Maybe it can be done with native PowerShell code?

    Thanks



  • 10.  RE: List Cluster, Host and Datastores

    Posted Dec 14, 2012 12:54 PM


  • 11.  RE: List Cluster, Host and Datastores

    Posted Dec 14, 2012 01:24 PM

    Hi Luc

    Great script!

    I like to hard code the cluster names and csv file like.

    For example:

    $ClusName="*"
    $CSVName="Test2.csv"

    How would I be able to list all of the cluster names in the report and not the CanonicalName ?

    Thanks



  • 12.  RE: List Cluster, Host and Datastores

    Posted Dec 14, 2012 03:05 PM

    The clustername is in the report (first column).

    Combining different clusters in 1 CSV will be a bit tricky, since the script uses objects where some of the properties are named after the nodes in a cluster (see the esx01, esx02... columns in the sample)

    The easiest solution is to save each cluster report in a separate worksheet in an XLS file.

    See my Export-Xls function in Beyond Export-Csv: Export-Xls



  • 13.  RE: List Cluster, Host and Datastores

    Posted Dec 14, 2012 07:11 PM

    Hi Luc

    Is there a work around for your script for hosts that have IP addresses as the host name?



  • 14.  RE: List Cluster, Host and Datastores

    Posted Dec 16, 2012 03:20 AM

    Hi Luc

    I was able to use your LUN report and Export-Xls scripts successfully but for some reason only the first cluster is sorted by dsName.

    #Load PowerCLI snapin
    #add-pssnapin VMware.VimAutomation.Core


    # Create array of vCenter hosts to connect to
    #############################################
    $hosts = @(esx01)

    <#
        "esx02",       
        "esx03",
        "esx04")
        #>
    # Connect to servers
    ################################
    Connect-VIServer -Server $hosts
    ################################

    ##Beyond Export-Csv: Export-Xls Function created by Luc Dekens
    #Does not work with Office 2003 and earlier versions of Excel
    ###############################################################################
    #requires -version 2

    function Export-Xls{
    <#
    .SYNOPSIS
    Saves Microsoft .NET Framework objects to a worksheet in an XLS file
    .DESCRIPTION
    The Export-Xls function allows you to save Microsoft .NET Framework objects
    to a named worksheet in an Excel file (type XLS). The position of the
    worksheet can be specified.
    .NOTES
    Author:  Luc Dekens
    .PARAMETER InputObject
    Specifies the objects to be written to the worksheet. The parameter accepts
    objects through the pipeline.
    .PARAMETER Path
    Specifies the path to the XLS file.
    .PARAMETER WorksheetName
    The name for the new worksheet. If not specified the name will
    be "Sheet" followed by the "Ticks" value
    .PARAMETER SheetPosition
    Specifies where the new worksheet will be inserted in the series of
    existing worksheets. You can specify "begin" or "end". The default
    is "begin".
    .PARAMETER ChartType
    Specifies the type of chart you want add to the worksheet.
    All types in the [microsoft.Office.Interop.Excel.XlChartType]
    enumeration are accepted.
    .PARAMETER NoTypeInformation
    Omits the type information from the worksheet. The default is to
    include the "#TYPE" line.
    .PARAMETER AppendWorksheet
    Specifies if the worksheet should keep or remove the existing
    worksheet in the spreadsheet. The default is to append.
    .EXAMPLE
    PS> $data = Get-Process | Select-Object Name, Id, WS
    PS> Export-Xls $data C:\Reports\MyWkb.xls -WorksheetName "WS" -AppendWorksheet:$false
    .EXAMPLE
    PS> $data = Get-Process | Select-Object Name, Id, WS
    PS> Export-Xls $data C:\Reports\MyWkb.xls -SheetPosition "end"
    .EXAMPLE
    PS> $data = Get-Process | Select-Object Name, Id, WS
    PS> Export-Xls $data C:\Reports\MyWkb.xls -WorksheetName "WS" -ChartType "xlColumnClustered"
    #>
    param(
    [parameter(ValueFromPipeline = $true,Position=1)]
    [ValidateNotNullOrEmpty()]
    $InputObject,
    [parameter(Position=2)]
    [ValidateNotNullOrEmpty()]
    [string]$Path,
    [string]$WorksheetName = ("Sheet " + (Get-Date).Ticks),
    [string]$SheetPosition = "begin",
    [PSObject]$ChartType,
    [switch]$NoTypeInformation = $true,
    [switch]$AppendWorksheet = $true
    )

    begin{
      [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel")
      if($ChartType){
       [microsoft.Office.Interop.Excel.XlChartType]$ChartType = $ChartType
      }

      function Set-ClipBoard{
       param(
        [string]$text
       )
       process{
        Add-Type -AssemblyName System.Windows.Forms
        $tb = New-Object System.Windows.Forms.TextBox
        $tb.Multiline = $true
        $tb.Text = $text
        $tb.SelectAll()
        $tb.Copy()
       }
      }

      function Add-Array2Clipboard {
       param (
        [PSObject[]]$ConvertObject,
        [switch]$Header
       )
       process{
        $array = @()

        if ($Header) {
         $line =""
         $ConvertObject | Get-Member -MemberType Property,NoteProperty,CodeProperty | Select -Property Name | %{
          $line += ($_.Name.tostring() + "`t")
         }
         $array += ($line.TrimEnd("`t") + "`r")
        }
        else {
         foreach($row in $ConvertObject){
          $line =""
          $row | Get-Member -MemberType Property,NoteProperty | %{
           $Name = $_.Name
           if(!$Row.$Name){$Row.$Name = ""}
           $line += ([string]$Row.$Name + "`t")
          }
          $array += ($line.TrimEnd("`t") + "`r")
         }
        }
        Set-ClipBoard $array
       }
      }

      $excelApp = New-Object -ComObject "Excel.Application"
      $originalAlerts = $excelApp.DisplayAlerts
      $excelApp.DisplayAlerts = $false
      if(Test-Path -Path $Path -PathType "Leaf"){
       $workBook = $excelApp.Workbooks.Open($Path)
      }
      else{
       $workBook = $excelApp.Workbooks.Add()
      }
      $sheet = $excelApp.Worksheets.Add($workBook.Worksheets.Item(1))
      if(!$AppendWorksheet){
       $workBook.Sheets | where {$_ -ne $sheet} | %{$_.Delete()}
      }
      $sheet.Name = $WorksheetName
      if($SheetPosition -eq "end"){
       $nrSheets = $workBook.Sheets.Count
       2..($nrSheets) |%{
        $workbook.Sheets.Item($_).Move($workbook.Sheets.Item($_ - 1))
       }
      }
      $sheet.Activate()
      $array = @()
    }

    process{
      $array += $InputObject
    }

    end{
      Add-Array2Clipboard $array -Header:$True
      $selection = $sheet.Range("A1")
      $selection.Select() | Out-Null
      $sheet.Paste()
      $Sheet.UsedRange.HorizontalAlignment = [microsoft.Office.Interop.Excel.XlHAlign]::xlHAlignCenter
      Add-Array2Clipboard $array
      $selection = $sheet.Range("A2")
      $selection.Select() | Out-Null
      $sheet.Paste() | Out-Null
      $selection = $sheet.Range("A1")
      $selection.Select() | Out-Null

      $sheet.UsedRange.EntireColumn.AutoFit() | Out-Null
      $workbook.Sheets.Item(1).Select()
      if($ChartType){
       $sheet.Shapes.AddChart($ChartType) | Out-Null
      }
      $workbook.SaveAs($Path)
      $excelApp.DisplayAlerts = $originalAlerts
      $excelApp.Quit()
      Stop-Process -Name "Excel"
    }
    }
    #End Export-Xls Function
    ###############################################################################

    #Get Presented LUNS - LUN Report script created by Luc Dekens
    ###############################################################################
    #<#

    Function GetLun{

    #param($clusName,$csvName=("C:\Temp\" + $clusName + "-LUN.csv"))
    param($clusName)

    $rndNum = Get-Random -Maximum 99999
    public string Cluster;

    public string CanonicalName;
    public string Datastore;
    public string SizeMB;

    "@
    $LunInfoDef = "public struct LunInfo" + $rndNum + "{`n" + $LunInfoDef

    $esxServers = Get-Cluster $clusName | Get-VMHost | Sort-Object -Property Name
    $esxServers | %{
    $LunInfoDef += ("`n`tpublic string " + ($_.Name.Split(".")[0]) + ";")
    }
    $LunInfoDef += "`n}"

    Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef

    $scsiTab = @{}
    $esxServers | %{
    $esxImpl = $_

    # Get SCSI LUNs
    $esxImpl | Get-ScsiLun | where {$_.LunType -eq "Disk"} | %{

      $key = $esxImpl.Name.Split(".")[0] + "-" + $_.CanonicalName.Split(".")[1]
      if(!$scsiTab.ContainsKey($key)){

       $scsiTab[$key] = $_.CanonicalName,"",$_.CapacityMB
      }
    }

    #Where-Object {$_.Name -notlike "DataStore1*"}
    # Get the VMFS datastores
    $esxImpl | Get-Datastore | where {$_.Type -eq "VMFS"} | Where-Object {$_.Name -notlike "DataStore1*"} | Get-View | %{
      $dsName = $_.Name
      $_.Info.Vmfs.Extent | %{
       $key = $esxImpl.Name.Split(".")[0] + "-" + $_.DiskName.Split(".")[1]
       $scsiTab[$key] = $scsiTab[$key][0], $dsName, $scsiTab[$key][2]
       
            }
    }
    }

    # Get the RDM disks
    Get-Cluster $clusName | Get-VM | Get-View | %{
    $vm = $_
    $vm.Config.Hardware.Device | where {$_.gettype().Name -eq "VirtualDisk"} | %{
      if("physicalMode","virtualmode" -contains $_.Backing.CompatibilityMode){
       $disk = $_.Backing.LunUuid.Substring(10,32)
       $key = (Get-View $vm.Runtime.Host).Name.Split(".")[0] + "-" + $disk
       $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label
      }
    }
    }

    $scsiTab.GetEnumerator() | Group-Object -Property {$_.Key.Split("-")[1]} | %{
    $lun = New-Object ("LunInfo" + $rndNum)
    $lun.Cluster = $clusName
    $_.Group | %{
      $esxName = $_.Key.Split("-")[0]
      $lun.$esxName = "X"
      if(!$lun.CanonicalName){$lun.CanonicalName = $_.Value[0]}
      if(!$lun.Datastore){$lun.Datastore = $_.Value[1]}
      if(!$lun.SizeMB){$lun.SizeMB = $_.Value[2]}
            
           }
    $lun
        }
      } GetLun "Cluster1" | Sort $dsName | Export-Xls -Path S:\Scripts\Reports\LUN-Report.xls -WorksheetName "Cluster1" 
        GetLun "Cluster2" | Sort $dsName | Export-Xls -Path S:\Scripts\Reports\LUN-Report.xls -WorksheetName "Cluster2"
        GetLun "Cluster3" | Sort $dsName | Export-Xls -Path S:\Scripts\Reports\LUN-Report.xls -WorksheetName "Cluster3"
        

    #Sort $dsName | Export-Csv $csvName -NoTypeInformation -UseCulture
    #Invoke-Item $csvName

    #>
    #End Get Presented LUNS
    #####################
    # Disconnect servers
    ####################
    Disconnect-VIServer $hosts -Confirm:$False
    ##end of script





  • 15.  RE: List Cluster, Host and Datastores

    Posted Dec 16, 2012 09:36 AM

    There doesn't seem to be anything in the variable $dsName, it's not initialised.

    The objects that are returned have the following properties:

    ClusterName, CanonicalName, UsedBy, SizeMB, esx01, esx02....

    By which property do you want to sort ? By UsedBy ?

    Then the 3 lines should be

    ...

    GetLun "Cluster1" | Sort -Property UsedBy | Export-Xls -Path S:\Scripts\Reports\LUN-Report.xls -WorksheetName "Cluster1" 

    GetLun "Cluster2" | Sort -Property UsedBy | Export-Xls -Path S:\Scripts\Reports\LUN-Report.xls -WorksheetName "Cluster2"
    GetLun "Cluster3" | Sort -Property UsedBy | Export-Xls -Path S:\Scripts\Reports\LUN-Report.xls -WorksheetName "Cluster3"



  • 16.  RE: List Cluster, Host and Datastores

    Posted Dec 16, 2012 06:05 PM

    Hi Luc

    Sort -Property UsedBy worked.

    Thanks again!



  • 17.  RE: List Cluster, Host and Datastores

    Posted Dec 15, 2012 10:03 PM

    Luc

    Your the man! Thanks so much for creating the  Beyond Export-Csv: Export-Xls function.



  • 18.  RE: List Cluster, Host and Datastores

    Posted Dec 13, 2012 09:07 PM

    I prefer rvtools (http://www.robware.net/ )

    It's easier than scripting, and it does more than you can imagine..all for FREE



  • 19.  RE: List Cluster, Host and Datastores

    Posted Dec 13, 2012 09:28 PM

    Although I also think that RVTools is a great tool, some people want to know how to program things themself in PowerCLI. And that is what this community is for. I don't think that it makes sense in this community to answer questions with you can also use RVTools.