cEPRSAddHost.psm1

enum Ensure
{
    Present
    Absent
}

[DSCResource()]

class cEPRSAddHostEntry
{
    [DscProperty(Key)] [String] $ServerName
    [DscProperty(Key)] [String] $IPAddress
    [DscProperty(Key)] [String] $MappingServerName
    

    [cEPRSAddHostEntry] Get()
    {
        
        $HostFile= "C:\Windows\System32\drivers\etc\hosts"
        if((gc $HostFile) -cmatch $this.IPAddress)
        {
             $Ensure = $true
        }
        else 
        {
            $Ensure = $false
        }
         Write-Verbose "Getting detected values..."
        
            $returnValue = @{
            ServerName = $this.ServerName
            Ensure =  $Ensure
        
        }

        return $returnValue
    
    }
     
    [bool] Test()
    {
        $result = $false
    
        $HostFile= "C:\Windows\System32\drivers\etc\hosts"
    
        if((gc $HostFile) -cmatch $this.IPAddress)
        {
            Write-Verbose "Entry for IPAddress already exists"
            $result = $true
        }
        else
        {
            $result = $false
        }
  
    
        return $result

    }

     [void] Set()
     {

        $IP = $this.IPAddress
        $MapServer = $this.MappingServerName
       
        $HostFile= "C:\Windows\System32\drivers\etc\hosts"
    
        $HostFileEntry = "`t$IP`t$MapServer"

        try
        {
            Write-Verbose "Adding host entry here - $HostFile"
            if((gc $HostFile) -cmatch $this.IPAddress)
            {
              Write-Verbose "The hosts file already contains the entry: $IP. File not updated.";
            }
            else
            {
                Write-Verbose "Adding entry $HostFileEntry to hosts file"
                Add-Content $HostFile $HostFileEntry
            }
        
        }
        Catch [system.exception]
        {
            Write-Verbose $_.exception.message
        }
        Finally
        {
            "Executed Successfully"
        } 
        
     }

}