none
Ignoring a timeout during a loop in my script RRS feed

  • Question

  • Hi all,

    Wonder if anyone is able to help out, I've put together a script which loops through a set of datastores and carries out an unmap process on each datastore - I've added a rest period at the end of each loop (2 hours) to allow the job to complete in the background before starting the next loop through.

    Basically, the issue is that each time the unmap takes place (VMWare ESX hosts) the end point times out in the powershell window, even though the command was received and I know the unmap process has kicked off. Sometimes this results in...

    The operation has timed out
    At line:1 char:1

    Other times it will just hang and proceed no further. So my question is, knowing that this timeout issue will occur, how can I a) ignore it and b) continue with the loop on each datastore. My code for the 'foreach' process is below (without the rest of the script/variables etc):

    ForEach ($DataStore in $DataStores)
    {
    	$EsxHost = Get-VMHost -Id ($DataStore.ExtensionData.Host | Get-Random).Key
    	
    	$EsxCli = Get-EsxCli -VMHost $EsxHost
    
    #####################################
    #Initiate UNMAPs with rest inbetween# 
    #####################################
    	
        $EsxCli.storage.vmfs.unmap($blocks,"$DataStore", $null)
        
        Start-Sleep -s $Rest


    Any pointers/suggestions would be great.

    Cheers

    Sam


    Monday, November 27, 2017 7:56 PM

Answers

  • You can create a function for including Erroraction parameter for unmapping Datastore.

    function Unmap-Datastore
    {
      [CmdletBinding()]
      Param( )
    process{    $EsxHost = Get-VMHost -Id ($DataStore.ExtensionData.Host | Get-Random).Key
    	
    	$EsxCli = Get-EsxCli -VMHost $EsxHost
    
            #####################################
            #Initiate UNMAPs with rest inbetween# 
            #####################################
    	
        $EsxCli.storage.vmfs.unmap($blocks,"$DataStore", $null)   
    
    }
    }
    
    ForEach ($DataStore in $DataStores)
    {  
       unmap-datastore -erroraction silentlycontinue
       
       start-sleep -s $rest 
    
    }


    • Marked as answer by _Samuman_ Thursday, November 30, 2017 1:38 PM
    Monday, November 27, 2017 10:13 PM

All replies

  • You fail to say where it is hanging.

    Don't place quotes around variables to avoid issues.

    Post issues with EsxCli to vSphere forums.


    \_(ツ)_/

    Monday, November 27, 2017 8:43 PM
  • You can create a function for including Erroraction parameter for unmapping Datastore.

    function Unmap-Datastore
    {
      [CmdletBinding()]
      Param( )
    process{    $EsxHost = Get-VMHost -Id ($DataStore.ExtensionData.Host | Get-Random).Key
    	
    	$EsxCli = Get-EsxCli -VMHost $EsxHost
    
            #####################################
            #Initiate UNMAPs with rest inbetween# 
            #####################################
    	
        $EsxCli.storage.vmfs.unmap($blocks,"$DataStore", $null)   
    
    }
    }
    
    ForEach ($DataStore in $DataStores)
    {  
       unmap-datastore -erroraction silentlycontinue
       
       start-sleep -s $rest 
    
    }


    • Marked as answer by _Samuman_ Thursday, November 30, 2017 1:38 PM
    Monday, November 27, 2017 10:13 PM
  • Hi jrv,

    I wasn't asking a Esx/vSphere question, I was asking a PowerShell question.

    The 'timeout' isn't the issue nor is it something I'm asking about here - I can anticipate the timeout happening which is why I wanted to build something into the script to deal with it.

    It hangs after here, before the loop finishes:

    $EsxCli.storage.vmfs.unmap($blocks,"$DataStore", $null)
    Tuesday, November 28, 2017 9:18 AM
  • Thanks asharma5, this sounds like a good idea.

    I'll try and feedback.

    Thanks,

    Sam

    Tuesday, November 28, 2017 9:31 AM
  • If an API call is hanging there is nothing you can do about it in PowerShell.  If there is a timeout then you need to detect it and report it.  PowerShell cannot do this for external programs except by checking the exit code of the process or by detecting the returned text.


    \_(ツ)_/

    Tuesday, November 28, 2017 9:47 AM
  • Thanks asharma5

    I implemented error actions and moved my script around accordingly - seemed to do the job :)

    Many thanks again,

    Sam

    Thursday, November 30, 2017 1:39 PM