1 2 3 Previous Next 33 Replies Latest reply on Nov 11, 2019 10:30 PM by LucD Go to original post
      • 30. Re: Disk consolidation script -- Need help testing
        LucD Guru
        Community WarriorsvExpertUser Moderators

        You need to use the Count property of you want to test like that

         

        $vCenters = $vCenters = @('10.x.x.78')

        $logpath = "$env:D\vmk\VM_Consolidation.log"


        ############

        <#

        .SYNOPSIS

        Logs messages to a file

        .DESCRIPTION

        Receives messages and logs them to an output file.

        .PARAMETER msg

        The message to be written

        .EXAMPLE

        Write-Log 'Write this to the logfile'

        .NOTES

        Requires $logfile to be configured as the path to the output log file

        #>


        function Write-Log {

          param($msg)


          "$(Get-Date -Format G) : $msg" | Out-File -FilePath $logpath -Append -Force

        }


        foreach ($vCenter in $vCenters) {

          try {

           Connect-VIServer $vCenter -ErrorAction Stop

          } catch {

           Write-Log 'Cannot connect to $vCenter'

          }

        }


        if ($Global:DefaultVIServers.Count -gt 0) {

          $VMsNeedingConsolidation = Get-VM | Where-Object {$_.ExtensionData.Runtime.consolidationNeeded}

          if ($VMsNeedingConsolidation.Count -gt 0){

           Write-Log "VMs needing consolidation: $VMsNeedingConsolidation"

        }

        • 31. Re: Disk consolidation script -- Need help testing
          vmk2014 Hot Shot

          Thank you, LucD. It worked this time, but it only  generated output saying that the VM's consolidation needed in VM_consolidation.log file. I'm looking to automate the VM consolidation and i also observed while using the first script that the disk consolidation fails due to lock on file. Can it be mitigated ?

           

          Thanks

          vmk

          • 32. Re: Disk consolidation script -- Need help testing
            vmk2014 Hot Shot

            Hi LucD,

             

            Can the VM disk consolidation be initiated from this script ? Right now, it only recommend the VM disk consolidation.

             

            Thanks

            vmk

            • 33. Re: Disk consolidation script -- Need help testing
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              Sure, call the ConsolidateVMDisks method on the VirtualMachine.

               

              $vm.ExtensionData.ConsolidateVMDisks()

               

              1 2 3 Previous Next