I'm trying to make a simple Powershell script to add a user to few groups determined by .csv file for new users and stuff. I took for help this article : https://www.sharepointdiary.com/2021/08/bulk-add-users-to-microsoft-teams-using-powershell.html?
I may be mistaking with .csv/ForEach/Try all together.
The error I get is "-GroupID" in Add-TeamUser gets a "null" so it won't work. Replacing "$GrpName" to an actual group name in the Get-Team command works. So the problem must be the .csv reading... I guess.'
\#Connect Microsoft Teams
Connect-MicrosoftTeams
\#Select .csv with browsing
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = \[Environment\]::GetFolderPath('Desktop')
Filter = "CSV (\*.csv) | \*.csv"
}
$null = $FileBrowser.ShowDialog()
$CSVPath = $FileBrowser.FileName
$inputHeaders = 'Group', 'Role'
\#Select User name
$UserTeams = Read-Host "Name of User"
\#Import Groups by .csv
$Import = Import-Csv -Path $CSVPath -Delimiter ";" -Header $inputHeaders| ForEach-Object {
\#Transform group name into group ID
$GrpName = $Import.Group
$TeamID = Get-Team | Where {$\_.DisplayName -eq $GrpName} | Select -ExpandProperty GroupID
$Role = $Import.Role
\#Add User to the Groups
Add-TeamUser -GroupId $TeamID -User $UserTeams -Role $Role
}