cEPRSXml.psm1


[DSCResource()]

class cEPRSLoadXml
{
    [DscProperty(Key)] [String] $ConfigFilePath
    
    [cEPRSLoadXml] Get()
    {
        $config = @{FilePath = $this.FilePath; }  
        return $config
    }
    
    [bool] Test()
    {
        $curentDir = Get-Location

        if($this.ConfigFilePath)
        {
             return $false
        }
        else
        {
             return $true
        }
        
    
    }

    [void] Set()
    {
        try
    {
        
            [System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument

             $xd.load($this.ConfigFilePath)
        
    }
    Catch [system.exception]
    {
        Write-Output $_.exception.message
    }
    Finally
    {
        "Executed Successfully"
    }
}
}

[DSCResource()]
class cEPRSReadXml
{
    [DscProperty(Key)] [String] $Path
    [DscProperty(Key)] [String] $FileName
    
    [cEPRSReadXml] Get()
    {
        $config = @{FileName = $this.FileName; }  
        return $config
    }
    
    [bool] Test()
    {
        if(Test-Path -isvalid "$($this.Path)\$($this.FileName)")
    {       
        $result=$false
    }
    else
    {
        Write-Verbose "The specified file/path does not exist. Please pass valid file name and path"
        $result=$true
    }

    return $result
    }

    [void] Set()
    {
        try
    {    
        Write-Verbose "Placing contents of text file into a variable - START $($this.Path)\$($this.FileName)" -Verbose
        $FileContent = Get-Content "$($this.Path)\$($this.FileName)"
        Write-Verbose "Placing contents of text file into a variable - END" -Verbose

        if($FileContent)
        {
            Write-Verbose "Displaying content of file $($this.FileName) -START" -Verbose
            foreach($line in $FileContent)
            {
                Write-Verbose $line -Verbose
            }
            Write-Verbose "Displaying content of file $($this.FileName) -END" -Verbose
        }
    }
    Catch [system.exception]
    {
        Write-Verbose $_.exception.message
    }

}
}

[DSCResource()]
class cEPRSXMLRemoveNodeSingle
{


    [DscProperty(Key)] [string] $ConfigPath
    [DscProperty(Key)] [ValidateSet("Absent","Present")] [string] $Ensure
    [DscProperty(Mandatory = $true)] [bool] $IsSinglenode
    [DscProperty(Key)] [string] $Configurationpath
    [DscProperty(Key)] [string] $Innertext

[cEPRSXMLRemoveNodeSingle] Get()
{

        $returnValue = @{
        ConfigPath = [System.String]
        Ensure = [System.String]
        }

        return $returnValue

}

[void] Set()

{
    [Bool]$SingleNode = $this.IsSinglenode

    Write-Verbose "set function execution starts......." -Verbose 


        if(Test-Path $this.ConfigPath)
       {
           $xml=New-Object XML
           $xml.load($this.ConfigPath)
           if($SingleNode) 
           {
             $nodes=$xml.SelectSingleNode($this.Configurationpath)
              foreach($node in $nodes)
             {
                 if($node.InnerText -eq $this.Innertext)
                 {
                     [Void]$node.ParentNode.RemoveChild($node)
                      Write-Verbose "Single node $node removed successfully" -Verbose
            
                 }
             }
           }
           $xml.Save($this.ConfigPath)
       }

       Write-Verbose "set function execution ends......."
}

[bool] Test()
{
        
    if(!(Test-Path $this.ConfigPath))
    {
        Write-Warning "$($this.ConfigPath) doesn't exist"
        return $true

    }
    else
    {
        return $false
    }
}
}

[DSCResource()]
class cEPRSXMLRemoveNodeKeyValuePair
{


    [DscProperty(Key)] [string] $ConfigPath
    [DscProperty(Key)] [ValidateSet("Absent","Present")] [string] $Ensure
    [DscProperty(Key)] [string] $Tagname
    [DscProperty(Key)] [string] $AttributekeyName
    [DscProperty(Key)] [string] $AttributekeyValue
    [DscProperty(Key)] [string] $AttibutenodetoUpdate
    [DscProperty(Key)] [string] $AttributenodeValue

[cEPRSXMLRemoveNodeKeyValuePair] Get()
{

        $returnValue = @{
        ConfigPath = [System.String]
        Ensure = [System.String]
        }

        return $returnValue

}

[void] Set()

{
   $AttributeName = $this.AttributekeyName
    $AttributeValue = $this.AttributekeyValue
    $tag = $this.Tagname

    Write-Verbose "set function execution starts......." -Verbose 


        if(Test-Path $this.ConfigPath)
       {
           $xml=New-Object XML
           $xml.load($this.ConfigPath)
           Write-Verbose "Tag $tag" -Verbose
           $Count = $null
           
             $nodes = $xml.GetElementsByTagName($tag)
            Write-Verbose "In else part: $nodes" -Verbose
               if($nodes.Count -gt 0)
                {
                 Write-Verbose "Nodes: $nodes" -Verbose
                    foreach($node in $nodes)
                    {
                        Write-Verbose "$node : $AttributeName" -Verbose
                       if($node.$AttributeName -eq "$AttributeValue")
                       {
                          [Void]$node.ParentNode.RemoveChild($node)
                          Write-Verbose "$node.$AttributeName node removed successfully" -Verbose
                          $Count=1
                          break;
                       }
                    }
                }
               if(!$Count)
               {
                  Write-Verbose "No node exists in configuration file with $AttributeName" -Verbose
               }
          
           $xml.Save($this.ConfigPath)
       }

       Write-Verbose "set function execution ends......."
}

[bool] Test()
{
             
    if(!(Test-Path $this.ConfigPath))
    {
        Write-Warning "$($this.ConfigPath) doesn't exist"
        return $true

    }
    else
    {
        return $false
    }
}
}

[DscResource()]
class cEPRSAddConfigNodes
{
   [DscProperty(Key)] [String] $ConfigPath
   [DscProperty(Key)] [String] $Ensure
   [DscProperty(Key)] [String] $TagName
   [DscProperty(Key)] [String] $AttributeKeyName
   [DscProperty(Key)] [String] $AttributeKeyValue
   [DscProperty(Key)] [String] $AttributeNodeToUpdate
   [DscProperty(Key)] [String] $AttributeNodeValue
   [DscProperty(Key)] [String] $ConfigurationSectionPath
   [DscProperty(Key)] [String] $LogFile
   


  [cEPRSAddConfigNodes] Get()
  {
      $returnValue = @{
                         ConfigPath = $this.ConfigPath
                         Ensure = $this.Ensure
                         TagName = $this.TagName
                         AttributeKeyName = $this.AttributeKeyName
                         AttributeKeyValue = $this.AttributeKeyValue
                         AttributeNodeToUpdate = $this.AttributeNodeToUpdate
                         AttributeNodeValue = $this.AttributeNodeValue
                         ConfigurationSectionPath = $this.ConfigurationSectionPath
                         LogFile = $this.LogFile
           
      
                       }

              return $returnValue
  }

  [bool] Test()
  {
     if($this.Ensure -eq "Present")
     {
       $result =  $false
     }
     else
     {
       $result = $true
     }

      return $result
  }

  [void] Set()
  {

       Write-Verbose "Set function starts"

       if(Test-Path "$($this.ConfigPath)") 
       {
         $xml = New-Object XML
         $xml.Load($this.ConfigPath)
         $node = $xml.SelectSingleNode("$($this.ConfigurationSectionPath)")
         if($node)
         {
            $xmlElt = $xml.CreateElement($this.Tagname)
            $xmlElt.SetAttribute("$($this.AttributeKeyName)","$($this.AttributeKeyValue)")
            $xmlElt.SetAttribute("$($this.AttributeNodeToUpdate)","$($this.AttributeNodeValue)")
            $node.appendchild($xmlElt)

                    
         }
         else
         {
             Write-Output "Configuration section $this.ConfigurationSectionPath does not exists in config file"
             Write-Output "Configuration section $this.ConfigurationSectionPath does not exists in config file" | Out-File $this.Logfile -Append
         }
         $xml.save("$($this.ConfigPath)")
       }

       Write-Verbose "Set Execution Ends"

  }

}


[DscResource()]
class cEPRSXmlEditSingleNode
{


    [DscProperty(Key)] [string] $ConfigPath
    [DscProperty(Key)] [ValidateSet("Absent","Present")] [string] $Ensure
    [DscProperty(Mandatory = $true)] [bool] $IsSinglenode
    [DscProperty(Key)] [string] $Configurationpath
    [DscProperty(Key)] [string] $Innertext
    [DscProperty(Key)] [string] $InnertexttoUpdate

[cEPRSXmlEditSingleNode] Get()
{

        $returnValue = @{
        ConfigPath = [System.String]
        Ensure = [System.String]
        }

        return $returnValue

}

[void] Set()

{
    [Bool]$SingleNode = $this.IsSinglenode
    Write-Verbose "set function execution starts......." -Verbose 

       if(Test-Path $this.ConfigPath)
       {
           $xml=New-Object XML
           $xml.load($this.ConfigPath)
           if($SingleNode) 
           {
             $nodes=$xml.SelectSingleNode($this.Configurationpath)
              foreach($node in $nodes)
             {
                 if($node.InnerText -eq $this.Innertext)
                 {
                     $node.InnerText=$this.InnertexttoUpdate
                      Write-Verbose "Single node $node updated successfully" -Verbose
            
                 }
             }
           }
           else
           {
             Throw "single node set to false! Please make it true"
           }
           $xml.Save($this.ConfigPath)
       }

       Write-Verbose "set function execution ends......."
}

[bool] Test()
{
       if(!(Test-Path $this.ConfigPath))
    {
        Write-Warning "$($this.ConfigPath) doesn't exist"
        return $true

    }
    else
    {
        return $false
    }
}
}

[DscResource()]
class cEPRSXmlEditKeyvaluePair
{


    [DscProperty(Key)] [string] $ConfigPath
    [DscProperty(Key)] [ValidateSet("Absent","Present")] [string] $Ensure
    [DscProperty(Key)] [string] $Tagname
    [DscProperty(Key)] [string] $AttributekeyName
    [DscProperty(Key)] [string] $AttributekeyValue
    [DscProperty(Key)] [string] $AttibutenodetoUpdate
    [DscProperty(Key)] [string] $AttributenodeValue

[cEPRSXmlEditKeyvaluePair] Get()
{

        $returnValue = @{
        ConfigPath = [System.String]
        Ensure = [System.String]
        }

        return $returnValue

}

[void] Set()

{
    $AttributeName = $this.AttributekeyName
    $AttributeValue = $this.AttributekeyValue
    $AttibutenodetoUpdate_xml=$this.AttibutenodetoUpdate
    $AttributenodeValue_new=$this.AttributenodeValue
    $tag = $this.Tagname

    Write-Verbose "set function execution starts......." -Verbose 


       if(Test-Path $this.ConfigPath)
       {
           $xml=New-Object XML
           $xml.load($this.ConfigPath)
           Write-Verbose "Tag $tag" -Verbose
           $Count = $null
           
           $nodes = $xml.GetElementsByTagName($tag)
            
               if($nodes.Count -gt 0)
                {
                 Write-Verbose "Nodes: $nodes" -Verbose
                    foreach($node in $nodes)
                    {
                        Write-Verbose "$node : $AttributeName" -Verbose
                       if($node.$AttributeName -eq "$AttributeValue")
                       {
                          $node.$AttibutenodetoUpdate_xml=$AttributenodeValue_new
                          Write-Verbose "$node.$AttibutenodetoUpdate_xml node updated successfully" -Verbose
                          $Count=1
                          break;
                       }
                    }
                }
               if(!$Count)
               {
                  Write-Verbose "No node exists in configuration file with $AttributeName" -Verbose
               }
           
           $xml.Save($this.ConfigPath)
       }

       Write-Verbose "set function execution ends......."
}

[bool] Test()
{
    if(!(Test-Path $this.ConfigPath))
    {
        Write-Warning "$($this.ConfigPath) doesn't exist"
        return $true

    }
    else
    {
        return $false
    }
}
}