Skip to content

Instantly share code, notes, and snippets.

@jdmills-edu
Created September 21, 2017 13:47
Show Gist options
  • Save jdmills-edu/6b8492ce34081eb31ed824c69af11dd9 to your computer and use it in GitHub Desktop.
Save jdmills-edu/6b8492ce34081eb31ed824c69af11dd9 to your computer and use it in GitHub Desktop.
A PowerShell script that uses the Dean Evans EMS API to return the list of bookings in a given room at a given time.
param(
[string]$building,
[string]$room,
[string]$time
)
$dateTime = $time | Get-Date
$dateString = $dateTime.ToString("yyyy-MM-ddThh:mm:ss")
#Define API connection parameters
$url = "http://ems.yourdomain.com/emsapi/service.asmx"
$apiuser = "youremsapiuser"
$apipw = "youremsapipassword"
#Define API endpoints
$endpoint_buildings = "/GetBuildings"
$uri_buildings = $url+$endpoint_buildings
$endpoint_rooms = "/GetAllRooms"
$uri_rooms = $url+$endpoint_rooms
$endpoint_bookings = "/GetAllRoomBookings"
$uri_bookings = $url+$endpoint_bookings
#Get Buildings
$query_buildings = "
UserName=$apiuser
Password=$apipw
"
$urlencode_buildings = $query_buildings.Trim() -replace("`r`n","&") -replace(" ")
$buildings_wr = Invoke-RestMethod -Uri $uri_buildings -Method Post -Body $urlencode_buildings
$buildings = ($buildings_wr.string.'#text' | ConvertFrom-Json).data
$thisBuilding = $buildings | where BuildingCode -like "*$building*"
$buildingID = $thisBuilding.ID
$query_rooms = "
UserName=$apiuser
Password=$apipw
BuildingID=$buildingID
"
$urlencode_rooms = $query_rooms.Trim() -replace("`r`n","&") -replace(" ")
$rooms_wr = Invoke-RestMethod -Uri $uri_rooms -Method Post -Body $urlencode_rooms
$rooms = ($rooms_wr.string.'#text' | ConvertFrom-Json).data
$thisRoom = $rooms | Where-Object{$_.Room.split("-")[1] -like "*$room*"}
$roomID = $thisRoom.ID
$query_bookings = "
UserName=$apiuser
Password=$apipw
StartDate=$dateString
EndDate=$dateString
RoomID=$roomID
ViewComboRoomComponents=false
"
#URL encode bookings query.
$urlencode_bookings = $query_bookings.Trim() -replace("`r`n","&") -replace(" ")
$bookings_wr = Invoke-RestMethod -Uri $uri_bookings -Method Post -Body $urlencode_bookings
$bookings = ($bookings_wr.string.'#text' | ConvertFrom-Json).data
$booking = $bookings | Where-Object{$dateTime -gt $_.TimeEventStart -and $dateTime -lt $_.TimeEventEnd}
return $booking
@scotthardwick
Copy link

JD,
Thanks for sharing ;-)
Here is a different take on this using the New-WebServiceProxy cmdlet.

Function Get-BookingAtTime {
# A PowerShell script that uses the Dean Evans EMS API to return the list of bookings in a given room at a given time.
param(
    [string]$room,
    [string]$time
)

# Constants
$allBuildings = -1  # Building Code -1 is ALL buildings
$ViewComboRoomComponents = $false

# Set up the Date Variables
$dateAndTime = $time | Get-Date

## EMS API Connection Variables
$url = "http://ems.yourdomain.com/EMSAPI/Service.asmx"
$username = "youremsapiuser"
$password = "youremsapipassword"

# Set up the EMS Connection
$EmsConnector = New-WebServiceProxy -Uri $url

# Get a Listing of all rooms in all buildings
[xml]$Rooms = $EmsConnector.GetAllRooms($username, $password, $allBuildings)
$RoomArray = $Rooms.Rooms.Data

# Find the specific room ID we are looking for
$thisRoom = $RoomArray | Where-Object {$_.Room -like "*$room*"}
$roomID = $thisRoom.ID

# Get Bookings for this specific room from a specific day
[xml]$BookingArray = $EmsConnector.GetAllRoomBookings($username, $password, $dateAndTime, $dateAndTime, $roomID, $ViewComboRoomComponents)

# Narrow down the list to include only items that match our time parameter
if ($BookingArray.Bookings.Data) {
    $booking = $BookingArray.Bookings.Data | Where-Object { $dateAndTime -ge $([datetime]$_.TimeEventStart) -and $dateAndTime -lt $([datetime]$_.TimeEventEnd) }
    }
else { $booking = $null }

return $booking

}

Get-BookingAtTime -room "BH101" -time "8:30AM"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment