locked
I get Unexpected Token '{" for the switch sentence RRS feed

  • Question

  • I am trying to use switch sentence to execute add-adprincipalgroupmembership command. I am passing a $Roles[] to switch construct. But I am getting the error:

    I get Unexpected Token '{" for the switch sentences for the "{" in the switch statements like in:

    nexpected token '{' in expression or statement.
    At C:\Users\okadhim\Documents\Get-SystemAccess.ps1:55 char:12
    + "ReadOnly"    {Add-ADPrincipalGroupMembership -Identity $Username -Me ...


    Function Add-DP3Access{
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$TRUE)][String]$Username,

    [Parameter(Mandatory=$TRUE)][String[]]$Roles
    )
    Begin{}
    Process{


    Swich ($Roles)
    {

    "Admin"{Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3Admin}

    "TeamLeader" {Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3TeamLeader}

    "AcceptanceRep"{Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3AcceptanceRep}

    "HardshipRep"{Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3HardshipRep}

    "Fraud"{Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3Fraud}

    "AtlasSystemAccount"{Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3AtlasSysAcc}

    "ReadOnly" {Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3ReadOnly}

    "FleetUser" {Add-ADPrincipalGroupMembership -Identity $uSERNAME -MemberOf gapDP3FleetUser}

    "FleetManager" {Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3FleetManager}

    "CustSolTeamLeader" {Add-ADPrincipalGroupMembership -Identity $Username -MemberOf gapDP3CustSolTeamLeader}
    }
    }
    END{}


    Wednesday, August 23, 2017 3:08 AM

Answers

  • A slightly more efficient way to do this:

    Function Add-DP3Access {
    	[CmdletBinding()]
    	Param (
    		[Parameter(Mandatory)]
    		[String]$Username,
    		[Parameter(Mandatory)]
    		[ValidateSet('TeamLeader', 'AcceptanceRep', 'HardshipRep', 'Fraud', 'AtlasSystemAccount', 'ReadOnly', 'FleetUser', 'FleetManager', 'CustSolTeamLeader')]
    		[String[]]$Roles
    	)
    	Begin{
    		$groupnames = @{
    			TeamLeader = 'gapDP3TeamLeader'
    			AcceptanceRep = 'gapDP3AcceptanceRep'
    			HardshipRep = 'gapDP3HardshipRep'
    			Fraud = 'gapDP3Fraud'
    			AtlasSystemAccount = 'gapDP3AtlasSysAcc'
    			ReadOnly = 'gapDP3ReadOnly'
    			FleetUser = 'gapDP3FleetUser'
    			FleetManager = 'gapDP3FleetManager'
    			CustSolTeamLeader = 'gapDP3CustSolTeamLeader'
    		}
            }
    	Process {
    		foreach($role in $Roles){
    			Add-ADPrincipalGroupMembership -Identity $Username -MemberOf $groupnames[$role]
    		}
    	}
    }


    \_(ツ)_/



    • Edited by jrv Wednesday, August 23, 2017 3:41 AM
    • Marked as answer by Simsim1981 Wednesday, August 23, 2017 4:56 AM
    • Unmarked as answer by Simsim1981 Wednesday, August 23, 2017 4:56 AM
    • Marked as answer by Simsim1981 Wednesday, August 23, 2017 5:52 AM
    Wednesday, August 23, 2017 3:40 AM

All replies

  • What is a switch "sentence".  Do you mean a switch "statement?

    If you format your code correctly it will be easier to find syntax errors.


    \_(ツ)_/

    Wednesday, August 23, 2017 3:24 AM
  • Here is how to best use a switch statement.  It is concise and easy to read.  It does exactly the same thing but also enumerate the "$Roles" array  which is necessary.

    Function Add-DP3Access {
    	[CmdletBinding()]
    	Param (
    		[Parameter(Mandatory)]
    		[String]$Username,
    		[Parameter(Mandatory)]
    		[String[]]$Roles
    	)
    	
    	Process {
    		
    		foreach($role in $Roles){
    			$groupname = switch ($Roles) {
    				Admin { 'gapDP3Admin' }
    				TeamLeader { 'gapDP3TeamLeader' }
    				AcceptanceRep { 'gapDP3AcceptanceRep' }
    				HardshipRep { 'gapDP3HardshipRep' }
    				Fraud { 'gapDP3Fraud' }
    				AtlasSystemAccount { 'gapDP3AtlasSysAcc' }
    				ReadOnly { 'gapDP3ReadOnly' }
    				FleetUser { 'gapDP3FleetUser' }			
    				FleetManager { 'gapDP3FleetManager' }
    				CustSolTeamLeader { 'gapDP3CustSolTeamLeader' }
    			}
    			Add-ADPrincipalGroupMembership -Identity $Username -MemberOf $groupname
    		}
    	}
    }


    \_(ツ)_/


    • Edited by jrv Wednesday, August 23, 2017 3:32 AM
    Wednesday, August 23, 2017 3:32 AM
  • A slightly more efficient way to do this:

    Function Add-DP3Access {
    	[CmdletBinding()]
    	Param (
    		[Parameter(Mandatory)]
    		[String]$Username,
    		[Parameter(Mandatory)]
    		[ValidateSet('TeamLeader', 'AcceptanceRep', 'HardshipRep', 'Fraud', 'AtlasSystemAccount', 'ReadOnly', 'FleetUser', 'FleetManager', 'CustSolTeamLeader')]
    		[String[]]$Roles
    	)
    	Begin{
    		$groupnames = @{
    			TeamLeader = 'gapDP3TeamLeader'
    			AcceptanceRep = 'gapDP3AcceptanceRep'
    			HardshipRep = 'gapDP3HardshipRep'
    			Fraud = 'gapDP3Fraud'
    			AtlasSystemAccount = 'gapDP3AtlasSysAcc'
    			ReadOnly = 'gapDP3ReadOnly'
    			FleetUser = 'gapDP3FleetUser'
    			FleetManager = 'gapDP3FleetManager'
    			CustSolTeamLeader = 'gapDP3CustSolTeamLeader'
    		}
            }
    	Process {
    		foreach($role in $Roles){
    			Add-ADPrincipalGroupMembership -Identity $Username -MemberOf $groupnames[$role]
    		}
    	}
    }


    \_(ツ)_/



    • Edited by jrv Wednesday, August 23, 2017 3:41 AM
    • Marked as answer by Simsim1981 Wednesday, August 23, 2017 4:56 AM
    • Unmarked as answer by Simsim1981 Wednesday, August 23, 2017 4:56 AM
    • Marked as answer by Simsim1981 Wednesday, August 23, 2017 5:52 AM
    Wednesday, August 23, 2017 3:40 AM