-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathExportPolicy.ps1
More file actions
67 lines (54 loc) · 1.92 KB
/
ExportPolicy.ps1
File metadata and controls
67 lines (54 loc) · 1.92 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Function to get the policy ID by name
function Get-PolicyIdByName {
param (
[string]$policyName
)
# Get all device configurations
$policies = Get-MgDeviceManagementDeviceConfiguration
# Find the policy with the matching name
$policy = $policies | Where-Object { $_.displayName -eq $policyName }
if ($policy) {
return $policy.id
}
else {
Write-Error "Policy with name '$policyName' not found."
return $null
Exit
}
}
# Function to export Intune configuration policy to JSON
function Export-IntuneConfigPolicy {
param (
[string]$policyName,
[string]$outputFilePath
)
# Get the policy ID by name
$policyId = Get-PolicyIdByName -policyName $policyName
if ($policyId) {
# Get the configuration policy by ID
$policy = Get-MgDeviceManagementDeviceConfiguration -DeviceConfigurationId $policyId
# Convert the policy to JSON and save to file
$json = $policy | ConvertTo-Json -Depth 10
$json | Out-File -FilePath $outputFilePath -Encoding utf8
Write-Output "Configuration policy exported to $outputFilePath"
}
}
# Function to sanitize the file name
function Sanitize-FileName {
param (
[string]$fileName
)
# Replace invalid characters with an underscore
$fileName = $fileName -replace '[<>:"/\\|?*]', ''
# Replace spaces with underscores
$fileName = $fileName -replace ' ', '_'
# Remove square brackets
$fileName = $fileName -replace '[\[\]]', ''
return $fileName
}
# Main Execution
$policyName = "[Bonus] W10 Enable FIDO2 security keys" # Replace with your actual policy name
# Sanitize the policy name to create a valid file name
$sanitizedPolicyName = Sanitize-FileName -fileName $policyName
$outputFilePath = Join-Path $PSScriptRoot "$($sanitizedPolicyName).json"
Export-IntuneConfigPolicy -policyName $policyName -outputFilePath $outputFilePath