-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathW365-Display-CloudPc-InGracePeriod.ps1
52 lines (42 loc) · 1.97 KB
/
W365-Display-CloudPc-InGracePeriod.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Install the Microsoft Graph PowerShell SDK if not already installed
# Install-Module Microsoft.Graph -Scope CurrentUser
# Import the Microsoft Graph module
Import-Module Microsoft.Graph
# Define the output paths for the CSV file and the error log
$outputPath = "C:\CloudPCs_In_Grace_Period.csv"
$errorLogPath = "C:\CloudPCs_Error_Log.txt"
# Clear any existing error log
if (Test-Path $errorLogPath) { Remove-Item $errorLogPath }
# Connect to Microsoft Graph using CloudPC.Read.All scope with Beta profile
try {
# Set the Microsoft Graph API profile to 'beta' to access Cloud PC functionalities
Select-MgProfile -Name 'beta'
# Authenticate and request the required CloudPC scope
Connect-MgGraph -Scopes "CloudPC.Read.All"
Write-Host "Successfully connected to Microsoft Graph using the beta profile."
} catch {
Write-Error "Error connecting to Microsoft Graph: $_"
Add-Content $errorLogPath "Error connecting to Microsoft Graph: $_"
exit
}
# Query Cloud PCs in grace period
try {
# Retrieve Cloud PCs via Microsoft Graph API in beta
$cloudPCs = Get-MgBetaDeviceManagementVirtualEndpointCloudPc | Where-Object { $_.provisioningState -eq 'InGracePeriod' }
# Check if any Cloud PCs are found in grace period
if ($cloudPCs.Count -eq 0) {
Write-Host "No Cloud PCs found in grace period."
Add-Content $errorLogPath "No Cloud PCs found in grace period on $(Get-Date)."
exit
}
# Select the relevant properties to export
$cloudPCsFormatted = $cloudPCs | Select-Object displayName, userPrincipalName, provisioningState, lastModifiedDateTime
# Export the Cloud PCs to a CSV file
$cloudPCsFormatted | Export-Csv -Path $outputPath -NoTypeInformation
Write-Host "Export completed. CSV file saved at $outputPath"
} catch {
Write-Error "Error retrieving or exporting Cloud PC data: $_"
Add-Content $errorLogPath "Error retrieving or exporting Cloud PC data: $_"
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph