Home > Net >  Convertfrom-JSON assistance
Convertfrom-JSON assistance

Time:12-04

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": "[email protected]",
                    "role": "Owner"
                },
                {
                    "email": "[email protected]",
                    "role": "Member"
                },
                {
                    "email": "[email protected]",
                    "role": "Member"
                }
            ],
            "channels": [
                {
                    "displayName": "Systems",
                    "membershipType": "Standard"
                },
                {
                    "displayName": "Dev",
                    "membershipType": "Standard"
                },
                {
                    "displayName": "Suport",
                    "membershipType": "Standard",
                    "users": [
                        {
                            "email": "[email protected]",
                            "role": "Owner"
                        }
                    ]
                }
            ]
        }
    ]
}

CodePudding user response:

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 {

}
  • Related