PoshCode Archive  Artifact [43e6ce782b]

Artifact 43e6ce782b0b6765d8ea9a3b062c7f9c5a1163e3b50617883e6702e6ed4eba41:

  • File Get-SRVPriority.ps1 — part of check-in [e0a55c2c6f] at 2018-06-10 12:56:29 on branch trunk — Returns the priority SRV hostname and port for a particular service and domain. Requires NetCmdlets (get-dns). (user: Lance Robinson size: 1235)

# encoding: ascii
# api: powershell
# title: Get-SRVPriority
# description: Returns the priority SRV hostname and port for a particular service and domain.  Requires NetCmdlets (get-dns).
# version: 0.1
# type: function
# author: Lance Robinson
# license: CC0
# function: Get-SRVPriority
# x-poshcode-id: 1125
# x-archived: 2009-05-27T16:32:45
#
#
#Returns the priority SRV hostname and port for a particular service and domain.
function Get-SRVPriority {
param( [string] $query = $( Throw "Query required in the format _Service._Proto.DomainName (ie, `"_ftp._tcp.myserver.net`" or `"_xmpp-client._tcp.gmail.com`").") )
	
  #get all the SRV records for this service/domain, sorted by descending priority (lower priority = preferred server)
  $srvrecords = @(get-dns $query -type SRV | sort-object -Property PRIORITY -des)
  #verify that there are some records
  if ($srvrecords.Length -eq 0) { Throw "No records found." }
  #now gather all the records that are of the lowest priority, and sort them by weight (higher weight = preferred server)
  $srvrecords = @($srvrecords | ?{$_.PRIORITY -eq $srvrecords[0].PRIORITY} | sort -Property WEIGHT)
  #got it:
  "$($srvrecords[0].TARGET):$($srvrecords[0].PORT)"
}