Traverse.psm1

#Requires -Version 3

function Connect-TraverseBSM {


#.Description
# Connects to a Traverse BSM system with the Web Services API enabled.

#.Parameter Hostname
#The DNS name or IP address of the Traverse BSM system

#.Parameter Credential
#The username and password needed to access the system in secure PSCredential format.

#.Parameter Force
#Create a new session even if one already exists



param (
    [Parameter(Mandatory=$true)][String]$Hostname,
    [Parameter(Mandatory=$true)][PSCredential]$Credential = (get-pscredential -message "Enter your Traverse Username and Password"),
    [Switch]$Force
) # Param

if ($global:TraverseSession -and !$force) {write-warning "You are already logged into Traverse. Use the -force parameter if you want to connect to a different one or use a different username";return} 

#Workaround for bug with new-webserviceproxy (http://www.sqlmusings.com/2012/02/04/resolving-ssrs-and-powershell-new-webserviceproxy-namespace-issue/)
$TraverseBSMLoginWS = (new-webserviceproxy -uri "https://$($hostname)/api/soap/login?wsdl" -ErrorAction stop)
$TraverseBSMLoginNS = $TraverseBSMLoginWS.gettype().namespace

#Create the login request and unpack the password from the encrypted credentials
$loginRequest = new-object ($TraverseBSMLoginNS + '.loginRequest')
$loginRequest.username = $credential.GetNetworkCredential().Username
$loginRequest.password = $credential.GetNetworkCredential().Password

$loginResult = $TraverseBSMLoginWS.login($loginRequest)

if (!$loginResult.success) {throw "The connection failed to $Hostname. Reason: Error $($loginresult.errorcode) $($loginresult.errormessage)"}

set-variable -name TraverseSession -value $loginresult -scope global
set-variable -name TraverseHostname -value $hostname -scope global
write-verbose "Connected to $hostname as $($loginrequest.username)"

} # Connect-TraverseBSM


function Get-TraverseDevice {
#.Description
#Gets all listed Traverse devices.
#TODO: Add SearchCriteria

param (
) # Param

#Exit if not connected
if (!$global:TraverseSession) {write-warning "You are not connected to a Traverse BSM system. Use Connect-TraverseBSM first";return}

#Connect to the Device Web Service
$TraverseBSMDeviceWS = (new-webserviceproxy -uri "https://$($TraverseHostname)/api/soap/device?wsdl" -ErrorAction stop)
$TraverseBSMDeviceNS = $TraverseBSMDeviceWS.gettype().namespace

#Create device request
$DeviceRequest = new-object ($TraverseBSMDeviceNS + '.deviceStatusesRequest')
$DeviceRequest.sessionid = $TraverseSession.result.sessionid

$DeviceResult = $TraverseBSMDeviceWS.getStatuses($DeviceRequest)

if (!$DeviceResult.success) {throw "The connection failed to $TraverseHostname. Reason: Error $($DeviceResult.errorcode) $($DeviceResult.errormessage)"}

return $DeviceResult.result.devices
}