JobVariable - How to set static-dynamic value for this variable RRS feed

  • Question

  • Trying to create an automated Checkpoint script to create checkpoints of all running VMs before Patching.

    It would be great if I could loop through all VMs and create a background variable that could be watched via another process.

    In the code below (will be placed in a function), I would like "-JobVariable RemoveCP" to be "-JobVariable Remove-$vmName"

    That way it can delete the previous months checkpoints on all running VMs at the same time. Is that possible using -JobVariable or should I be looking at using Start-Job instead, or something else entirely?

    $checkpointName = "BeforeUpdate"
    # Create ARRAY of all Checkpoints for $vmName
    $Checkpoints = Get-VMCheckpoint -VM "$vmName"
    # Show how many Checkpoints EXIST
    write-host Number in Array: $Checkpoints.Count
    If ($Checkpoints.Count -ge 1)
    # Check IF 1 or more Checkpoints EXIST
          foreach($element in $Checkpoints)
          # LOOP through each Checkpoint in ARRAY $Checkpoints
              # Identify each positional element in the ARRAY
              $elementNumber = $Checkpoints.IndexOf($element)
              if ($Checkpoints[$elementNumber] -match ($checkpointName))
              # CHECK if any Checkpoint MATCHES $checkpointName
                  # Show ARRAY element NUMBER and Checkpoint Name
                  write-host $elementNumber $element
                  # Remove checkpoint in ARRAY MATCHING $checkpointName
                 Remove-VMCheckpoint -VMCheckpoint $Checkpoints[$elementNumber] -RunAsynchronously -JobVariable "RemoveCP"
    #             Remove-VMCheckpoint -VMCheckpoint $Checkpoints[$elementNumber]
    # Display job progress - this is just for testing
    While ($RemoveCP.status -eq "Running") 
       Write-Progress -activity "Removing Checkpoint" -status $RemoveCP.progress
       Start-Sleep -seconds 2 
                } Else {
                  # ERROR: CONTINUE: Current Checkpoint does NOT MATCH $checkpointName for $vmName
                    # That's OK. Move on to the next Checkpoint Element Number for $vmName
                  # DO NOTHING HERE
        } Else {
          # ERROR: CONTINUE: No Checkpoints for $vmName
            # This must be a NEW VM with no checkpoints OR someone removed it
            # Either way, that's OK. Move on the the next $vmName
            write-host No Checkpoints for $vmName



    Wednesday, August 29, 2018 9:41 AM