Public/Get-SwNode.ps1

Function Get-SwNode {
    <#
        .SYNOPSIS
        Return Node from solarwinds database
 
        .DESCRIPTION
        This function gets an InfoServiceProxy and a name or ID related to a Node. after that can retrive usefull information in object format.
        Note: Name and Id could not be used at the same time.
        If you use name, function will find all Node which can match this name partially.
        If you do not define name or Id, Function will return all Node in database
 
        .EXAMPLE
        PS> Get-SwNode -InfoServiceProxy $swsi -Name "TestSrv-01"
 
        Id : 1234
        Caption : TestSrv-01
        Name : TestSrv-01
        DNSName : TestSrv-01.Contoso.com
        IsServer : True
        Type : Agent
        Vendor : Windows
        IP : 192.168.10.11
        MachineType : Windows 2022 Server
        Location : Site-A
        Status : 1
        ChildStatus : 1
        StatusMessage : Node status is Up.
        EngineId : 1
        LastSync : 5/18/2024 10:35:23 AM
        Uri : swis://MainPoller.Contoso.com/Orion/Orion.Nodes/NodeID=1234
 
        .EXAMPLE
        PS> Get-SwEngine -InfoServiceProxy $swsi -Id 1234
 
        Id : 1234
        Caption : TestSrv-01
        Name : TestSrv-01
        DNSName : TestSrv-01.Contoso.com
        IsServer : True
        Type : Agent
        Vendor : Windows
        IP : 192.168.10.11
        MachineType : Windows 2022 Server
        Location : Site-A
        Status : 1
        ChildStatus : 1
        StatusMessage : Node status is Up.
        EngineId : 1
        LastSync : 5/18/2024 10:35:23 AM
        Uri : swis://MainPoller.Contoso.com/Orion/Orion.Nodes/NodeID=1234
    #>

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

        [string]$Name,

        [int]$Id
    )

    Begin{
        if ($PSBoundParameters.ContainsKey('Name') -and $PSBoundParameters.ContainsKey('Id') ){
            Write-Error("Both Name and Id can not be used at the same time")
            return
        }

        $query = "SELECT NodeID,DNS,Caption,ObjectSubType,IP_Address,Vendor,Uri,`
                    MachineType,Status,ChildStatus,StatusDescription,LastSync, `
                    IsServer,EngineID,Location,NextRediscovery FROM Orion.nodes"


        if ($PSBoundParameters.ContainsKey('Name')){
            $query += " WHERE Caption Like '%$Name%'"
        }
        elseif ($PSBoundParameters.ContainsKey('Id')) {
            $stringId = $Id.ToString()
            $query += " WHERE NodeId = '$stringId'"
        }
    }

    Process{
        Write-Verbose -Message "Fetching query from server ..."
        $result = Get-SwisData -SwisConnection $InfoServiceProxy -Query $query
        if($null -eq $result){
            return $null
        }
        elseif ($result.length -eq 1) {
            return ConvertTo-SwNode -Object $result
        }
        else{
            [Parmis.SolarWinds.Node[]]$output = @()
            foreach ($item in $result ){
                $output += ConvertTo-SwNode -Object $item
            }
            return($output)
        }
    }
    End{
    }
}