Why is my code running twice inside my TestScript section

Welcome Forums DSC (Desired State Configuration) Why is my code running twice inside my TestScript section

This topic contains 0 replies, has 1 voice, and was last updated by  Charlie 2 months ago.

  • Author
    Posts
  • #161213

    Participant
    Topics: 7
    Replies: 1
    Points: 38
    Rank: Member

    Can someone please help me with the following

    I am using Azure Automation DSC in a LAB, I have once Azure VM on-boarded to Azure Automation DSC

    I am finding it quite flaky at the moment (not reliable), unless I am doing something wrong

    for example if I run the following PowerShell as a function

    function CheckWindowsFeatures {
     
     begin {
      
      $AllowedFeatures = @("BitLocker", "EnhancedStorage", "FileAndStorage-Services", "NET-Framework-45-Core", "NET-Framework-45-Features", "NET-WCF-Services45", "NET-WCF-TCP-PortSharing45", "PowerShell", "PowerShell-ISE", "PowerShellRoot", "Storage-Services", "System-DataArchiver", "Windows-Defender", "WoW64-Support", "XPS-Viewer")
      $InstalledWindowsFeatures = (Get-WindowsFeature | Where-Object { $_.Installed } | Select-Object -ExpandProperty Name)
      $OFS = ","
      return @{ 'Installed Features' = $InstalledWindowsFeatures }
     }
     
     process {
      
      Write-Verbose "Checking Windows Features/Roles"
      
      $AdditionalFeatures = $MissingFeatures = $false
      
      $Comparison = @(Compare-Object $using:AllowedFeatures $InstalledWindowsFeatures | Where-Object { $_.SideIndicator -eq "=>" } | Select-Object -ExpandProperty InputObject)
      if ($Comparison.Count)
      {
       $AdditionalFeatures = $true
       $WarningMessage = "Found $($Comparison.Count) Windows Features/Roles which are not on the allowed list of Windows Features. The list of these additons is as follows`r`n$($Comparison | Out-String)"
       Write-Verbose $WarningMessage
       Write-EventLog -LogName Application -Message $WarningMessage -Source "Application" -EntryType Warning -EventId 102
       
      }
      
      $Comparison2 = @(Compare-Object $using:AllowedFeatures $InstalledWindowsFeatures | Where-Object { $_.SideIndicator -eq "< =" } | Select-Object -ExpandProperty InputObject)
      if ($Comparison2.Count)
      {
       $MissingFeatures = $true
       $WarningMessage = "Found $($Comparison2.Count) Missing Windows Features/Roles which should be installed but are not. The list of missing features/roles is as follows`r`n$($Comparison2 | Out-String)"
       Write-Verbose $WarningMessage
       Write-EventLog -LogName Application -Message $WarningMessage -Source "Application" -EntryType Warning -EventId 103
       
      }
      
      if ($AdditionalFeatures -or $MissingFeatures) { return $false }
      else { return $true }
     }
     
     end {
      
      $string = [string]"Not set this items via script by design"
      Write-Verbose $string
     }
     
     
    }

    It runs and I see two entries on my Windows Event log, one to tell me the E: drive is missing and another to tell me my C: drive is the wrong size (as expected)

    If I run the same code using DSC I set the above entries in my event log, however I get them twice so I end up with four entries in my event log (which I do not want as there is only two issues to log). Here is my DSC code

    Configuration MtConfig01
    {
     Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
     
     
     Node localhost
     {
      
      Script CheckDiskVolumes {
                 
       GetScript = { Return @{
         Result = [string]$(Get-CimInstance Win32_Volume -ErrorAction Stop | Select-Object DriveLetter, DriveType, Capacity, FreeSpace, Label, FileSystem, BootVolume, Compressed, SerialNumber | Out-String)
        } }
       
                
       TestScript = {
        
        
        $VolumesDriveLettersCompliant = $VolumeSizeCompliant = $true
        $VerbosePreference = "Continue"
        $ComputerName = $env:COMPUTERNAME
        $DriveLetterArray = @("C:", "D:", "E:")
        
        $Script:Volumes = Get-CimInstance Win32_Volume -ErrorAction Stop | Select-Object DriveLetter, DriveType, @{ l = 'CapacityGB'; e = { [System.Math]::Round(($_.Capacity / 1gb), 2) } }, @{ l = 'FreeSpaceGB'; e = { [System.Math]::Round(($_.FreeSpace / 1gb), 2) } }, Label, FileSystem, BootVolume, Compressed, SerialNumber
        
        foreach ($Volume in ($Volumes | Where-Object { $_.DriveLetter })) {
         
         #region check for missing or additional volumes
          # look for additiional drives
         if ($DriveLetterArray -notcontains $Volume.DriveLetter)
         {
          $WarningMessage = "Found the following Volume Drive Letter $($Volume.DriveLetter) on computer $ComputerName it should NOT be present`r`nThe allowed drive letters are`r`n$($DriveLetterArray | Out-String), please investigate and resolve."
          Write-Verbose $WarningMessage
          Write-EventLog -LogName Application -Message $WarningMessage -Source "Application" -EntryType Warning -EventId 99
          $VolumesDriveLettersCompliant = $false
         }
         
         #endregion check for missing or additional volumes
         
         #region check drive sizes
         if ($Volume.DriveLetter -eq "C:" -and ($Volume.CapacityGB -lt 200 -or $Volume.CapacityGB -gt 220))
         {
          $WarningMessage = "Disk Volume C: should be 200 GB in size but the actual size is $($Volume.CapacityGB) GB"
          Write-Verbose $WarningMessage
          Write-EventLog -LogName Application -Message $WarningMessage -Source "Application" -EntryType Warning -EventId 101
          $VolumeSizeCompliant = $false
         }
         
         if ($Volume.DriveLetter -eq "E:" -and ($Volume.CapacityGB -lt 200 -or $Volume.CapacityGB -gt 220))
         {
          $WarningMessage = "Disk Volume E: should be 200 GB in size but the actual size is $($Volume.CapacityGB) GB"
          Write-Verbose $WarningMessage
          Write-EventLog -LogName Application -Message $WarningMessage -Source "Application" -EntryType Warning -EventId 101
          $VolumeSizeCompliant = $false
         }
         #endregion check drive sizes
        }
        
         # look for missing drives
        if (@($Volumes | Select-Object -ExpandProperty DriveLetter) -notcontains "C:") { Write-Verbose "C: drive volume missing"; $MissingDrive += "C:" }
        if (@($Volumes | Select-Object -ExpandProperty DriveLetter) -notcontains "D:") { Write-Verbose "D: drive volume missing"; $MissingDrive += "D:" }
        if (@($Volumes | Select-Object -ExpandProperty DriveLetter) -notcontains "E:") { Write-Verbose "E: drive volume missing"; $MissingDrive += "E:" }
        
        if ($MissingDrive.count)
        {
         $WarningMessage = "The following drive volume letter/s are missing $($MissingDrive | Out-String)"
         Write-Verbose $WarningMessage
         Write-EventLog -LogName Application -Message $WarningMessage -Source "Application" -EntryType Warning -EventId 100
         $VolumesDriveLettersCompliant = $false
        }
        
        
        if (!$VolumesDriveLettersCompliant -or !$VolumeSizeCompliant) { return $false }
        else { return $true }
        
       }
       
       # Returns nothing            
       SetScript = {
        $string = [string]"Not set this items via script by design"
        Write-Verbose $string
       }
      }
      
      
     }
    }
    I do not understand why the exact same code when run via DSC produces duplicate events in the event log
    Please help 🙂
    CXMelga

     

     

You must be logged in to reply to this topic.