Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion from MSGraph module to MGGraph (Move to MSAL and enable Posh7 compatibility) #68

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions IntuneBackupAndRestore/Private/Get-MGGraphAllPages.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function Get-MGGraphAllPages {
<#
.SYNOPSIS
Retrieve all pages of a Microsoft Graph Query

.DESCRIPTION
Retrieve all pages of a Microsoft Graph Query

.PARAMETER GraphResults
Microsoft Graph Query Results

.EXAMPLE
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations" | Get-MGGraphAllPages

#>

[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]$GraphResults
)
$uri = $null
$QueryResults = @()
do {
if($uri){$GraphResults = Invoke-MgGraphRequest -uri "$uri"}
if ($GraphResults.value) {
$QueryResults += $GraphResults.value
}
else {
$QueryResults += $GraphResults
}
$uri = $GraphResults.'@odata.nextlink'
} until (!($uri))

#Check for null Value
if(($QueryResults.count -eq 2) -and ([string]::IsNullOrEmpty($QueryResults.value)) -and ($QueryResults.'@odata.context' -match "https://graph.microsoft.com/")) {
$QueryResults = $null
}

return $QueryResults

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupAppProtectionPolicy {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if ($null -eq (Get-MgContext)) {
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,9 +39,20 @@ function Invoke-IntuneBackupAppProtectionPolicy {
}

# Get all App Protection Policies
$appProtectionPolicies = Get-IntuneAppProtectionPolicy | Get-MSGraphAllPages
$appProtectionPolicies = Invoke-MgGraphRequest -Uri "/$ApiVersion/deviceAppManagement/managedAppPolicies" | Get-MgGraphAllPages

foreach ($appProtectionPolicy in $appProtectionPolicies) {

if (($appProtectionPolicy.AppGroupType -eq "selectedPublicApps") -and ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.androidManagedAppProtection')) {
$uri = "$ApiVersion/deviceAppManagement/androidManagedAppProtections('$($appProtectionPolicy.id)')"+'?$expand=apps'
$appProtectionPolicy.apps = (Invoke-MgGraphRequest -method get -Uri $uri).apps
}

if (($appProtectionPolicy.AppGroupType -eq "selectedPublicApps") -and ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.iosManagedAppProtection')) {
$uri = "$ApiVersion/deviceAppManagement/iosManagedAppProtections('$($appProtectionPolicy.id)')"+'?$expand=apps'
$appProtectionPolicy.add("apps",(Invoke-MgGraphRequest -method get -Uri $uri).apps)
}

$fileName = ($appProtectionPolicy.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$appProtectionPolicy | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\App Protection Policies\$fileName.json"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,39 @@ function Invoke-IntuneBackupAppProtectionPolicyAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if ($null -eq (Get-MgContext)) {
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
if (-not (Test-Path "$Path\App Protection Policies\Assignments")) {
$null = New-Item -Path "$Path\App Protection Policies\Assignments" -ItemType Directory
}

# Get all assignments from all policies
$appProtectionPolicies = Get-IntuneAppProtectionPolicy | Get-MSGraphAllPages
$appProtectionPolicies = Invoke-MgGraphRequest -Uri "/$ApiVersion/deviceAppManagement/managedAppPolicies" | Get-MgGraphAllPages

foreach ($appProtectionPolicy in $appProtectionPolicies) {
# If Android
if ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.androidManagedAppProtection') {
$assignments = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceAppManagement/androidManagedAppProtections('$($appProtectionPolicy.id)')/assignments"
$assignments = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceAppManagement/androidManagedAppProtections('$($appProtectionPolicy.id)')/assignments"
}
# Elseif iOS
elseif ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.iosManagedAppProtection') {
$assignments = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceAppManagement/iosManagedAppProtections('$($appProtectionPolicy.id)')/assignments"
$assignments = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceAppManagement/iosManagedAppProtections('$($appProtectionPolicy.id)')/assignments"
}
# Elseif Windows 10 with enrollment
elseif ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.mdmWindowsInformationProtectionPolicy') {
$assignments = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceAppManagement/mdmWindowsInformationProtectionPolicies('$($appProtectionPolicy.id)')/assignments"
$assignments = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceAppManagement/mdmWindowsInformationProtectionPolicies('$($appProtectionPolicy.id)')/assignments"
}
# Elseif Windows 10 without enrollment
elseif ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.windowsInformationProtectionPolicy') {
$assignments = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceAppManagement/windowsInformationProtectionPolicies('$($appProtectionPolicy.id)')/assignments"
$assignments = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceAppManagement/windowsInformationProtectionPolicies('$($appProtectionPolicy.id)')/assignments"
}
else {
# Not supported App Protection Policy
Expand Down
17 changes: 11 additions & 6 deletions IntuneBackupAndRestore/Public/Invoke-IntuneBackupClientApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ function Invoke-IntuneBackupClientApp {
[ValidateSet("v1.0", "Beta")]
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,14 +39,15 @@ function Invoke-IntuneBackupClientApp {
}

# Get all Client Apps
$clientApps = Invoke-MSGraphRequest -Url 'deviceAppManagement/mobileApps?$filter=(microsoft.graph.managedApp/appAvailability%20eq%20null%20or%20microsoft.graph.managedApp/appAvailability%20eq%20%27lineOfBusiness%27%20or%20isAssigned%20eq%20true)' | Get-MSGraphAllPages
$filter = "microsoft.graph.managedApp/appAvailability eq null or microsoft.graph.managedApp/appAvailability eq 'lineOfBusiness' or isAssigned eq true"
$clientApps = Invoke-MgRestMethod -Uri "$apiversion/deviceAppManagement/mobileApps?filter=$filter" | Get-MgGraphAllPages

foreach ($clientApp in $clientApps) {
$clientAppType = $clientApp.'@odata.type'.split('.')[-1]

$fileName = ($clientApp.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$clientAppDetails = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceAppManagement/mobileApps/$($clientApp.id)"
$clientAppDetails | ConvertTo-Json | Out-File -LiteralPath "$path\Client Apps\$($clientAppType)_$($fileName).json"
$clientAppDetails = Invoke-MgRestMethod -Uri "$apiversion/deviceAppManagement/mobileApps/$($clientApp.id)"
$clientAppDetails | ConvertTo-Json -depth 3 | Out-File -LiteralPath "$path\Client Apps\$($clientAppType)_$($fileName).json"

[PSCustomObject]@{
"Action" = "Backup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ function Invoke-IntuneBackupClientAppAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}


# Create folder if not exists
if (-not (Test-Path "$Path\Client Apps\Assignments")) {
$null = New-Item -Path "$Path\Client Apps\Assignments" -ItemType Directory
}

# Get all assignments from all policies
$clientApps = Invoke-MSGraphRequest -Url 'deviceAppManagement/mobileApps?$filter=(microsoft.graph.managedApp/appAvailability%20eq%20null%20or%20microsoft.graph.managedApp/appAvailability%20eq%20%27lineOfBusiness%27%20or%20isAssigned%20eq%20true)' | Get-MSGraphAllPages
# Get all Client Apps
$filter = "microsoft.graph.managedApp/appAvailability eq null or microsoft.graph.managedApp/appAvailability eq 'lineOfBusiness' or isAssigned eq true"
$clientApps = Invoke-MgRestMethod -Uri "$apiversion/deviceAppManagement/mobileApps?filter=$filter" | Get-MgGraphAllPages

foreach ($clientApp in $clientApps) {
$assignments = Get-DeviceAppManagement_MobileApps_Assignments -MobileAppId $clientApp.id
$assignments = (Invoke-MgRestMethod -Uri "/$apiversion/deviceAppManagement/mobileApps/$($clientApp.id)/assignments").value
if ($assignments) {
$fileName = ($clientApp.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$assignments | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\Client Apps\Assignments\$($clientApp.id) - $fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupConfigurationPolicy {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,11 +39,11 @@ function Invoke-IntuneBackupConfigurationPolicy {
}

# Get all Setting Catalogs Policies
$configurationPolicies = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies" | Get-MSGraphAllPages
$configurationPolicies = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies" | Get-MGGraphAllPages

foreach ($configurationPolicy in $configurationPolicies) {
$configurationPolicy | Add-Member -MemberType NoteProperty -Name 'settings' -Value @() -Force
$settings = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies/$($configurationPolicy.id)/settings" | Get-MSGraphAllPages
$settings = (Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies/$($configurationPolicy.id)/settings").value

if ($settings -isnot [System.Array]) {
$configurationPolicy.Settings = @($settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupConfigurationPolicyAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,11 +39,10 @@ function Invoke-IntuneBackupConfigurationPolicyAssignment {
}

# Get all assignments from all policies
$configurationPolicies = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies" | Get-MSGraphAllPages
$configurationPolicies = (Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies").value

foreach ($configurationPolicy in $configurationPolicies) {
$assignments = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies/$($configurationPolicy.id)/assignments" | Get-MSGraphAllPages

$assignments = (Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies/$($configurationPolicy.id)/assignments").value
if ($assignments) {
$fileName = ($configurationPolicy.name).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$assignments | ConvertTo-Json | Out-File -LiteralPath "$path\Settings Catalog\Assignments\$fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupDeviceCompliancePolicy {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,8 +39,7 @@ function Invoke-IntuneBackupDeviceCompliancePolicy {
}

# Get all Device Compliance Policies
$deviceCompliancePolicies = Get-DeviceManagement_DeviceCompliancePolicies | Get-MSGraphAllPages

$deviceCompliancePolicies = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/deviceCompliancePolicies" | Get-MGGraphAllPages
foreach ($deviceCompliancePolicy in $deviceCompliancePolicies) {
$fileName = ($deviceCompliancePolicy.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$deviceCompliancePolicy | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\Device Compliance Policies\$fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ function Invoke-IntuneBackupDeviceCompliancePolicyAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
if (-not (Test-Path "$Path\Device Compliance Policies\Assignments")) {
$null = New-Item -Path "$Path\Device Compliance Policies\Assignments" -ItemType Directory
}

# Get all assignments from all policies
$deviceCompliancePolicies = Get-DeviceManagement_DeviceCompliancePolicies | Get-MSGraphAllPages
# Get all Device Compliance Policies
$deviceCompliancePolicies = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/deviceCompliancePolicies" | Get-MGGraphAllPages

foreach ($deviceCompliancePolicy in $deviceCompliancePolicies) {
$assignments = Get-DeviceManagement_DeviceCompliancePolicies_Assignments -DeviceCompliancePolicyId $deviceCompliancePolicy.id
$assignments = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/deviceCompliancePolicies/$($deviceCompliancePolicy.id)/assignments" | Get-MGGraphAllPages
if ($assignments) {
$fileName = ($deviceCompliancePolicy.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$assignments | ConvertTo-Json | Out-File -LiteralPath "$path\Device Compliance Policies\Assignments\$fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupDeviceConfiguration {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,7 +39,7 @@ function Invoke-IntuneBackupDeviceConfiguration {
}

# Get all device configurations
$deviceConfigurations = Get-DeviceManagement_DeviceConfigurations | Get-MSGraphAllPages
$deviceConfigurations = Invoke-MgGraphRequest -Uri "$apiVersion/deviceManagement/deviceConfigurations" | Get-MGGraphAllPages


foreach ($deviceConfiguration in $deviceConfigurations) {
Expand All @@ -48,7 +52,7 @@ function Invoke-IntuneBackupDeviceConfiguration {
foreach ($omaSetting in $deviceConfiguration.omaSettings) {
# Check if this particular setting is encrypted, and get the plaintext only if necessary
if ($omaSetting.isEncrypted) {
$omaSettingValue = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/deviceConfigurations/$($deviceConfiguration.id)/getOmaSettingPlainTextValue(secretReferenceValueId='$($omaSetting.secretReferenceValueId)')" | Get-MSGraphAllPages
$omaSettingValue = Invoke-MgGraphRequest -Uri "$apiVersion/deviceManagement/deviceConfigurations/$($deviceConfiguration.id)/getOmaSettingPlainTextValue(secretReferenceValueId='$($omaSetting.secretReferenceValueId)')" | Get-MgGraphAllPages
}
# Define a new 'unencrypted' OMA Setting
$newOmaSetting = @{}
Expand Down
Loading