cEPRSGroupCreation.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
          
    }
}