Skip to content

Instantly share code, notes, and snippets.

@jdmills-edu
Created September 21, 2017 13:41
Show Gist options
  • Save jdmills-edu/b1467237644ed8383858aa0b9765b1e2 to your computer and use it in GitHub Desktop.
Save jdmills-edu/b1467237644ed8383858aa0b9765b1e2 to your computer and use it in GitHub Desktop.
A PowerShell script that uses the Dean Evans EMS API to retrieve a complete list of all buildings, rooms, and bookings in those rooms for the next calendar year.
#This script queries the EMS API for all bookings in all rooms in all buildings for the next calendar year.
#It requires the EMS Read-Only API and an authorised user on the EMS database.
#Date math
$today = Get-Date
$lastYear = $today.AddYears(-1)
$nextYear = $today.AddYears(1)
$todayString = $today.ToString("MMMM dd, yyyy")
$lastYearString = $lastYear.ToString("MMMM dd, yyyy")
$nextYearString = $nextYear.ToString("MMMM dd, yyyy")
#Define API connection parameters
$url = "http://ems.yourdomain.com/emsapi/service.asmx"
$apiuser = "yourapiuser"
$apipw = "yourapipassword"
#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
#Uncomment this line to see a grid view of all buildings.
#$buildings | Out-GridView
#Get Rooms in Buildings
$allRooms = @()
$buildings | ForEach-Object {
$buildingID = $_.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
$allRooms += $rooms
}
#Uncomment this line to see a grid view of all rooms.
#$allRooms | Sort BuildingCode | Out-GridView
#Get All Appointments in All Rooms
$allBookings = @()
$allRooms | ForEach-Object {
$roomID = $_.ID
$query_bookings = "
UserName=$apiuser
Password=$apipw
StartDate=$todayString
EndDate=$nextYearString
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
$allBookings += $bookings
}
#Uncomment this line to see a grid view of all bookings.
#$allBookings | Out-GridView
@jdmills-edu
Copy link
Author

Assuming the API is installed and configured on your EMS server, you can retrieve a list of available API endpoints at this URL: http(s)://ems.yourdomain.com/emsapi/service.asmx

Additional configuration is required to instruct the API to return JSON instead of XML.

@scotthardwick
Copy link

scotthardwick commented Mar 13, 2018

JD,
Thanks for sharing this. There is almost zero code out on the web for this particular API.
Here is an alternate way to do this as well.... (Though we are using XML here.)

Function Get-RoomsAndBookings {
# A PowerShell script that uses the Dean Evans EMS API to retrieve a complete list of all buildings, rooms, and bookings in those rooms for the next calendar year.

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

# Set up the Date Variables
$today = Get-Date
$nextYear = $today.AddYears(1)

# 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

# Get a Listing of all bookings in all rooms
$AllBookingsArray = @()
$RoomArray | ForEach-Object {
    $roomID = $_.ID
    [xml]$bookingsReturn = $EmsConnector.GetAllRoomBookings($username, $password, $today, $nextyear, $roomID, $ViewComboRoomComponents)
    $bookings = $bookingsReturn.Bookings.Data
    $AllBookingsArray += $bookings
}

return $AllBookingsArray
}

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