1

我正在尝试编写一个脚本来从 JSON 文件创建 Microsoft Team。我正在尝试了解 Convertfrom-JSON 以及如何将对象通过管道和用户创建一个新团队。我可以通过输入 $json.teams 来查看对象。为什么 $_.Displayname 工作?我知道这就是正在酝酿中的事情。

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"
                        }
                    ]
                }
            ]
        }
    ]
}
4

1 回答 1

0

我将根据上面之前的评论对此进行尝试。我不得不对您的 JSON 进行小手术,因为它不太正确,请参见上文。这可能不完全正确,但也许它会让你更接近:

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 {

}
于 2021-12-03T15:47:34.003 回答