Managing bookings efficiently is crucial for businesses that rely on scheduled services. Microsoft Graph API provides powerful tools to manage Microsoft Bookings programmatically. In this article, we will explore a PowerShell script that helps administrators list, create, update, and delete bookings dynamically using Microsoft Graph. This script can save time and reduce manual effort, making the process seamless and automated.
We’ll dive into the script, explain its functionality, and discuss potential enhancements, errors, and solutions to ensure you can use it effectively.
# Ensure you have the Microsoft Graph PowerShell module installed
# Install-Module -Name Microsoft.Graph -Scope CurrentUser
# Connect to Microsoft Graph
Write-Host "Connecting to Microsoft Graph..."
Connect-MgGraph -Scopes "Bookings.ReadWrite.All"
Function Show-Menu {
Write-Host "==================================="
Write-Host "1. List All Bookings"
Write-Host "2. Create a New Booking"
Write-Host "3. Update an Existing Booking"
Write-Host "4. Delete a Booking"
Write-Host "5. Exit"
Write-Host "==================================="
}
Function List-AllBookings {
Write-Host "Fetching all bookings..."
$bookings = Get-MgBookingBusiness -All
if ($bookings) {
$bookings | Select-Object Id, DisplayName, Email, Phone | Format-Table
} else {
Write-Host "No bookings found." -ForegroundColor Yellow
}
}
Function Create-Booking {
Write-Host "Enter the following details to create a new booking."
$DisplayName = Read-Host "Enter Display Name"
$Email = Read-Host "Enter Email"
$Phone = Read-Host "Enter Phone Number"
$params = @{
DisplayName = $DisplayName
EmailAddress = $Email
Phone = $Phone
}
Try {
$newBooking = New-MgBookingBusiness -BodyParameter $params
Write-Host "New booking created successfully with ID:" $newBooking.Id
} Catch {
Write-Host "Failed to create booking. Error:" $_.Exception.Message -ForegroundColor Red
}
}
Function Update-Booking {
Write-Host "Enter the ID of the booking you want to update:"
$BookingId = Read-Host "Booking ID"
Write-Host "Enter the new details for the booking (leave blank to skip)."
$newDisplayName = Read-Host "New Display Name"
$newEmail = Read-Host "New Email Address"
$newPhone = Read-Host "New Phone Number"
$updateParams = @{}
if ($newDisplayName) { $updateParams["DisplayName"] = $newDisplayName }
if ($newEmail) { $updateParams["EmailAddress"] = $newEmail }
if ($newPhone) { $updateParams["Phone"] = $newPhone }
Try {
Update-MgBookingBusiness -BookingBusinessId $BookingId -BodyParameter $updateParams
Write-Host "Booking updated successfully."
} Catch {
Write-Host "Failed to update booking. Error:" $_.Exception.Message -ForegroundColor Red
}
}
Function Delete-Booking {
Write-Host "Enter the ID of the booking you want to delete:"
$BookingId = Read-Host "Booking ID"
Try {
Remove-MgBookingBusiness -BookingBusinessId $BookingId -Confirm:$false
Write-Host "Booking deleted successfully."
} Catch {
Write-Host "Failed to delete booking. Error:" $_.Exception.Message -ForegroundColor Red
}
}
# Main Program Loop
Do {
Show-Menu
$choice = Read-Host "Select an option"
Switch ($choice) {
"1" { List-AllBookings }
"2" { Create-Booking }
"3" { Update-Booking }
"4" { Delete-Booking }
"5" {
Write-Host "Exiting... Have a great day!" -ForegroundColor Green
Break
}
Default {
Write-Host "Invalid selection. Please try again." -ForegroundColor Yellow
}
}
} While ($choice -ne "5")
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Error | Cause | Solution |
AccessDenied | Insufficient permissions. | Ensure Bookings.ReadWrite.All permissions are granted. |
InvalidRequest | Incorrect or missing parameters. | Verify required parameters like DisplayName and Email are correctly provided. |
ResourceNotFound | Invalid Booking ID. | Check the entered Booking ID for accuracy. |
AuthenticationError | Token expiration or authentication failure. | Reconnect to Microsoft Graph using Connect-MgGraph. |
This PowerShell script simplifies the management of Microsoft Bookings by enabling administrators to list, create, update, or delete bookings dynamically. With its interactive menu-driven interface, it caters to various administrative needs, improving productivity and reducing manual intervention.
By leveraging this script and exploring further enhancements, administrators can unlock the full potential of Microsoft Graph API for managing bookings. Try it out and let automation handle your booking tasks effortlessly!
© M365Corner. All Rights Reserved. Design by HTML Codex.