Helpers/Invoke-SCOMTask.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

function Invoke-SCOMTask
{
    [CmdletBinding()]
    [OutputType([Microsoft.EnterpriseManagement.Runtime.TaskResult])]
    param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [System.String]
        $TaskName,

        [Parameter(Mandatory = $true, Position = 1)]
        [Microsoft.EnterpriseManagement.Common.EnterpriseManagementObject[]]
        $Instance,

        [Parameter(Mandatory = $true, Position = 2)]
        [Microsoft.SystemCenter.Core.Connection.Connection]
        $Connection
    )

    $Task = Get-SCOMTask -Name $TaskName -SCSession $Connection -ErrorAction Stop

    $Results = Start-SCOMTask -Task $Task -Instance $Instance -ErrorAction Stop

    foreach ($Result in $Results)
    {
        # Endless while loop to wait for the current task. The loop ends as if
        # the TimeFinished porperty is not null anymore.
        while ($null -eq ($Result = Get-SCOMTaskResult -Id $Result.Id -SCSession $Connection).TimeFinished)
        {
            Start-Sleep -Seconds 1
        }

        # Check if the task has finished successfully. If yes, parse the result
        # as XML and return it. If not, write an custom error to the error
        # stream.
        if ($Result.Status -eq 'Succeeded')
        {
            Write-Output $Result
        }
        else
        {
            $Exception   = New-Object -TypeName 'System.Exception' -ArgumentList ('{0}: {1}' -f $Result.ErrorId, $Result.ErrorMessage)
            $ErrorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList $Exception, '0', 'InvalidOperation', $Result

            $PSCmdlet.ThrowTerminatingError($ErrorRecord)
        }
    }
}