cEPRSGroupCreation.psm1

enum Ensure
{
   Absent
   Present
}

[DscResource()]
class cEPRSCreatingGroup
{

   [DscProperty(Key)] [String] $Ensure
   [DscProperty(Key)] [String] $GroupName    

   [cEPRSCreatingGroup] Get()
    {                        
      $node = @{
                  Ensure = $this.Ensure
                  GroupName = $this.GroupName                                    
               }    

               return $node
     }   

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

        return $result
   }

   [void] Set()
   {
        Write-Verbose "Create the group in the local users and group....."

        NET LOCALGROUP "$($this.GroupName)" /Add

   }
 
}


[DscResource()]
class cEPRSAddUsersToGroup
{
  [DSCProperty(key)] [String] $GroupName
  [DSCProperty(key)] [String] $Users 
  [DSCProperty(key)] [String] $Ensure
  

  [cEPRSAddUsersToGroup] Get()
  {
    Add-Type -AssemblyName System.DirectoryServices.AccountManagement 
    #Try to find a group by its name

    $principalcontext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList ([System.DirectoryServices.AccountManagement.ContextType]::Machine)
    $group = $null
      
    try
    {
        $group = [ System.DirectoryServices.AccountManagement.GroupPrincipal ]::FindByIdentity($principalcontext, $this.GroupName);
        
        if($group -ne $null)
        {
            $members = [String[]]@(Enumeratemembers -group $group)
         
            $returnvalue = @{          
                            GroupName = $this.Groupname
                            Users = $this.Users
                            Ensure = $this.Ensure
                            }

                    return $returnvalue
        }

        #if group is not found return Ensure = Absent
        return = @{
                    GroupName = $this.GroupName
                    Ensure = $this.Ensure
                    }

    }
    Finally
    {
        if($group -ne $null)
        {
            $group.Dispose();
        }
            $this.prinicpalcontext.Dispose();
        }
    }
  

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

        return $result      
    }

    [void] Set()
    { 
        Write-Verbose "Add Users to the group....."

        NET LOCALGROUP "$($this.GroupName)" "$($this.Users)" /add
          
    }
}