365 Group Script

Welcome Forums General PowerShell Q&A 365 Group Script

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

 
Participant
1 week, 1 day ago.

  • Author
    Posts
  • #134478

    Participant
    Points: 42
    Rank: Member

    Hi All

    I am now been tasked with creating a script that adds 365 groups to a user.

     

    Below this is what have started with.

    For ease of starting this I have it reading from host, for testing purpose, which is why I have it using $User to set user for the group.

     

    #Sets Variables
    $email = Read-Host "Enter Email Address"
    
    #Sets Credentials for logon
    $Username = "Username"
    $Password = ConvertTo-SecureString 'Password' -AsPlainText -Force
    $Credential = New-Object System.Management.Automation.PSCredential $Username, $Password
    
    #Connects to 365
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
    Import-PSSession $Session
    
    #Connects to AzureAD
    Connect-MsolService -Credential $Credential 
    Import-Module MSonline
    
    #License check(Get-MsolUser -UserPrincipalName [INSERT EMAIL]).Licenses.ServiceStatus
    #Gets User
    $User = Get-MsolUser -SearchString "$email" | Select DisplayName
    
    #Loop to check if user Exists
    Do 
    {
    $UserCheckValid = Get-MsolUser -UserPrincipalName $email -ErrorAction SilentlyContinue
    Sleep 10
    }
    While ($UserCheckValid -eq $null)
    
    #Add Generic Groups
    
    #Adds 365 Group
    Add-UnifiedGroupLinks –Identity "365 Group" –LinkType Members –Links "$User"
    
    Remove-PSSession $Session

    But the output I'm getting is this Output:

    Couldn't find object "@{UserPrincipalName=First.Test@company.com}". Please make sure that it was spelled correctly or specify a 
    different object.
    + CategoryInfo : NotSpecified: (365 Group:UnifiedGroupIdParameter) [Add-UnifiedGroupLinks], ADNoSuchObjectException
    + FullyQualifiedErrorId : [Server=AM0PR01MB4628,RequestId=fc5cf0d5-8d37-4203-942d-7d1337cf0d1f,TimeStamp=14/01/2019 14:10:56] 
    [FailureCategory=Cmdlet-ADNoSuchObjectException] 48886C4E,Microsoft.Exchange.Management.RecipientTasks.AddUnifiedGroupLinks
    + PSComputerName : outlook.office365.com

    I'm think it might be to do with $User Variable, so I changed $User to Get-MsolUser -UserPrincipalName "$email" & Get the following Output:

    Couldn't find object "Microsoft.Online.Administration.User". Please make sure that it was spelled correctly or specify a different 
    object.
    + CategoryInfo : NotSpecified: (365 Group:UnifiedGroupIdParameter) [Add-UnifiedGroupLinks], ADNoSuchObjectException
    + FullyQualifiedErrorId : [Server=AM0PR01MB4628,RequestId=ee4909bf-0e42-45c1-ad9e-a7bd96538e36,TimeStamp=14/01/2019 14:30:45] 
    [FailureCategory=Cmdlet-ADNoSuchObjectException] 4E6866B8,Microsoft.Exchange.Management.RecipientTasks.AddUnifiedGroupLinks
    + PSComputerName : outlook.office365.com

     

    I'm just unsure on why. More eye would help

  • #134513

    Participant
    Points: 1,124
    Helping Hand
    Rank: Community Hero

    What are you giving to $email variable ? The second failure is expected as warping inside double quotes will do a string conversion of the variable and for many of the objects string conversion gives the type.

  • #134523

    Participant
    Points: -5
    Rank: Member

    The $User variable being set by Get-MsolUser is a Microsoft.Online.Administration.User object.  In general, I would expect that the -Links parameter of Add-UnifiedGroupLinks would accept that variable without any quotes around it and use the correct property from the object to identify who you're adding to the group.  If, for some reason, it doesn't work with the full $User object, try using $User.UserPrincipalName (again, with no quotes) to pass in the UPN property.

You must be logged in to reply to this topic.