Calling Variable in {} does not work

Welcome Forums General PowerShell Q&A Calling Variable in {} does not work

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
9 months, 2 weeks ago.

  • Author
    Posts
  • #92554

    Participant
    Points: 21
    Rank: Member

    Hi Everyone,

    Can someone please help me with calling variable in below-mentioned code. Appreciate your assistance.

    Function RecipientScope
    {

    $MMARSCode = Read-Host "Type the 3 Character code of Agency"
    $MMARS = ("'" + $MMARSCode + "'")
    $Id = ("Recipient Scope – "+ $MMARSCode)
    $Identity = $Id
    $DMailbox = ("'" + "eDiscoveryMailbox-"+ $MMARS + "'")
    $DiscoverMailbox = $DMailbox
    $DisList = ("'" + $MMARS + "*" + "'")
    $DL = $DisList

    Try

    {

    Set-ManagementScope -Identity $Identity -RecipientRestrictionFilter {((((((((((RecipientTypeDetails -eq 'UserMailbox') -and (CustomAttribute12 -eq "$MMARS"))) -or (((RecipientTypeDetails -eq 'RoomMailbox') -and (CustomAttribute12 -eq "$MMARS"))))) -or (((RecipientTypeDetails -eq 'EquipmentMailbox') -and (CustomAttribute12 -eq "$MMARS"))))) -or (((RecipientTypeDetails -eq 'DiscoveryMailbox') -and (DisplayName -eq "$DiscoverMailbox"))))) -or (((RecipientTypeDetails -eq 'SharedMailbox') -and (CustomAttribute12 -eq "$MMARS"))) -or (((RecipientTypeDetails -eq 'MailUniversalDistributionGroup') -and (DisplayName -like "$DL"))))}

    Write-host "Scoping Admin rights for Agency $("$MMARS") for Recipient Management for $($Identity) and $($DiscoverMailbox) and DL starting with $($DL)" -ForegroundColor Green
    }

    Catch {$Error}
    }

    RecipientScope

  • #92581

    Participant
    Points: 153
    Helping Hand
    Rank: Participant

    If I counted it right you've used 9 variables in your code. Which one is it you have problems with?

    BTW: If you format your code as code here in the forum and indent it nicely it will be easier for all of us to read it, understand it and debug it if needed.

    To make your code easier to read you should think about using splatting. If you want to use a try – catch you should use the -ErrorAction Stop in the try block. Otherwise your catch block will just run when the cmdlet you use in the try block triggers a terminating error.

  • #92587

    Participant
    Points: 21
    Rank: Member

    all variables are picked up as text in command:

    Function RecipientScope
    {
    
    $MMARSCode = Read-Host "Type the 3 Character code of Agency"
    $MMARS = ("'" + $MMARSCode + "'")
    $Id = ("Recipient Scope – "+ $MMARSCode)
    $Identity = $Id
    $DMailbox = ("'" + "eDiscoveryMailbox-"+ $MMARS + "'")
    $DiscoverMailbox = $DMailbox
    $DisList = ("'" + $MMARS + "*" + "'")
    $DL = $DisList
    
    Try
    
    {
    
    Set-ManagementScope -Identity $Identity -RecipientRestrictionFilter {((((((((((RecipientTypeDetails -eq 'UserMailbox') -and (CustomAttribute12 -eq "$MMARS"))) -or (((RecipientTypeDetails -eq 'RoomMailbox') -and (CustomAttribute12 -eq "$MMARS"))))) -or (((RecipientTypeDetails -eq 'EquipmentMailbox') -and (CustomAttribute12 -eq "$MMARS"))))) -or (((RecipientTypeDetails -eq 'DiscoveryMailbox') -and (DisplayName -eq "$DiscoverMailbox"))))) -or (((RecipientTypeDetails -eq 'SharedMailbox') -and (CustomAttribute12 -eq "$MMARS"))) -or (((RecipientTypeDetails -eq 'MailUniversalDistributionGroup') -and (DisplayName -like "$DL"))))}
    
    Write-host "Scoping Admin rights for Agency $("$MMARS") for Recipient Management for $($Identity) and $($DiscoverMailbox) and DL starting with $($DL)" -ForegroundColor Green
    }
    
    Catch {$Error}
    }
    

    however, this line of command is picking up the variables just fine:

     Write-host "Scoping Admin rights for Agency $("$MMARS") for Recipient Management for $($Identity) and $($DiscoverMailbox) and DL starting with $($DL)" -ForegroundColor Green 
  • #92591

    Participant
    Points: 153
    Helping Hand
    Rank: Participant

    Ah ... I guess I know what you mean. Why do you actually use all these quoting stuff. .. and even mixed single and double quotes ... I'm pretty sure that's a little too much. Remove them and try it again.

  • #92596

    Participant
    Points: 21
    Rank: Member

    If I remove ' quotes :

    Set-ManagementScope -Identity $Identity -RecipientRestrictionFilter {((((((((((RecipientTypeDetails -eq 'UserMailbox') -and (CustomAttribute12 -eq $MMARS))) -or (((RecipientTypeDetails -eq 'RoomMailbox') -and (CustomAttribute12 -eq $MMARS))))) -or (((RecipientTypeDetails -eq 'EquipmentMailbox') -and (CustomAttribute12 -eq $MMARS))))) -or (((RecipientTypeDetails -eq 'DiscoveryMailbox') -and (DisplayName -eq $DiscoverMailbox))))) -or (((RecipientTypeDetails -eq 'SharedMailbox') -and (CustomAttribute12 -eq $MMARS))) -or (((RecipientTypeDetails -eq 'MailUniversalDistributionGroup') -and (DisplayName -like $DL))))} 

    it is putting $null

  • #92603

    Participant
    Points: 153
    Helping Hand
    Rank: Participant

    I cannot really check the functionality but I think it should work like this:

    Function RecipientScope{
    
        $MMARSCode = Read-Host "Type the 3 Character code of Agency"
        $MMARS = ("'" + $MMARSCode + "'")
        $Id = ("Recipient Scope – "+ $MMARSCode)
        $Identity = $Id
        $DMailbox = ("'" + "eDiscoveryMailbox-"+ $MMARSCode + "'")
        $DiscoverMailbox = $DMailbox
        $DisList = ("'" + $MMARSCode + "*" + "'")
        $DL = $DisList
    
        Try{
            Set-ManagementScope -Identity $Identity -RecipientRestrictionFilter {((((((((((RecipientTypeDetails -eq 'UserMailbox') -and (CustomAttribute12 -eq $MMARS))) -or (((RecipientTypeDetails -eq 'RoomMailbox') -and (CustomAttribute12 -eq $MMARS))))) -or (((RecipientTypeDetails -eq 'EquipmentMailbox') -and (CustomAttribute12 -eq $MMARS))))) -or (((RecipientTypeDetails -eq 'DiscoveryMailbox') -and (DisplayName -eq $DiscoverMailbox))))) -or (((RecipientTypeDetails -eq 'SharedMailbox') -and (CustomAttribute12 -eq $MMARS))) -or (((RecipientTypeDetails -eq 'MailUniversalDistributionGroup') -and (DisplayName -like $DL))))}  -ErrorAction Stop
            Write-host "Scoping Admin rights for Agency $MMARS for Recipient Management for '$($Identity)' and $DiscoverMailbox and DL starting with $DL" -ForegroundColor Green
        }
        Catch {
            $Error 
        }
    }

    You should really try to simplify your code. It is really confusing. I think some of your variables are superfluous. This: $Identity = $Id or this $DL = $DisList is defenitely unnecessary.

  • #92617

    Participant
    Points: 225
    Helping Hand
    Rank: Participant

    I agree with Olaf here. Why are you sending the same string output to different variables?
    If you allow out put of each line, code step though, it becomes real evident.

        ($MMARSCode = Read-Host 'Type the 3 Character code of Agency')
        Type the 3 Character code of Agency: USA
        USA
    
        ($MMARS = ("'" + $MMARSCode + "'"))
        'USA'
    
        ($Id = ("Recipient Scope – "+ $MMARSCode))
        Recipient Scope – USA
    
        ($Identity = $Id)
        Recipient Scope – USA
    
        ($DMailbox = ("'" + "eDiscoveryMailbox-"+ $MMARSCode + "'"))
        'eDiscoveryMailbox-USA'
    
        ($DiscoverMailbox = $DMailbox)
        'eDiscoveryMailbox-USA'
    
        ($DisList = ("'" + $MMARSCode + "*" + "'"))
        'USA*'
    
        ($DL = $DisList)
        'USA*'
    

    What do you feel you are gaining by this approach vs, just doing something like this...

        ($MMARSCode = Read-Host 'Type the 3 Character code of Agency')
        Type the 3 Character code of Agency: USA
        USA
    
        ($Identity = ("Recipient Scope – $MMARSCode"))
        Recipient Scope – USA
    
        ($DiscoverMailbox = ("'" + "eDiscoveryMailbox-"+ $MMARSCode + "'"))
        'eDiscoveryMailbox-USA'
    
        ($DL = ("'" + $MMARSCode + "*" + "'"))
        'USA*'
    

The topic ‘Calling Variable in {} does not work’ is closed to new replies.