VMware Cloud Community
fajarpri
Enthusiast
Enthusiast
Jump to solution

To list VM, cluster, hosts, folders, vmdk file path

Hi all,

Does anyone have a working script for the above?

I'm on 4.1

Thanks in advance Smiley Happy

Reply
0 Kudos
1 Solution

Accepted Solutions
julienvarela
Commander
Commander
Jump to solution

Hi,

You can try this :

$AuditVM = @()

ForEach ($vm in (Get-Cluster | Get-VM )) {

    foreach ($vmguest in @($vm | Get-VMguest)) {

            foreach ($cluster in @($vm | Get-Cluster )) {

                foreach ($datastore in @(Get-VM $vm | Get-Datastore)) {

                    foreach ($Network in @(Get-VM $vm | Get-NetworkAdapter)) {

    $objGuest= "" | Select State, Name,CPU, Memory, IPAddress, OSFullName, Cluster, Folder, UsedspaceGB, Datastore, Description, MacAddress, Path, NetworkName, NetworkAdapter

    $objGuest.State= $vmguest.state

    $objGuest.CPU = $vm.NumCPU

    $objGuest.Memory = $vm.MemoryMB

    $objGuest.Description= $vm.Notes

    $objGuest.Folder=$vm.Folder

    $objGuest.Name = $vm.Name

    $objGuest.IPAddress = [string]::Join(',',$vmguest.IPAddress)

    $objGuest.OSFullName = $vmguest.OSFullName

    $objGuest.UsedSpaceGb = $vm.UsedSpaceGB

    $objGuest.Cluster = $cluster.Name

    $objGuest.Datastore = $datastore.Name

    $objGuest.MacAddress = $Network.MacAddress

    $objGuest.Path = $vm.Extensiondata.Config.Files.VmPathName

    $objGuest.NetworkName = $Network.NetworkName

    $objGuest.NetworkAdapter = $Network.Name

    $AuditVM += $objGuest

    }

    }

    }

    }

}

$AuditVM

Regards,

Julien

Regards, J.Varela http://vthink.fr

View solution in original post

Reply
0 Kudos
10 Replies
julienvarela
Commander
Commander
Jump to solution

Hi,

You can try this :

$AuditVM = @()

ForEach ($vm in (Get-Cluster | Get-VM )) {

    foreach ($vmguest in @($vm | Get-VMguest)) {

            foreach ($cluster in @($vm | Get-Cluster )) {

                foreach ($datastore in @(Get-VM $vm | Get-Datastore)) {

                    foreach ($Network in @(Get-VM $vm | Get-NetworkAdapter)) {

    $objGuest= "" | Select State, Name,CPU, Memory, IPAddress, OSFullName, Cluster, Folder, UsedspaceGB, Datastore, Description, MacAddress, Path, NetworkName, NetworkAdapter

    $objGuest.State= $vmguest.state

    $objGuest.CPU = $vm.NumCPU

    $objGuest.Memory = $vm.MemoryMB

    $objGuest.Description= $vm.Notes

    $objGuest.Folder=$vm.Folder

    $objGuest.Name = $vm.Name

    $objGuest.IPAddress = [string]::Join(',',$vmguest.IPAddress)

    $objGuest.OSFullName = $vmguest.OSFullName

    $objGuest.UsedSpaceGb = $vm.UsedSpaceGB

    $objGuest.Cluster = $cluster.Name

    $objGuest.Datastore = $datastore.Name

    $objGuest.MacAddress = $Network.MacAddress

    $objGuest.Path = $vm.Extensiondata.Config.Files.VmPathName

    $objGuest.NetworkName = $Network.NetworkName

    $objGuest.NetworkAdapter = $Network.Name

    $AuditVM += $objGuest

    }

    }

    }

    }

}

$AuditVM

Regards,

Julien

Regards, J.Varela http://vthink.fr
Reply
0 Kudos
fajarpri
Enthusiast
Enthusiast
Jump to solution

Thank you Julien it works Smiley Happy

However, for the Folder, it doesn't show the folder structure, only the Folder the VM located.

Is it possible to show the Folder structure too, e.g. IT Infra > Fileserver > Accounting

Reply
0 Kudos
julienvarela
Commander
Commander
Jump to solution

Hi,

I just integrated the folderpath function that LucD has created by the past and it should work.

function Get-FolderPath{

<#

.SYNOPSIS

    Returns the folderpath for a folder

.DESCRIPTION

    The function will return the complete folderpath for

    a given folder, optionally with the "hidden" folders

    included. The function also indicats if it is a "blue"

    or "yellow" folder.

.NOTES

    Authors:    Luc Dekens

.PARAMETER Folder

    On or more folders

.PARAMETER ShowHidden

    Switch to specify if "hidden" folders should be included

    in the returned path. The default is $false.

.EXAMPLE

    PS> Get-FolderPath -Folder (Get-Folder -Name "MyFolder")

.EXAMPLE

    PS> Get-Folder | Get-FolderPath -ShowHidden:$true

#>

    param(

    [parameter(valuefrompipeline = $true,

    position = 0,

    HelpMessage = "Enter a folder")]

    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,

    [switch]$ShowHidden = $false

    )

    begin{

        $excludedNames = "Datacenters","vm","host"

    }

    process{

        $Folder | %{

            $fld = $_.Extensiondata

            $fldType = "yellow"

            if($fld.ChildType -contains "VirtualMachine"){

                $fldType = "blue"

            }

            $path = $fld.Name

            while($fld.Parent){

                $fld = Get-View $fld.Parent

                if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){

                    $path = $fld.Name + "\" + $path

                }

            }

            $row = "" | Select Path

            $row.Path = $path

            $row

        }

    }

}

$AuditVM = @()

ForEach ($vm in (Get-Cluster Maquette_DEV | Get-VM )) {

    foreach ($vmguest in @($vm | Get-VMguest)) {

            foreach ($cluster in @($vm | Get-Cluster )) {

                foreach ($datastore in @(Get-VM $vm | Get-Datastore)) {

                    foreach ($Network in @(Get-VM $vm | Get-NetworkAdapter)) {

    $objGuest= "" | Select State, Name,CPU, Memory, IPAddress, OSFullName, Cluster, Folder, UsedspaceGB, Datastore, Description, MacAddress, Path, NetworkName, NetworkAdapter

    $objGuest.State= $vmguest.state

    $objGuest.CPU = $vm.NumCPU

    $objGuest.Memory = $vm.MemoryMB

    $objGuest.Description= $vm.Notes

    $objGuest.Folder= Get-folder $vm.Folder | Get-folderpath

    $objGuest.Name = $vm.Name

    $objGuest.IPAddress = [string]::Join(',',$vmguest.IPAddress)

    $objGuest.OSFullName = $vmguest.OSFullName

    $objGuest.UsedSpaceGb = $vm.UsedSpaceGB

    $objGuest.Cluster = $cluster.Name

    $objGuest.Datastore = $datastore.Name

    $objGuest.MacAddress = $Network.MacAddress

    $objGuest.Path = $vm.Extensiondata.Config.Files.VmPathName

    $objGuest.NetworkName = $Network.NetworkName

    $objGuest.NetworkAdapter = $Network.Name

    $AuditVM += $objGuest

    }

    }

    }

    }

}

$AuditVM

Regards,

Julien

Regards, J.Varela http://vthink.fr
fajarpri
Enthusiast
Enthusiast
Jump to solution

Wow cool!
Thank you very much Julien! Smiley Happy

Reply
0 Kudos
CarlosDionizio
Contributor
Contributor
Jump to solution

Hi for all!

This work with version 5.0 ... ?

Tks

Hi,

I just integrated the folderpath function that LucD has created by the past and it should work.

function Get-FolderPath{

<#

.SYNOPSIS

    Returns the folderpath for a folder

.DESCRIPTION

    The function will return the complete folderpath for

    a given folder, optionally with the "hidden" folders

    included. The function also indicats if it is a "blue"

    or "yellow" folder.

.NOTES

    Authors:    Luc Dekens

.PARAMETER Folder

    On or more folders

.PARAMETER ShowHidden

    Switch to specify if "hidden" folders should be included

    in the returned path. The default is $false.

.EXAMPLE

    PS> Get-FolderPath -Folder (Get-Folder -Name "MyFolder")

.EXAMPLE

    PS> Get-Folder | Get-FolderPath -ShowHidden:$true

#>

    param(

    [parameter(valuefrompipeline = $true,

    position = 0,

    HelpMessage = "Enter a folder")]

    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,

    [switch]$ShowHidden = $false

    )

    begin{

        $excludedNames = "Datacenters","vm","host"

    }

    process{

        $Folder | %{

            $fld = $_.Extensiondata

            $fldType = "yellow"

            if($fld.ChildType -contains "VirtualMachine"){

                $fldType = "blue"

            }

            $path = $fld.Name

            while($fld.Parent){

                $fld = Get-View $fld.Parent

                if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){

                    $path = $fld.Name + "\" + $path

                }

            }

            $row = "" | Select Path

            $row.Path = $path

            $row

        }

    }

}

$AuditVM = @()

ForEach ($vm in (Get-Cluster Maquette_DEV | Get-VM )) {

    foreach ($vmguest in @($vm | Get-VMguest)) {

            foreach ($cluster in @($vm | Get-Cluster )) {

                foreach ($datastore in @(Get-VM $vm | Get-Datastore)) {

                    foreach ($Network in @(Get-VM $vm | Get-NetworkAdapter)) {

    $objGuest= "" | Select State, Name,CPU, Memory, IPAddress, OSFullName, Cluster, Folder, UsedspaceGB, Datastore, Description, MacAddress, Path, NetworkName, NetworkAdapter

    $objGuest.State= $vmguest.state

    $objGuest.CPU = $vm.NumCPU

    $objGuest.Memory = $vm.MemoryMB

    $objGuest.Description= $vm.Notes

    $objGuest.Folder= Get-folder $vm.Folder | Get-folderpath

    $objGuest.Name = $vm.Name

    $objGuest.IPAddress = [string]::Join(',',$vmguest.IPAddress)

    $objGuest.OSFullName = $vmguest.OSFullName

    $objGuest.UsedSpaceGb = $vm.UsedSpaceGB

    $objGuest.Cluster = $cluster.Name

    $objGuest.Datastore = $datastore.Name

    $objGuest.MacAddress = $Network.MacAddress

    $objGuest.Path = $vm.Extensiondata.Config.Files.VmPathName

    $objGuest.NetworkName = $Network.NetworkName

    $objGuest.NetworkAdapter = $Network.Name

    $AuditVM += $objGuest

    }

    }

    }

    }

}

$AuditVM

Reply
0 Kudos
julienvarela
Commander
Commander
Jump to solution

Hi ,

Yes its work with vsphere 5.

regards,

Julien

Regards, J.Varela http://vthink.fr
Reply
0 Kudos
CarlosDionizio
Contributor
Contributor
Jump to solution

Tks Julie!

ITs only .\script.ps1 to execute?

Sorry i m newbie in PowerCLI and powershell!

Reply
0 Kudos
julienvarela
Commander
Commander
Jump to solution

Yes , but first you need to start powercli and start a connection to your VC.

Regards,

julien

Regards, J.Varela http://vthink.fr
Reply
0 Kudos
cmatheson
Contributor
Contributor
Jump to solution

Sorry to bump an old thread but would it be possible to sort the results of this by the Folder?

We have different clients organized into folders I'd like to sort the result by the folder name if possible

I've tried a few different ways but I am still fairly new to power-cli and powershell.

Reply
0 Kudos
vin01
Expert
Expert
Jump to solution

Hi Carlos

Many Thanks for the script..Along with this output i need few more info .. like "Datacenter Name",Harddisk wise data with nna or along datastore with nna. and Lun ID.

I'm attaching a script which i found in communities It is given by LucD.It may be help full with the current script.

Regards Vineeth.K
Reply
0 Kudos