1

I am trying to put together a script to create a Microsoft Team from a JSON file. I am trying to understand Convertfrom-JSON and how I can pipe the object into creating a new team with channels and users. I can view the object by entering in $json.teams. why does $_.Displayname work? I know that is what is in the pipeline.

function New-MSteam {

    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [string]$TeamsFilePath
    )

    begin {
      
        #checking for Microsoft Teams Module
        Write-Verbose "Importing modules"
        $Module = Get-Module -Name MicrosoftTeams -ListAvailable
        if ($Module.Count -eq 0) {
            Write-Verbose "Installing MicrosoftTeams module"
            Install-Module -Name MicrosoftTeams -AllowPrerelease -AllowClobber -Force
        }
  
        Connect-MicrosoftTeams

    }

    process {
        #Converting JSON
        $json = Get-Content  -Path c:\salest.json | ConvertFrom-Json
        $json.teams | ForEach-Object {
            $_.gettype().Team
        }

        #creating New team
        $NewTeam = New-Team -DisplayName $teams.DisplayName -Visibility $teams.Visibility
        $NewTeam.Users | ForEach-Object { $_.email
            Add-TeamUser -User $_.email -Role $_.Role -GroupId $NewTeam.GroupId
        }

        $Team.Channels | ForEach-Object
        New-TeamChannel -DisplayName $_.DisplayName -MembershipType $_.MembershipType -GroupId $NewTeam.GroupId
                      
    }
              
}

end {

}
{
    "teams": [
        {
            "displayName": "IT Team",
            "visibility": "Public",
            "users": [
                {
                    "email": "admin@3q4kz.onmicrosoft.com",
                    "role": "Owner"
                },
                {
                    "email": "AlexW@3q4kz.onmicrosoft.com",
                    "role": "Member"
                },
                {
                    "email": "GradyA@3q4kz.onmicrosoft.com",
                    "role": "Member"
                }
            ],
            "channels": [
                {
                    "displayName": "Systems",
                    "membershipType": "Standard"
                },
                {
                    "displayName": "Dev",
                    "membershipType": "Standard"
                },
                {
                    "displayName": "Suport",
                    "membershipType": "Standard",
                    "users": [
                        {
                            "email": "admin@3q44z.onmicrosoft.com",
                            "role": "Owner"
                        }
                    ]
                }
            ]
        }
    ]
}
WaitingForGuacamole
  • 3,744
  • 1
  • 8
  • 22
Derek
  • 37
  • 5

1 Answers1

0

I'll take a shot at this based on previous comments above. I had to do minor surgery on your JSON as it wasn't quite correct, please see above. This may not be completely correct, but perhaps it will get you closer:

function New-MSteam {

    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [string]$TeamsFilePath
    )

    begin {
      
        #checking for Microsoft Teams Module
        Write-Verbose "Importing modules"
        $Module = Get-Module -Name MicrosoftTeams -ListAvailable
        if ($Module.Count -eq 0) {
            Write-Verbose "Installing MicrosoftTeams module"
            Install-Module -Name MicrosoftTeams -AllowPrerelease -AllowClobber -Force
        }
  
        Connect-MicrosoftTeams

    }

    process {
        # Convert JSON
        $json = Get-Content -Path c:\salest.json | ConvertFrom-Json

        # Iterate teams property in JSON
        $json.teams | ForEach-Object {
            # Create New team
            $NewTeam = New-Team -DisplayName $_.DisplayName -Visibility $_.Visibility
            
            # Iterate the users in JSON, adding them to the team just created
            $_.Users | ForEach-Object { 
                Add-TeamUser -User $_.email -Role $_.Role -GroupId $NewTeam.GroupId
            }

            # Iterate the channels in JSON
            $_.Channels | ForEach-Object {
                New-TeamChannel -DisplayName $_.DisplayName -MembershipType $_.MembershipType -GroupId $NewTeam.GroupId
            }                  
        }          
    }
}

end {

}
WaitingForGuacamole
  • 3,744
  • 1
  • 8
  • 22
  • So that did create the team but did not add the users or create the channels. – Derek Dec 03 '21 at 16:01
  • TeamUser: Line | 8 | … Add-TeamUser -User $_.email -Role $_.Role -GroupId $NewTe … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Error occurred while executing Code: Request_ResourceNotFound Message: Resource 'admin@3q44kz.onmicrosoft.com' does not exist or one of its queried reference-property objects are not present. InnerError: RequestId: 1d86ddb9-4fa0-475b-9baf-bdad73d4474d DateTimeStamp: 2021-12-03T15:59:34 HttpStatusCode: Request_ResourceNotFound – Derek Dec 03 '21 at 16:02
  • That error suggests your user doesn't exist in the current tenant. Is it somehow a guest user? – WaitingForGuacamole Dec 03 '21 at 16:08
  • 1
    yes, I just figured this out. i was selected our prod tenant and not dev tenant. it works. Thank you! – Derek Dec 03 '21 at 16:11
  • So just putting the ForEach-Object goes through each item in the json? – Derek Dec 03 '21 at 16:49
  • Yep, goes through each item in the object to the left of the pipe. In your case, the teams property of the JSON, and then within each team, the users and the channels. – WaitingForGuacamole Dec 03 '21 at 16:51
  • thank you so much for explaining. it makes sense now. – Derek Dec 03 '21 at 16:54