Import powershell module in guest configuration script resource

Ethan Hou 56 Reputation points Microsoft Employee
2022-06-10T09:57:05.073+00:00

Hi,

I'm trying to create a guest configuration to monitor if the VM enabled Windows defender realtimeMonitoring.

Here is my code:

    Configuration EnableRealtimeMonitoring  
    {  
        Import-DscResource -ModuleName 'PSDscResources'  
      
        Node localhost  
        {  
            Script EnableRealtimeMonitoring  
            {  
                GetScript = {  
                    $RealtimeMonitoringEnabled = Get-MpComputerStatus | select -expandproperty RealTimeProtectionEnabled  
                    return @{ 'Result' = "$RealtimeMonitoringEnabled" }  
                }  
                TestScript = {  
                    $state = [scriptblock]::Create($GetScript).Invoke()  
      
                    if( $state.Result -eq 1 )  
                    {  
                        Write-Verbose -Message ('Current state of RealtimeMonitoringEnabled is {0}' -f $state.Result)  
                        return $true  
                    }  
                    Write-Verbose -Message ('Current state of RealtimeMonitoringEnabled is {0}' -f $state.Result)  
                    return $false  
                }  
                SetScript = {  
                    Set-MpPreference -DisableRealtimeMonitoring $false  
                }  
            }  
        }  
    }  

However, while testing, the PowerShell returns following error:

Error Message: PowerShell DSC resource                         | MSFT_ScriptResource  failed to execute Test-TargetResource functionality with error message:                          | System.InvalidOperationException: The test script threw an error.  --->                                               | System.Management.Automation.MethodInvocationException: Exception calling "Invoke" with "0"                           | argument(s): "The term 'Get-MpComputerStatus' is not recognized as a name of a cmdlet, function,                      | script file, or executable program. Check the spelling of the name, or if a path was included, verify                 | that the path is correct and try again."  ---> System.Management.Automation.CommandNotFoundException:                 | The term 'Get-MpComputerStatus' is not recognized as a name of a cmdlet, function, script file, or                    | executable program. Check the spelling of the name, or if a path was included, verify that the path is  
     | correct and try again.    at  
     | System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext,  
     | Exception exception)    at System  

The command Get-MpComputerStatus is a common term in the PowerShell module Defender, but I don't know how I can import this module in the guest configuration script. I tried lots of methods including:

Import-Module Defender  
Import-DscResource -ModuleName 'Defender'  
Import-DscResource -Name 'Defender'  

but either one worked.

Could you please tell me how can I import PowerShell modules in the guest configuration script?

Thanks!

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,102 questions
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
790 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jackson Martins 9,641 Reputation points MVP
    2022-06-10T11:37:27.83+00:00

    You can try Install-Module -Name WindowsDefender

    or you can download directly from https://www.powershellgallery.com/packages/WindowsDefender/1.0.0.4

    Get in touch if you need more help with this issue.

    --please don't forget to Accept as answer if the reply is helpful--