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!