-
Notifications
You must be signed in to change notification settings - Fork 187
/
Get-DeviceHealthScripts.ps1
62 lines (47 loc) · 2.25 KB
/
Get-DeviceHealthScripts.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
53
54
55
56
57
58
59
60
61
62
####################################################
#region Initialization code
$m = Get-Module -Name Microsoft.Graph.Intune -ListAvailable
if (-not $m)
{
Install-Module NuGet -Force
Install-Module Microsoft.Graph.Intune
}
Import-Module Microsoft.Graph.Intune -Global
#endregion
####################################################
Function Get-DeviceHealthScripts(){
<#
.SYNOPSIS
Get all or individual Intune PowerShell Health scripts (aka Proactive Remediation scripts) and save them in specified folder.
.DESCRIPTION
The Get-DeviceHealthScripts cmdlet downloads all PowerShell Detection and Remediation scripts from Intune to a specified folder.
Initial Author: Oliver Kieselbach (oliverkieselbach.com)
The script is provided "AS IS" with no warranties.
.PARAMETER FolderPath
The folder where the PowerShell scripts are saved.
.EXAMPLE
Download all Intune PowerShell scripts to the specified folder
Get-DeviceHealthScripts -FolderPath C:\temp\HealthScripts
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String] $FolderPath
)
$graphApiVersion = "Beta"
$graphUrl = "https://graph.microsoft.com/$graphApiVersion"
$result = Invoke-MSGraphRequest -Url "$graphUrl/deviceManagement/deviceHealthScripts" -HttpMethod GET
$scriptIds = $result.value | Select-Object id,displayName
foreach($scriptId in $scriptIds){
$script = Invoke-MSGraphRequest -Url "$graphUrl/deviceManagement/deviceHEalthScripts/$($scriptId.id)" -HttpMethod GET
$healthScriptPath = Join-Path $FolderPath ($script.displayName)
New-Item -Path $healthScriptPath -ItemType Directory
if (($script.detectionScriptContent).Length -ne 0) {
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($($script.detectionScriptContent))) | Out-File -Encoding ASCII -FilePath $(Join-Path $healthScriptPath "DetectionScript.ps1")
}
if (($script.remediationScriptContent).Length -ne 0) {
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($($script.remediationScriptContent))) | Out-File -Encoding ASCII -FilePath $(Join-Path $healthScriptPath "RemediationScript.ps1")
}
}
}
Connect-MSGraph | Out-Null
Get-DeviceHealthScripts -FolderPath C:\temp\HealthScripts