As a user, I need a document to tell me how I can import PowerShell module to the DSC script.
I'm trying to create a guest configuration to monitor if the VM enabled Windows defender realtimeMonitoring.
Configuration EnableRealtimeMonitoring
{
Import-DscResource -ModuleName 'PSDscResources'
Node localhost
{
Script EnableRealtimeMonitoring
{
GetScript = {
$RealtimeMonitoringEnabled = Get-MpComputerStatus | select -expandproperty RealTimeProtectionEnabled
return @{ 'Result' = "$RealtimeMonitoringEnabled" }
}
TestScript = {
# Create and invoke a scriptblock using the $GetScript automatic variable, which contains a string representation of the GetScript.
$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 evaluating the compliance status the Get-GuestConfigurationPackageComplianceStatus command return 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
So I tried to import Defender module since Get-MpComputerStatus is a command from Defender module.
Import-DscResource -ModuleName 'Defender'
Import-DscResource -Name 'Defender'
Import-Module -Name 'Defender'