我正试图编写一个脚本,从JSON文件创建一个Microsoft团队。我正在尝试理解ConvertfromJSON,以及如何通过管道将对象创建一个具有通道和用户的新团队。我可以通过输入$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"
}
]
}
]
}
]
}
我将根据前面的评论对此进行尝试。我不得不对你的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 {
}