Affichage des articles dont le libellé est path. Afficher tous les articles
Affichage des articles dont le libellé est path. Afficher tous les articles

vendredi 11 septembre 2015

Retrieving a VM Snapshot path from vCenter using Powershell

Sometime, retrieving the path of a VMware VM's Snapshot can prove useful.

As we've had to automate the deployment of some VMware Horizon Pools using Powershell, the cmdlet Add-AutomaticLinkedCLonePool was asking for a Snapshot path as an input with the "ParentSnapshotPath" parameter.

So here's the function that answered this need!

#Requires -Version 3.0


Function Get-VMSnapshotPath {

<#

    .SYNOPSIS

            This function will return a VM's Snapshot path

    .DESCRIPTION

            This function will return a VM's Snapshot path

    .PARAMETER  Snapshot

            Specifies a VM Snapshot

    .EXAMPLE

            Get-Snapshot -VM (Get-VM "Master") -Name "BaseSnapshot" | Get-VMSnapshotPath


            /BaseSnapshot

    .EXAMPLE

            Get-Snapshot -VM (Get-VM "Master") -Name "SecondSnapshot" | Get-VMSnapshotPath


            /BaseSnapshot/SecondSnapshot

    .EXAMPLE

            (Get-VM "Master"), (Get-VM "MasterVDI") | Get-Snapshot | Get-VMSnapshotPath


            /BaseSnapshot

            /BaseSnapshot/SecondSnapshot

            /VDIBaseSnapshot

    .NOTES

            NAME:     Get-VMLogicalPath

            AUTHOR:   ROULEAU Benjamin

            LASTEDIT: 2015-02-05

    .LINKS

            http://katalykt.blogspot.fr/

#>

    [CmdletBinding()]

    PARAM(

        [Parameter(

            Mandatory,

            ValueFromPipeline,

            ValueFromPipelineByPropertyName)]

        [VMware.VimAutomation.ViCore.Impl.V1.VM.SnapshotImpl]$Snapshot

    )


    BEGIN {

        

    }


    PROCESS {

        $Snapshot | ForEach-Object {

            Write-Verbose -Message ("[PROCESS - Get-VMSnapshotPath] Retrieving the path of Snapshot: '{0}' on VM : '{1}'" -f $_.Name, $_.VM.Name)


            # We retrieve the Snapshot path up the the root of the VM

            $VMo = $_

            $Path = ""

            WHILE ($VMo) {

                $Path = "/" + $VMo.Name + $Path

                $VMo = $VMo.ParentSnapshot

            }


            $Path

        }

    }


    END {


    }

}

mercredi 10 juin 2015

Retrieving a VM's Logical Path from vCenter using Powershell

In some cases, knowing a Virtual Machine’s Logical Path can be quite useful when the need to perform automated tasks in VMware arise.

One of our objectives during our last VMware Horizon View deployment was to automate the creation of multiple Automatic Linked Clone pools. As a matter of fact, the Cmdlet linked to the pool’s creation (Add-AutomaticLinkedCLonePool) has two parameters which expect both the VM’s Full Path and the path to its Parent Folder.

The PowerShell Function below expects a VM as a parameter and returns the VM’s name, its Full Path and its Parent Folder’s path as shown in the synopsis. Just what we needed for our automation process!

#requires -version 2


Function Get-VMLogicalPath {

<#

    .SYNOPSIS

            This function will return a VM's logical path within vCenter

    .DESCRIPTION

            This function will return a VM's logical path within vCenter with or without the VM's name

    .PARAMETER  VM

            Specifies a vCenter VM

    .EXAMPLE

            Get-VMLogicalPath -VM (Get-VM -Name "Master")


            Name                 VMPath                                        ParentPath                                          

            ----                 ------                                        ----------                                          

            Master               /KatalyktDC/vm/PoolFolder/Master              /KatalyktDC/vm/PoolFolder  

    .EXAMPLE

            $VMArray | Get-VMLogicalPath


            Name                 VMPath                                        ParentPath                                          

            ----                 ------                                        ----------                                          

            Master               /KatalyktDC/vm/PoolFolder/Master              /KatalyktDC/vm/PoolFolder  

            MasterTraining       /KatalyktDC/vm/PoolFolder/MasterTraining      /KatalyktDC/vm/PoolFolder  

    .NOTES

            NAME:     Get-VMLogicalPath

            AUTHOR:   ROULEAU Benjamin

            LASTEDIT: 2014-02-05

    .LINKS

            http://katalykt.blogspot.fr/

#>

    [CmdletBinding()]

    PARAM(

        [Parameter(

            Mandatory=$true,

            ValueFromPipeline=$true,

            ValueFromPipelineByPropertyName=$true)]

        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]$VM

    )


    BEGIN {

        

    }


    PROCESS {

        $VM | ForEach-Object {

            TRY {

                Write-Verbose -Message ("[PROCESS - Get-VMLogicalPath] Retrieving path for VM {0}" -f $_.Name)


                # We keep retrieving the parent's folder up to the Datacenter root

                $VMo = $_.Folder

                $Path = ""

                WHILE ($VMo) {

                    $Path = "/" + $VMo.Name + $Path

                    $VMo = $VMo.Parent

                }


                # Compose the VM Path

                $VMPath = $Path + "/" + $_.Name

                

                Write-Verbose -Message ("[PROCESS - Get-VMLogicalPath] Retrieved path {0} for VM {1}" -f $VMPath, $_.Name)


                # Return the desired properties in an object

                New-Object -TypeName PSObject -Property @{

                    Name = $_.Name

                    ParentPath = $Path

                    VMPath = $VMPath

                }

                

            } CATCH {

                Write-Warning -Message ("[PROCESS - Get-VMLogicalPath] Something went wrong: {0}" -f $Error[0].Exception.Message)

            }

        }

    }


    END {


    }

}