Modules/M365DSCAgent.psm1

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# This function tests the configuration of the agent
function Test-M365DSCAgent
{
    [CmdletBinding()]
    param(

    )
    #region Telemetry
    $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new()
    $data.Add("Event", "TestAgent")
    Add-M365DSCTelemetryEvent -Data $data
    #endregion

    [array]$Recommendations = @()
    [array]$Issues = @()
    $TotalSteps = 3

    #region PowerShell Version
    Write-Progress -Activity "Scanning PowerShell Version..." -PercentComplete (1/$TotalSteps*100)
    $CurrentPSVersion = [version]$PSVersionTable.PSVersion

    if ($CurrentPSVersion -lt [version]5.1)
    {
        $Recommendations += @{
            ID = 'R1'
            Message = "We recommend installing PowerShell 5.1. You can download and install it from: " + `
                "https://www.microsoft.com/en-us/download/details.aspx?id=54616"
            }
    }
    elseif ($CurrentPSVersion -ge [version]"6.0")
    {
        $Issues += @{
            ID = 'I1'
            Message = "Microsoft365DSC is not supported with PowerShell Version $CurrentPSVersion. Please install version 5.1 from: " + `
                   "https://www.microsoft.com/en-us/download/details.aspx?id=54616"
        }
    }
    #endregion

    # We need to do a quick configuration of WinRM in order to be able to obtain configuration information;
    Write-Progress -Activity "Scanning WinRM MaxEnvelopeSize..." -PercentComplete (2/$TotalSteps*100)
    WinRM QuickConfig -Force | Out-Null

    #region MaxEnvelopeSize
    $CurrentMaxEnvelopeSize = (Get-Item -Path WSMan:\localhost\MaxEnvelopeSizekb).Value

    if ($CurrentMaxEnvelopeSize -le 1024)
    {
        $Recommendations += @{
            ID = 'R1'
            Message ="We recommend increasing the MaxEnvelopeSize of the agent's WinRM to a minimum of 10 Mb." + `
                " To make the change, run: Set-Item -Path WSMan:\localhost\MaxEnvelopeSizekb -Value 10240"
        }
    }
    #endregion

    #region Modules Dependencies
    Write-Progress -Activity "Scanning Dependencies..." -PercentComplete (3/$TotalSteps*100)
    $M365DSC = Get-Module Microsoft365DSC
    $ManifestPath = Join-Path -Path $M365DSC.ModuleBase -ChildPath 'Microsoft365DSC.psd1'
    $manifest = Import-PowerShellDataFile $ManifestPath
    $dependencies = $manifest.RequiredModules
    foreach ($dependency in $dependencies)
    {
        $module = Get-Module $dependency.ModuleName -ListAvailable | `
                        Where-Object -FilterScript {$_.Version -eq $dependency.RequiredVersion }
        if ($null -eq $module)
        {
            $Issues += @{
                ID = 'I2'
                Message ="M365DSC has a dependency on module $($dependency.ModuleName) which was not found. You need to install " + `
                    "this module by running: Install-Module $($dependency.ModuleName) -RequiredVersion $($dependency.RequiredVersion) -Force"
            }
        }
    }
    #endregion

    Write-Progress -Completed -Activity "Completed Analysis"
    if ($Issues.Count -gt 0)
    {
        Write-Information -MessageData ("The following issues were detected with the current agent's configuration. Please take " + `
                          "proper action to remediate.")
        $i = 1
        foreach ($issue in $Issues)
        {
            Write-Error -Message " [$i/$($Issues.Count)] $($issue.Message)"
        }
    }

    if ($Recommendations.Count -gt 0)
    {
        Write-Information -MessageData "The following recommendations were issued. We strongly recommend adressing those: "
        $i = 1
        foreach ($recommendation in $Recommendations)
        {
            Write-Warning " [$i/$($Recommendations.Count)] $($recommendation.Message)"
        }
    }

    if ($Recommendations.Count -eq 0 -and $Issues.Count -eq 0)
    {
        Write-Information -MessageData "The agent is properly configured."
    }
}