Announcement

Collapse
No announcement yet.

Vmware powercli question - how to Interrogate VMs in sub folders locations

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Vmware powercli question - how to Interrogate VMs in sub folders locations

    Hi Admins
    Any VMware Power Cli experts out there?

    I have got a question regarding a PowerShell script which was developed by one of our previous VM admins who was a bit of script guru who managed to create a script which we use as part of disaster recovery process.
    Unfortunately, my knowledge with PowerShell scripts is limited and I could do with some help.
    In summary we use this script as part of our DR recovery process to clone our volumes from our NetApp array using the NetApp flexclone method to bring our VMs online within minutes however the performance of that array is poor so we want to utilize another array which has higher performing disks. We use this PowerShell script to effectively interrogate a particular VM folder and it will literally process any VMs located on a particular storage which starts with the name 'snap' and if it identifies this, it will move to another storage array in a sequential order.

    As part of one of the variables defined we use $VM_LOCATION so the script knows which folder to look in to interrogate but ideally I need to amend this slightly so that it can interrogate sub folders within that main folder.

    I attach the script in txt format.

    Is this possible? Is anyone able to help me?
    I have tried to look for a variable which would meet this need but I cant seem to find one.
    Any help on this would be most appreciated.

    Many Thanks
    Attached Files

  • #2
    I personally don't like opening attached files so I have used my Sandbox VM to open the text file and the Powershell script from ranjb's post is now here using the Wrap Code tool. Nothing in the script has been altered in any way.

    Code:
    ######################
    # Storage vMotions VM files from one datastore to another, ie. from NetApp to NexSAN
    ######################
    
    ## Must have VMWare PowerCLI 6+ installed
    Add-PSSnapin "VMware.VimAutomation.Core"
    
    
    ######################
    # Function block     #
    ######################
    function cenWriteLog ($Message, $NewFile)
    {
        $DateTimeStamp =  Get-Date  -Format "yyyy-MM-dd HH:mm:ss"
        $DateTimeStamp = "** $DateTimeStamp  **"
        if ($NewFile)
        {
            $DateTimeStamp | out-file "log.txt"
        }
        else
        {
          $DateTimeStamp | out-file "log.txt" -Append 
        }
    
        $Message | out-file "log.txt" -Append
        $DateTimeStamp
        $Message
    } #Endfunction cenWriteLog
    ######################
    # Function block end #
    ######################
    
    
    ######################
    # Main script        #
    ######################
    #Disaster Recovery control variables
    $VC_Server = "Target vCentre server"
    $VM_LOCATION = 'Example folder name ' #VC folder to look in
    $DS_SOURCE_FILTER ="Snap*" #source DataStore location, use wild cards to scan more than one datastore
    $DS_TARGET_FILTER_PRIMARY = '*nexsan*sas*' #Primary target DataStore, accepts wildcards
    $DS_TARGET_FILTER_SECONDARY = '*nexsan*sata*' #Secondary target DataStor, accepts wildcards
    
    #Process control variables
    $TASK_MAX = 1 #Maximum number of VC tasks that can execute, due to the preformance of the storage subsystem, max recommended value is 3
    $TASK_WAIT = 1 #Delay between retrys in minutes
    $VM_MOVE_ASYNC = $true #$true for async, $false for sync.  To force one svmotion at a time, set this to $false
    $DS_BUFFER = 200 #Datastore space (GB) to remain free after SVMotion
    
    
    CLS
    cenWriteLog -Message "Scripted SVMotion started" -NewFile $true
    
    #Connect to VCenter
    $ResultText = Connect-viserver -Server $VC_Server
    cenWriteLog -Message $ResultText
    
    
    $FoundVMs  = Get-VM -Datastore $DS_SOURCE_FILTER |WHERE {($_.Folder -like $VM_LOCATION )} |SELECT Name, UsedSpaceGB
    cenWriteLog -Message $FoundVMs
    
    foreach($VM in $FoundVMs)
    {
        #record VM details
        $VMName = $VM.Name
        $RequiredSpace = $VM.UsedSpaceGB + $DS_BUFFER
    
        [boolean] $VM_Relocate = $false #Flag to monitor the status of the operation
    
        #Get primary datastores
        $DataStore = Get-Datastore -Name $DS_TARGET_FILTER_PRIMARY |WHERE {$_.FreeSpaceGB -ge $RequiredSpace} |SELECT Name | SELECT -First 1
    
        #if a suitable datastore has been found
        if($DataStore)
        {
    
            $TargetDSName  = $DataStore.Name  
            $VM_Relocate = $true
    
        } #if
        else
        {
            #Get secondary datastores
            $DataStore = Get-Datastore -Name $DS_TARGET_FILTER_SECONDARY |WHERE {$_.FreeSpaceGB -ge $RequiredSpace} |SELECT Name | SELECT -First 1
    
            if($DataStore)
            { 
                $TargetDSName  = $DataStore.Name   
                $VM_Relocate = $true
    
             } #if
    
        }
        if ($VM_Relocate)
        {
    
            $SleepCOunt = 0
            do
            {
    
                #Increase the sleep time to prevent querying the VC server too often
                $SleepCount++
    
                #Get current task stats
                $Task_Queued = Get-Task -Status:Queued
                $Task_Running = Get-Task -Status:Running
                $TaskCount = 0
    
                #Count the number of tasks currently running, sleep for a few seconds/minutes if applicable in order to reduce load on the server
                if ($Task_Queued) {$TaskCount = $Task_Queued.Count}
                if ($Task_Running){$TaskCount += $Task_Running.Count}
                if ($TaskCount -ge $TASK_MAX)
                {
                    $SleepTime = ($TASK_WAIT * $SleepCount)
                    cenWriteLog -Message "Sleeping for $SleepTime minutes, task count $TaskCount"
                    Start-Sleep -Seconds ($SleepTime * 60)
                }
            } while ($TaskCount -ge $TASK_MAX)
    
            cenWriteLog -Message "Moving $VMName"
            Move-VM -VM $VMName -Datastore $TargetDSName -RunAsync:$VM_MOVE_ASYNC
        }
        else
        {
            cenWriteLog -Message "Unable to move $VMName"
        }
    
    
    } #for..each
    
    Disconnect-VIServer -Server "Target vCentre Server" -Confirm:$false
    ######################
    # Main script end   #
    ######################
    1 1 was a racehorse.
    2 2 was 1 2.
    1 1 1 1 race 1 day,
    2 2 1 1 2

    Comment


    • #3
      Thanks for doing that biggles77

      Comment


      • #4
        This doesn't do cloning. All its doing is a storage vmotion from one san to another. Thats not DR.

        Comment

        Working...
        X