Stephanevg / PowerShellClassesSeries2

Contains code related to the powershell classes series hosted on PowershellDistrict.Com
MIT License
2 stars 0 forks source link

Enums with spaces #2

Open furieuxjk opened 6 years ago

furieuxjk commented 6 years ago

Hello!

First of all - thank you for great tutorials online! I am beginner and learning how to use Classes in PowerShell.

I have AD groups with spaces in sAMAccountName like "AD Group 1" and also I have AD groups with dots like "My.ADgroup.rw", but Enum doesn't allow to use spaces or dots. How to handle this situation?

Regards, F

Stephanevg commented 6 years ago

Hi @furieuxjk That is true. Enum can only contain Constants. There would be several solutions to your problem:

Something like this:


Enum Groups{
"ADGroup1" = 1
"MyADgrouprw" =2
}

#CSVFile

ID;SamACcountName;EnumName
1;""AD Group 1";ADGroup1"
2;My.ADgroup.rw;MyADgrouprw

Of course, you will have to create that list in the first place, but should not be too complicated using PowerShell ;)

furieuxjk commented 6 years ago

Thanks for reply!

In the end I did this:

in main Class I have this string array defined: [string[]]$BaseGroupsNewEmployee

then in Constructor I have these two AD groups defined: $this.BaseGroupsNewEmployee = "AD Group 1", "My.ADGroup.rw"

then in Method I have this: [void]addToBaseGroupsNewEmployee() { foreach ($group in $this.BaseGroupsNewEmployee) { Add-ADPrincipalGroupMembership -Identity $this.UserName -MemberOf $this.BaseGroupsNewEmployee } }

And then just call $whateverIsHere.addToBaseGroupsNewEmployee()