Powershell n00b here, option 1 works perfectly but I can’t understand why this switch statement doesn’t work when I choose option 2. Is this because I’m reusing variables or something stupid? If I insert $defaultgroups.defaultPPFPOgroups somewhere in the second switch statement I can see that it reads the values from the second column of the CSV file. It just will not add those values to the member.<\/p>\n
$defaultgroups = Import-Csv .\\ADGroupsCSV.csv\n \n function Show-Menu\n{\n param (\n [string]$Title = 'Add AD Groups to user'\n )\n Clear-Host\n Write-Host \"================ $Title ================\"\n \n Write-Host \"1: Press '1' to add default WH Groups.\"\n Write-Host \"2: Press '2' to add default OT Groups.\"\n Write-Host \"3: Press '3' to add Forest specific groups.\"\n Write-Host \"Q: Type 'Q' to quit.\"\n}\n\ndo\n {\n Show-Menu\n $selection = Read-Host \"Please make a selection\"\n try{\n switch ($selection)\n {\n '1' \n {\n $Member = read-host -Prompt \"Enter AD username, for example JohnS\"\n try{$User = Get-ADUser -Identity $Member}\n catch{Write-Warning 'Enter a valid username'\n Start-Sleep -Milliseconds 1000\n break} \n \n $ADGroups = foreach($Group in $defaultgroups.defaultWHgroups){ \n Get-ADGroup -Identity $Group\n Write-Progress -Activity \"Adding $Group to $User\"\n start-sleep -Milliseconds 200 \n }\n Add-ADPrincipalGroupMembership -Identity $member -MemberOf $ADGroups \n Write-Host \"Finished adding groups to user $Member\" -ForegroundColor green -BackgroundColor black \n }\n \n '2' \n {\n $Member = read-host -Prompt \"Enter AD username, for example JohnS\"\n try{$User = Get-ADUser -Identity $Member}\n catch{Write-Warning 'Enter a valid username'\n Start-Sleep -Milliseconds 1000\n break} \n \n $ADGroups = foreach($Group in $defaultgroups.defaultPPFPOgroups){\n\n Get-ADGroup -Identity $Group\n Write-Progress -Activity \"Adding $Group to $User\"\n start-sleep -Milliseconds 200 \n }\n Add-ADPrincipalGroupMembership -Identity $member -MemberOf $ADGroups \n Write-Host \"Finished adding groups to user $Member\" -ForegroundColor green -BackgroundColor black\n Start-Sleep -Milliseconds 1000 \n\n \n } \n '3' {\n 'Option #3'\n }\n }\n }\n catch{Write-Warning 'Enter a valid username.'}\n }\n until ($selection -eq 'q')\n Write-Host \"You have chosen to quit.\"-ForegroundColor green -BackgroundColor black\n<\/code><\/pre>","upvoteCount":5,"answerCount":6,"datePublished":"2021-11-22T02:12:12.000Z","author":{"@type":"Person","name":"spiceuser-5aro4","url":"https://community.spiceworks.com/u/spiceuser-5aro4"},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
Powershell n00b here, option 1 works perfectly but I can’t understand why this switch statement doesn’t work when I choose option 2. Is this because I’m reusing variables or something stupid? If I insert $defaultgroups.defaultPPFPOgroups somewhere in the second switch statement I can see that it reads the values from the second column of the CSV file. It just will not add those values to the member.<\/p>\n
$defaultgroups = Import-Csv .\\ADGroupsCSV.csv\n \n function Show-Menu\n{\n param (\n [string]$Title = 'Add AD Groups to user'\n )\n Clear-Host\n Write-Host \"================ $Title ================\"\n \n Write-Host \"1: Press '1' to add default WH Groups.\"\n Write-Host \"2: Press '2' to add default OT Groups.\"\n Write-Host \"3: Press '3' to add Forest specific groups.\"\n Write-Host \"Q: Type 'Q' to quit.\"\n}\n\ndo\n {\n Show-Menu\n $selection = Read-Host \"Please make a selection\"\n try{\n switch ($selection)\n {\n '1' \n {\n $Member = read-host -Prompt \"Enter AD username, for example JohnS\"\n try{$User = Get-ADUser -Identity $Member}\n catch{Write-Warning 'Enter a valid username'\n Start-Sleep -Milliseconds 1000\n break} \n \n $ADGroups = foreach($Group in $defaultgroups.defaultWHgroups){ \n Get-ADGroup -Identity $Group\n Write-Progress -Activity \"Adding $Group to $User\"\n start-sleep -Milliseconds 200 \n }\n Add-ADPrincipalGroupMembership -Identity $member -MemberOf $ADGroups \n Write-Host \"Finished adding groups to user $Member\" -ForegroundColor green -BackgroundColor black \n }\n \n '2' \n {\n $Member = read-host -Prompt \"Enter AD username, for example JohnS\"\n try{$User = Get-ADUser -Identity $Member}\n catch{Write-Warning 'Enter a valid username'\n Start-Sleep -Milliseconds 1000\n break} \n \n $ADGroups = foreach($Group in $defaultgroups.defaultPPFPOgroups){\n\n Get-ADGroup -Identity $Group\n Write-Progress -Activity \"Adding $Group to $User\"\n start-sleep -Milliseconds 200 \n }\n Add-ADPrincipalGroupMembership -Identity $member -MemberOf $ADGroups \n Write-Host \"Finished adding groups to user $Member\" -ForegroundColor green -BackgroundColor black\n Start-Sleep -Milliseconds 1000 \n\n \n } \n '3' {\n 'Option #3'\n }\n }\n }\n catch{Write-Warning 'Enter a valid username.'}\n }\n until ($selection -eq 'q')\n Write-Host \"You have chosen to quit.\"-ForegroundColor green -BackgroundColor black\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2021-11-22T02:12:12.000Z","url":"https://community.spiceworks.com/t/cannot-figure-this-switch-statement-out/817613/1","author":{"@type":"Person","name":"spiceuser-5aro4","url":"https://community.spiceworks.com/u/spiceuser-5aro4"}},{"@type":"Answer","text":"
Advertisement
At first glance looks ok without me trying to run the code.<\/p>\n
Any chance you have the column header spelled wrong on 2?<\/p>\n
If you do the following, do you get a group list as expected?<\/p>\n
$defaultgroups = Import-Csv .\\ADGroupsCSV.csv\n$defaultgroups.defaultPPFPOgroups\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-11-22T02:21:52.000Z","url":"https://community.spiceworks.com/t/cannot-figure-this-switch-statement-out/817613/2","author":{"@type":"Person","name":"PatrickFarrell","url":"https://community.spiceworks.com/u/PatrickFarrell"}},{"@type":"Answer","text":"I double checked the spelling and it all looks correct. I do get the expected output when I run the code below. I’ve been staring at it for hours trying to figure this out but I’m stuck!<\/p>\n
$defaultgroups = Import-Csv .\\ADGroupsCSV.csv\n$defaultgroups.defaultPPFPOgroups\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-11-22T02:27:32.000Z","url":"https://community.spiceworks.com/t/cannot-figure-this-switch-statement-out/817613/3","author":{"@type":"Person","name":"spiceuser-5aro4","url":"https://community.spiceworks.com/u/spiceuser-5aro4"}},{"@type":"Answer","text":"When i am doing this type of thing i make sure that my switch statement is working. so what i would suggest is do the following.<\/p>\n
$defaultgroups = Import-Csv .\\ADGroupsCSV.csv\n \n function Show-Menu\n{\n param (\n [string]$Title = 'Add AD Groups to user'\n )\n Clear-Host\n Write-Host \"================ $Title ================\"\n \n Write-Host \"1: Press '1' to add default WH Groups.\"\n Write-Host \"2: Press '2' to add default OT Groups.\"\n Write-Host \"3: Press '3' to add Forest specific groups.\"\n Write-Host \"Q: Type 'Q' to quit.\"\n}\n\ndo\n {\n Show-Menu\n $selection = Read-Host \"Please make a selection\"\n try{\n switch ($selection)\n {\n '1' \n {\n 'Option #1' \n '2' \n {\n 'Option #2'\n \n } \n '3' {\n 'Option #3'\n }\n }\n }\n catch{Write-Warning 'Enter a valid username.'}\n }\n until ($selection -eq 'q')\n Write-Host \"You have chosen to quit.\"-ForegroundColor green -BackgroundColor black\n<\/code><\/pre>\nThen test all three options and if they work you know it isn’t your switch statement.<\/p>\n
Then slowly start to add in your code for option 2 until it works how you want it then you know you have it right. Best way i have found to debug something.<\/p>\n
Alternatively, if you know option 1 is working then swap the code from 2 into 1 and run it, it is working? No? Then your script is wrong and it will never work.<\/p>","upvoteCount":1,"datePublished":"2021-11-22T11:11:44.000Z","url":"https://community.spiceworks.com/t/cannot-figure-this-switch-statement-out/817613/4","author":{"@type":"Person","name":"deanmoncaster","url":"https://community.spiceworks.com/u/deanmoncaster"}},{"@type":"Answer","text":"
It appears that the fundamental switch statement works fine. The second switch statement is identical to the first except it is selecting the second column in the csv. For some reason the second switch statement does not complete before returning to the menu function. Thanks for all the help so far. Any more ideas?<\/p>","upvoteCount":0,"datePublished":"2021-11-22T19:56:34.000Z","url":"https://community.spiceworks.com/t/cannot-figure-this-switch-statement-out/817613/5","author":{"@type":"Person","name":"spiceuser-5aro4","url":"https://community.spiceworks.com/u/spiceuser-5aro4"}},{"@type":"Answer","text":"
First, isolate the step that is failing. I pasted the script into VS Code an ran it after commenting out relevant sections. Your issue probably lies in this section:<\/p>\n
$ADGroups = foreach ($Group in $defaultgroups.defaultPPFPOgroups) {\n Get-ADGroup -Identity $Group\n Write-Progress -Activity \"Adding $Group to $User\"\n start-sleep -Milliseconds 200 \n }\n Add-ADPrincipalGroupMembership -Identity $member -MemberOf $ADGroups\n<\/code><\/pre>\nPossibly an uncaught exception due to a misspelled group name in the CSV. Use a Try-Catch block here too.<\/p>\n
As an aside, I will point out some issues (personal opinions coming, your mileage may vary):<\/p>\n
Your message “Adding $Group to $User” is misleading, it doesn’t happen until after that loop finishes.<\/p>\n
Don’t slow things down with sleep when it’s not necessary.<\/p>\n
The Get-ADGroup loops are unnecessary, Add-ADPrincipalGroupMembership will take the SAM account names happily. If you still prefer to verify the groups exist do it once at the beginning of the function. No need to repeat that step (and the code) in every branch of the switch. The last also applies to getting the user name, move that to a helper function (like Show-Menu).<\/p>\n
I recommend you do this “THE POWERSHELL WAY!” (tongue very much in cheek ;). Don’t create a text menu. Instead you should look into Advanced Functions<\/a> . They basically allow you to create commandlets in PowerShell, without having to program in a .Net language. You can even set it up to accept input from the pipeline, allowing you to do something like this:<\/p>\njdoe, tcat, jmouse | Add-CompanyGroups -DefaultWHGroups -DefaultOTGroups -SpecifcGroups remote, sales, hq\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-11-22T22:44:50.000Z","url":"https://community.spiceworks.com/t/cannot-figure-this-switch-statement-out/817613/6","author":{"@type":"Person","name":"tulioarends","url":"https://community.spiceworks.com/u/tulioarends"}}]}}