5 Replies Latest reply on Apr 17, 2019 12:40 AM by LucD

    Stop New-hardDisk task/command if task taking more time or hung

    thulase Lurker

      I want to a add a new disk to server using powercli. I can able add a disk to server using the below command.

      New-HardDisk -VM $VM -CapacityGB $Disksize -Datastore $Datastore -StorageFormat EagerZeroedThick.

       

      But some time if datastore having any issues. Disk creation is taking more time or it is hung. And the script is not moving to next VM.

      New-Harddisk doesn't have the -RunAsync syntax to go the next command.

      I want to wait some time for this task then cancel the task if it exceed the time limit. for this i wrote a script like below but script is not moving to next command until complete the New-Harddisk

      command.

       

      $vm = "Test11"

      $datastore = "Datastore10"

      [int]$Disksize = 200

      $CurrentTime = Get-Date

      $VmId = (Get-VM -Name $vm).id

       

      $StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch

      $StopWatch.Start()

       

      $I = $null

      do {

      $I++

          If($I -eq 1){            

          $output = New-HardDisk -VM $VM -CapacityGB $Disksize -Datastore $Datastore -StorageFormat EagerZeroedThick

          Write-host " working on disk" -ForegroundColor Green}

          else {}

                                

      $Task = Get-Task | Where-Object {$_.ObjectId -eq $VmId -and $_.Name -eq "ReconfigVM_Task" -and ($_.StartTime -gt $CurrentTime)}

      $TaskState = $Task.State

      $TaskState

      } until ($TaskState -eq "Success" -or $TaskState -eq "Error" -or $StopWatch.Elapsed.Minutes -ge 1) #Minutes Seconds

       

      $StopWatch.Stop()

       

      if($TaskState -eq "Success")

      {

      Write-host " command success"

      $output}

      elseif($TaskState -eq "Error")

      {

      Write-host "command Error"}

       

      else{

      try{Stop-Task -Task $Task -Confirm:$false -ErrorAction Ignore} catch {}

      Write-host "task terminated"}

       

      Write-host "working on second server"

        • 1. Re: Stop New-hardDisk task/command if task taking more time or hung
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          I suspect the  problem might come from the comparison of $CurrentTime with the task-s StartTime.
          That will never be exactly the same, so the test will always return $false (and the Do-loop continues).

           

          Leave that comparison out.

          Or test that CurrentTime and StartTime are for example maximum 10 seconds apart.

          • 2. Re: Stop New-hardDisk task/command if task taking more time or hung
            thulase Lurker

            LucD thanks for response

            Main problem is here.

            I want to initiate the New-HardDisk command and go to next line, without completing the disk addition task

             

            Disk addition and Get-task in Do loop. But loop is stopping at disk addition, after completing the disk addition task only command goining to next line. Here I don’t want to wait till complete the disk addition task.

            • 3. Re: Stop New-hardDisk task/command if task taking more time or hung
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              Ok, got it.

              Since, as you noticed, there is no RunAsync option for New-HardDisk, you can launch that part in a background job with Start-Job.

              Your script just continues after the Start-Job.

              • 4. Re: Stop New-hardDisk task/command if task taking more time or hung
                thulase Lurker

                yes I need same kind of RunAsync option for New-HardDisk.

                I have tried with start-Job command it is not working for Powercli command in power shell application.

                I tried to get more information on Start-job, I have found below is the problem.

                Start-Job initiates a new Powershell session which doesn’t have the PowerCLI module loaded and of course is not connected to any vCenter. And it is not taking the variables which created before start-job command.

                I fell start-job is only option for me. can you help me how to use the start job here.

                I read the below article but it is not working from me 

                Easily run PowerCLI commands as jobs 

                 

                $j = Start-Job -ScriptBlock {Stop-VM (get-vm test101)}

                $jobid = $j.id

                $Jstatus =$j.State

                 

                sleep 30

                If($j.State -ne "Completed"){

                Stop-Job -Id $jobid}

                else{}

                 

                From the article i have used the Function Start-PowerCLIJob with below command it is also not working. ($DefaultVIServer is showing the connected VC)

                 

                Start-PowerCLIJob -DefaultVIServer $DefaultVIServer -JobName "stopVM" -Module "VMware.VimAutomation.Core","VMware.DeployAutomation" -ScriptBlock {Start-VM (get-vm test101)}

                Get-Job "stopVM"

                • 5. Re: Stop New-hardDisk task/command if task taking more time or hung
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  There are a couple of things to watch out for when using PowerCLI in background jobs.

                  • you have to connect to a vSphere server
                  • there should not be any interactivity from the job
                  • you have to pass data to the background job via the ArgumentList
                  • you have to define in the background job which parameters you receive from the calling code

                   

                  The following is a basic script run as a background job.

                  Before running the script, make sure you are connected.

                  The module autoload feature will make sure that the PowerCLI cmdlets are found (without the need to do Import-Module).

                   

                   

                  $code = {

                     param(

                     [string]$Server,

                     [string]$SessionId,

                     [string]$VMName

                     )


                     Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null

                     Connect-VIServer -Server $Server -Session $SessionId

                     Get-VM -Name $VMName

                  }


                  $vmName = 'MyVM'

                  $sJob = @{

                     ScriptBlock = $code

                     ArgumentList = $global:DefaultVIServer.Name,$global:DefaultVIServer.SessionId,$vmName

                  }

                  $j = Start-Job @sJob


                  Receive-Job -Job $j -Wait