我正在尝试编写一个脚本来从 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"
}
]
}
]
}
]
}