Convertfrom-JSON求助

Convertfrom-JSON assistance

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

我会根据上面之前的评论来尝试一下。我不得不对您的 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 {

}