Skip to content

Commit 80604e0

Browse files
2 parents 1626ed8 + 4539e7c commit 80604e0

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

Linux/WSL/WSL Management Example/WSLDistroVersionCompliance.ps1

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
##
1+
##
22
## Compliance script used to calculate compliance against WSL distros based on Distro and Distro Version
33
##
44

@@ -27,60 +27,67 @@ $compliantDistroValues = [System.Collections.ArrayList]@()
2727
# Require last check in time to be within a certain number of days e.g.60 days
2828
$compliantLastCheckInTimeout = 60
2929

30-
# Pull list of user ids from registry
31-
$userIds = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Intune\WSLManagement' | Select-Object Name
32-
33-
# Put together a list of all the distros across users
34-
$distroIds = [System.Collections.ArrayList]@()
35-
foreach ($id in $userIds)
36-
{
37-
$id.Name = $id.Name.Replace('HKEY_LOCAL_MACHINE', 'HKLM:')
38-
$usersDistroIds = Get-ChildItem -Path $id.Name | Select-Object Name
30+
$isCompliant = $true
31+
try {
32+
# Pull list of user ids from registry
33+
$userIds = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Intune\WSLManagement' | Select-Object Name
3934

40-
foreach($usersDistroId in $usersDistroIds)
35+
# Put together a list of all the distros across users
36+
$distroIds = [System.Collections.ArrayList]@()
37+
foreach ($id in $userIds)
4138
{
42-
[void]$distroIds.Add($usersDistroId.Name)
43-
}
44-
}
39+
$id.Name = $id.Name.Replace('HKEY_LOCAL_MACHINE', 'HKLM:')
40+
$usersDistroIds = Get-ChildItem -Path $id.Name | Select-Object Name
4541

46-
# Create compliant last check in date
47-
$compliantDate = Get-Date
48-
$compliantDate = $compliantDate.AddDays($compliantLastCheckInTimeout * -1).ToUniversalTime()
42+
foreach($usersDistroId in $usersDistroIds)
43+
{
44+
[void]$distroIds.Add($usersDistroId.Name)
45+
}
46+
}
4947

50-
# Check compliance of all distros
51-
$isCompliant = $true
52-
foreach($distroId in $distroIds)
53-
{
54-
$name = $distroId.Replace('HKEY_LOCAL_MACHINE', 'HKLM:')
55-
$distro = Get-ItemPropertyValue -Path $name -Name Distro
56-
$distroVersion = Get-ItemPropertyValue -Path $name -Name Version
57-
$lastCheckin = Get-ItemPropertyValue -Path $name -Name LastCheckinTime
48+
# Create compliant last check in date
49+
$compliantDate = Get-Date
50+
$compliantDate = $compliantDate.AddDays($compliantLastCheckInTimeout * -1).ToUniversalTime()
5851

59-
# Convert and check last check in time
60-
$lastCheckin = Get-Date -Date $lastCheckin
61-
if ($lastCheckin -lt $compliantDate)
52+
# Check compliance of all distros
53+
foreach($distroId in $distroIds)
6254
{
63-
$isCompliant = $false
64-
break
65-
}
55+
$name = $distroId.Replace('HKEY_LOCAL_MACHINE', 'HKLM:')
56+
$distro = Get-ItemPropertyValue -Path $name -Name Distro
57+
$distroVersion = Get-ItemPropertyValue -Path $name -Name Version
58+
$lastCheckin = Get-ItemPropertyValue -Path $name -Name LastCheckinTime
6659

67-
# Check that disto and version meet compliance requirements
68-
$compliantDistro = $compliantDistroValues.where({$_.distro.ToLower() -eq $distro.ToLower()})
69-
if ($compliantDistro -ne $null)
70-
{
71-
$min = $compliantDistro.minVersion
72-
$max = $compliantDistro.maxVersion
73-
if ($distroVersion -lt $min -or $distroVersion -gt $max)
60+
# Convert and check last check in time
61+
$lastCheckin = Get-Date -Date $lastCheckin
62+
if ($lastCheckin -lt $compliantDate)
63+
{
64+
$isCompliant = $false
65+
break
66+
}
67+
68+
# Check that disto and version meet compliance requirements
69+
$compliantDistro = $compliantDistroValues.where({$_.distro.ToLower() -eq $distro.ToLower()})
70+
if ($compliantDistro -ne $null)
71+
{
72+
$min = $compliantDistro.minVersion
73+
$max = $compliantDistro.maxVersion
74+
if ($distroVersion -lt $min -or $distroVersion -gt $max)
75+
{
76+
$isCompliant = $false
77+
break
78+
}
79+
}
80+
else
7481
{
7582
$isCompliant = $false
7683
break
7784
}
7885
}
79-
else
80-
{
81-
$isCompliant = $false
82-
break
83-
}
86+
}
87+
catch {
88+
# Default to compliant if there are any issues reading registry keys
89+
$jsonOutput += @{ WSLInstancesComplianceStatus = "Compliant" }
90+
return $jsonOutput | ConvertTo-Json -Compress
8491
}
8592

8693
if ($isCompliant)

0 commit comments

Comments
 (0)