Windows 365 - End Grace Period using PowerShell Graph

Ranjithkumar Duraisamy 226 Reputation points
2024-08-08T05:55:06.05+00:00

Hello, I've been trying to create a PowerShell script to end grace period of Cloud PCs but getting an error like below. Could you please help finding what's wrong?

Failed to stop grace period for Cloud PC ID <ID>: One or more errors occurred.

# Set execution policy to bypass (if needed)
Set-ExecutionPolicy Bypass -Scope Process -Force

# Import required modules
Import-Module Microsoft.Graph.Authentication

# Connect to Microsoft Graph with appropriate permissions
Connect-MgGraph -Scopes "CloudPC.ReadWrite.All"

# Retrieve all managed Cloud PCs and filter for those in grace period
$cloudPCs = Get-MgBetaDeviceManagementVirtualEndpointCloudPc -All | Where-Object { $_.status -eq 'inGracePeriod' }

#Get-MgBetaDeviceManagementVirtualEndpointCloudPc -All | Where-Object { $_.status -eq 'inGracePeriod' }|GM

foreach ($cloudPC in $cloudPCs)
{
    $cloudPC | Select-Object ID, ManagedDeviceName, DisplayName, ProvisioningPolicyName, Status, GracePeriodEndDateTime | Format-Table -AutoSize
    $cloudPCID = $cloudPC.ID
    Write-Host "Ending GracePeriod of $($cloudPC.ID)"
    
    try {
        Stop-MgDeviceManagementVirtualEndpointCloudPcGracePeriod -CloudPcID $cloudPCID -WhatIf
    }
    catch {
        Write-Host "Failed to stop grace period for Cloud PC ID $($cloudPCID): $_"
    }
}

Windows 365 Enterprise
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,489 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.