From 7d36e0a14798c747dddd2344a0092450e642798e Mon Sep 17 00:00:00 2001 From: Matthew McKinnon Date: Mon, 17 Mar 2025 15:01:33 +1000 Subject: [PATCH] chore: add compliance policy import to main script --- CompliancePolicy.ps1 | 25 ------------------------- ImportPolicies.ps1 | 28 +++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 30 deletions(-) delete mode 100644 CompliancePolicy.ps1 diff --git a/CompliancePolicy.ps1 b/CompliancePolicy.ps1 deleted file mode 100644 index 8aa9be1..0000000 --- a/CompliancePolicy.ps1 +++ /dev/null @@ -1,25 +0,0 @@ -# Connect to Microsoft Graph -Connect-MgGraph -Scopes "DeviceManagementConfiguration.ReadWrite.All", "Organization.Read.All", "Group.ReadWrite.All", "Directory.ReadWrite.All" -NoWelcome - -$policies = Get-ChildItem ./policies/compliance - -ForEach ($policie in $policies) { - $PolicieName = $policie.name - - $JsonData = Get-Content -Path ./policies/compliance/$PolicieName -Raw - $JsonDataUpdated = $JsonData -replace '\$tenantId', $tenantId - $PolicyObject = $JsonDataUpdated | ConvertFrom-Json - - try { - $uri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies" # Using the beta version - $response = Invoke-MgGraphRequest -Method POST -Uri $uri -Body ($PolicyObject | ConvertTo-Json -Depth 10) - Write-Host "✅ $PolicieName - successfully imported!" - #$response - } catch { - Write-Error "❌ An error occurred while importing the policy: $_" - } -} - - -# Disconnect from Graph -$null = Disconnect-Graph -ErrorAction SilentlyContinue diff --git a/ImportPolicies.ps1 b/ImportPolicies.ps1 index 050cec1..2966856 100644 --- a/ImportPolicies.ps1 +++ b/ImportPolicies.ps1 @@ -65,8 +65,8 @@ Write-Host "✅ Successfully created group $group" $policies = Get-ChildItem ./policies/settingscatalog -ForEach ($policie in $policies) { - $PolicyName = $policie.name +ForEach ($policy in $policies) { + $PolicyName = $policy.name $JsonData = Get-Content -Path ./policies/settingscatalog/$PolicieName -Raw $JsonDataUpdated = $JsonData -replace '\$tenantId', $tenantId @@ -81,6 +81,24 @@ ForEach ($policie in $policies) { } } +$policies = Get-ChildItem ./policies/compliance + +ForEach ($policy in $policies) { + $PolicyName = $policy.name + + $JsonData = Get-Content -Path ./policies/compliance/$PolicyName -Raw + $JsonDataUpdated = $JsonData -replace '\$tenantId', $tenantId + $PolicyObject = $JsonDataUpdated | ConvertFrom-Json + + try { + $uri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies" # Using the beta version + $null = Invoke-MgGraphRequest -Method POST -Uri $uri -Body ($PolicyObject | ConvertTo-Json -Depth 10) + Write-Host "✅ $PolicyName - successfully imported!" + } catch { + Write-Error "❌ An error occurred while importing the policy: $_" + } +} + # Create Windows Update Ring Policies # Create a baseline policy using web interface @@ -225,7 +243,7 @@ $ring = $body.displayName $groupBodyJson = $Body | ConvertTo-Json -Depth 10 # Send the POST request to create the Driver Update Profile -$response = Invoke-MgGraphRequest -Method POST -Uri $uri -Body $groupBodyJson -ContentType "application/json" +$null = Invoke-MgGraphRequest -Method POST -Uri $uri -Body $groupBodyJson -ContentType "application/json" Write-Host "✅ Successfully created group $ring" @@ -245,7 +263,7 @@ $ring = $body.displayName $groupBodyJson = $Body | ConvertTo-Json -Depth 10 # Send the POST request to create the Driver Update Profile -$response = Invoke-MgGraphRequest -Method POST -Uri $uri -Body $groupBodyJson -ContentType "application/json" +$null = Invoke-MgGraphRequest -Method POST -Uri $uri -Body $groupBodyJson -ContentType "application/json" Write-Host "✅ Successfully created group $ring" @@ -264,7 +282,7 @@ $ring = $body.displayName $groupBodyJson = $Body | ConvertTo-Json -Depth 10 # Send the POST request to create the Driver Update Profile -$response = Invoke-MgGraphRequest -Method POST -Uri $uri -Body $groupBodyJson -ContentType "application/json" +$null = Invoke-MgGraphRequest -Method POST -Uri $uri -Body $groupBodyJson -ContentType "application/json" Write-Host "✅ Successfully created group $ring" $null = Disconnect-Graph -ErrorAction SilentlyContinue \ No newline at end of file