Public/Remove-SwNodeOrphanedVolume.ps1

Function Remove-SwNodeOrphanedVolume {
    <#
        .SYNOPSIS
        Remove Orphaned volume from a node.
 
        .DESCRIPTION
        Orphaned volumes means some volume which was monitored before, but now it is not exist on node anymore.
        In this case this node have to be pruned from node.
 
        Note: Node agent should be connected.
        Note: Node Id of target machine is required, you can execute "Get-Node" command to find node Id.
    #>

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

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

    Begin{
    }

    Process{
        foreach($item in $NodeId){
            Write-Verbose -Message "Geting node information ..."
            $node = Get-SwNode -InfoServiceProxy $InfoServiceProxy -Id $item
            Write-Verbose -Message "Geting agent information ..."
            $agent = Get-SwAgent -InfoServiceProxy $InfoServiceProxy -Name $node.Name
            if ($agent.ConnectionStatus -eq "1") {
                $allvolumes = Get-SWVolume -InfoServiceProxy $InfoServiceProxy -NodeId $node.Id | Where-Object -Property Status -eq 0
                foreach ($volume in $allvolumes){
                    if($PSCmdlet.ShouldProcess( "Verbose Description !", "Remove Resource " + "Caption")){
                        Remove-SwisObject $InfoServiceProxy $volume.Uri
                        Write-Verbose -Message ("{0} -> Volume with caption '{1}' is removed." -F $node.Name, $volume.Name)
                    }
                }
            }
            else{
                Write-Verbose -Message ("{0} -> Agent status is not connected." -F $node.Name)
            }
        }
    }

    End{
    }
}