Manage Microsoft Bookings Using Graph PowerShell

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.

The Script

# 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

How the Script Works

  • Connecting to Microsoft Graph: The script begins by connecting to Microsoft Graph using the Connect-MgGraph cmdlet with the Bookings.ReadWrite.All scope.
  • Menu Interface: The Show-Menu function displays a list of actions for administrators to choose from, including listing, creating, updating, deleting bookings, or exiting the script.
  • List All Bookings: The List-AllBookings function retrieves all bookings using Get-MgBookingBusiness and displays essential details in a table format.
  • Create a Booking: The Create-Booking function prompts the user for booking details and uses New-MgBookingBusiness with the required parameters.
  • Update a Booking:The Update-Booking function allows partial updates by collecting only the updated details and passing them to the Update-MgBookingBusiness cmdlet.
  • Delete a Booking:The Delete-Booking function deletes a booking by its ID using Remove-MgBookingBusiness.
  • Exit:The script exits gracefully when the user selects the "Exit" option.

Further Enhancements

  • Advanced Filtering: Add support for filtering bookings based on specific properties, such as date or customer.
  • Error Logging: Implement a logging mechanism to track errors and execution details for auditing purposes.
  • Bulk Operations: Enhance the script to handle bulk creation, updating, or deletion of bookings using a CSV file.
  • GUI Integration: Develop a graphical user interface (GUI) for a more user-friendly experience.

Possible Errors & Solutions

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.

Conclusion

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.