PowerCLI/Powershell

Discussion in 'Scripting & Programming' started by ThomasMc, Sep 8, 2011.

  1. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    Afternoon chaps, wondering if someone could confirm if this bit of code would work in PowerCLI

    Code:
    function CreateSnapshot {
    	param([string]$vmname)
    	Get-VM $vmname | New-Snapshot -Name "Before Failover"
    	$task = Get-Task | where {$_.Name -eq "CreateSnapshot_Task"}
    	do {}
    	While ($task -eq "Running")
    }
    
    Particularly the Do While block, what I'm aiming for is that it just waits until the $task is no longer in a running state.

    Thanks :)
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  2. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    Managed to test it and it seems to do what I expected but are still open to ideas if there is a correct way of doing it
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  3. soundian

    soundian Gigabyte Poster

    1,460
    71
    107
    My initial concern would be: How do you tell if the task is doing anything useful, or is in an infinite loop?
    An infinite loop on the task would mean an infinite loop on your do/while.
    You might want to add in some sort of time counter that will throw an error after X minutes.
     
    Last edited: Sep 8, 2011
    Certifications: A+, N+,MCDST,MCTS(680), MCP(270, 271, 272), ITILv3F, CCENT
    WIP: Knuckling down at my new job
    ThomasMc likes this.
  4. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    Good thinking :) I suppose I could pull the task start time and check it in my do block and if it passes x hrs/mins then throw an exception
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  5. ChrisH1979

    ChrisH1979 Byte Poster

    225
    9
    37
    Certifications: MCITP:SA, MCSA, MCTS:Win 7, Application Infrastructure
    WIP: MCITP:EA
    ThomasMc likes this.
  6. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  7. soundian

    soundian Gigabyte Poster

    1,460
    71
    107
    I use a similar system to reboot some of our mainframe print servers. It took me much googling to decide that knowing when a task is doing something useful is indistinguishable from when a task just running.
     
    Certifications: A+, N+,MCDST,MCTS(680), MCP(270, 271, 272), ITILv3F, CCENT
    WIP: Knuckling down at my new job
  8. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    This is what I've got now

    Code:
    function CreateSnapshot {
    	param([string]$vmname)
    	Get-VM $vmname | New-Snapshot -Name "Before Failover"
    	$task = Get-Task | where {$_.Name -eq "CreateSnapshot_Task"}
    	$i = 0
    	do {
    	if ($i -ge 50) {
    	Throw ("Snapshot timeout error")
    	} else {
    	$i++
    	Sleep 3
    	} While ($task -eq "Running")
    }
    
     
    Last edited: Sep 9, 2011
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  9. SimonD
    Honorary Member

    SimonD Terabyte Poster

    3,681
    440
    199
    Or of course you could just ask Mr Renouf to have a look ;)
     
    Certifications: CNA | CNE | CCNA | MCP | MCP+I | MCSE NT4 | MCSA 2003 | Security+ | MCSA:S 2003 | MCSE:S 2003 | MCTS:SCCM 2007 | MCTS:Win 7 | MCITP:EDA7 | MCITP:SA | MCITP:EA | MCTS:Hyper-V | VCP 4 | ITIL v3 Foundation | VCP 5 DCV | VCP 5 Cloud | VCP6 NV | VCP6 DCV | VCAP 5.5 DCA
  10. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    I've emailed the big chap to see what he thinks :D
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.