Public/Get-SwAgent.ps1
Function Get-SwAgent { <# .SYNOPSIS Return an agent from solarwinds database .DESCRIPTION This function gets an InfoServiceProxy and a name or ID related to a agent. 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 agents which can match this name partially. If you do not define name or Id, Function will return all agents in database .EXAMPLE PS> Get-SwAgent -InfoServiceProxy $swsi -Name "TestSrv" Id : 1234 Guid : a490ae62-15c0-4653-8da0-bc3106f7cf24 Name : TestSrv-01 PollingEngineId : 2 AgentStatus : 1 AgentStatusMessage : Agent is running ConnectionStatus : 1 ConnectionStatusMessage : Connected Version : 2023.4.1.2030 RegisteredOn : 7/19/2024 3:39:48 PM Node : Cosmos.Solarwinds.Node Id : 4321 Guid : a490ae62-15c0-4653-8da0-bc3106f7cf24 Name : TestSrv-02 PollingEngineId : 2 AgentStatus : 1 AgentStatusMessage : Agent is running ConnectionStatus : 1 ConnectionStatusMessage : Connected Version : 2023.4.1.2030 RegisteredOn : 7/15/2024 4:12:34 PM Node : Cosmos.Solarwinds.Node .EXAMPLE PS> Get-SwAgent -InfoServiceProxy $swsi -Id 1234 Id : 1234 Guid : a490ae62-15c0-4653-8da0-bc3106f7cf24 Name : TestSrv-01 PollingEngineId : 2 AgentStatus : 1 AgentStatusMessage : Agent is running ConnectionStatus : 1 ConnectionStatusMessage : Connected Version : 2023.4.1.2030 RegisteredOn : 7/19/2024 3:39:48 PM Node : Cosmos.Solarwinds.Node #> [CmdletBinding()] [OutputType([System.String])] 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 AgentId,NodeId,HostName,ConnectionStatus,AgentGuid,PollingEngineId,` ConnectionStatusMessage,AgentStatus,AgentStatusMessage, ` AgentVersion,RegisteredOn` FROM Orion.AgentManagement.Agent" if ($PSBoundParameters.ContainsKey('Name')){ $query += " WHERE HostName Like '%$Name%'" } elseif ($PSBoundParameters.ContainsKey('Id')) { $stringId = $Id.ToString() $query += " WHERE AgentId = '$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-SwAgent -Object $result } else{ [Parmis.SolarWinds.Agent[]]$output = @() foreach ($item in $result ){ $output += ConvertTo-SwAgent -Object $item } return($output) } } End{ } } |