Public/Add-SwNodeResource.ps1

Function Add-SwNodeResource {
    <#
        .SYNOPSIS
        Add all "Volume Utilization" resources to a node
 
        .DESCRIPTION
        This function scans a node (usually used for new installed one) and add all possible "Volume Utilization" resource to that node.
        You should define to variable:
            1- ServiceProxy object to solarwinds
            2- Target node Id
 
        .EXAMPLE
        PS> Add-SwNodeResource -InfoServiceProxy $swsi -NodeId 1234
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [SolarWinds.InformationService.Contract2.InfoServiceProxy]$InfoServiceProxy,

        [Parameter(Mandatory = $true)]
        [int]$NodeId,

        # Timeout in seconds for wait for 'ReadyForImport' status
        [int]$timeout = 120,

        # Time to wait before next status check
        [int]$timeBetweenChecks = 20
    )

    Begin {
        # It takes a while before job will turn to 'InProgress' status from 'Unknown'
        $ensureJobWasCreatedWait = 20
    }
    Process {

        $sw = [diagnostics.stopwatch]::StartNew()
        $node = Get-SwNode -InfoServiceProxy $InfoServiceProxy -Id $NodeId

        Write-Verbose -Message ("{0} -> Creating schedule list resources job..." -F $node.Name)
        do {
            if ($sw.Elapsed.TotalSeconds -gt $timeout) {
                throw ("Timeout elapsed when crating job. This is probably caused by calling this script with same nodeId. Please wait few minutes or extend timeout.")
            }
            $result = Invoke-SwisVerb -SwisConnection $InfoServiceProxy -EntityName "orion.nodes" -Verb "ScheduleListResources" -Arguments @($NodeId)
            $jobId = $result.'#text'
            Write-Verbose -Message "Created job with guid: $jobId"

            Start-Sleep -Seconds $ensureJobWasCreatedWait
            $status = Invoke-SwisVerb -SwisConnection $InfoServiceProxy -EntityName "orion.nodes" -Verb "GetScheduledListResourcesStatus" -Arguments @($jobId, $NodeId)
            Write-Verbose -Message ("Job status is: " + $status.'#text')
        } while ($status.'#text' -eq "Unknown")

        Write-Verbose -Message ("{0} -> Waiting until job status will be 'ReadyForImport'..." -F $node.Name)
        while ($status.'#text' -ne "ReadyForImport") {
            if ($sw.Elapsed.TotalSeconds -gt $timeout) {
                throw ("Timeout elapsed when waiting for status 'ReadyForImport'")
            }
            Start-Sleep -Seconds $timeBetweenChecks

            $status = Invoke-SwisVerb -SwisConnection $InfoServiceProxy -EntityName "orion.nodes" -Verb "GetScheduledListResourcesStatus" -Arguments @($jobId, $NodeId)
            Write-Verbose -Message ("Job status is: " + $status.'#text')
        }

        Write-Verbose -Message ("{0} -> Importing list resources..." -F $node.Name)
        $importResult = Invoke-SwisVerb -SwisConnection $InfoServiceProxy -EntityName "orion.nodes" -Verb "ImportListResourcesResult" -Arguments @($jobId, $NodeId)

        if (![System.Convert]::ToBoolean($importResult.'#text')) {
            throw ("Import of ListResources result for NodeId:" + $NodeId + " finished with errors.")
        }
    }
    End {
        Write-Verbose -Message "Script finished successfully"
    }
}