Functions/Invoke-AdminPortalCreation.ps1


function Invoke-AdminPortalCreation {

    # Connect to partnercenter to get all tenants


    [CmdletBinding()]
    param (
        # Parameter help description
        [Parameter()] [switch] $OpenInBrowser,
        [Parameter()] [switch] $DummyData
    )


    function Save-Files {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)] [string] $FolderPath
        )


        $RelPath = Join-Path "$($PSScriptRoot)" "\..\Resources\AdminPortal" -Resolve

        $Files = Get-ChildItem $RelPath -Recurse -File

        $Files.FullName | ForEach-Object {
            # $_ -replace "$($PSScriptRoot)\..\Resources\AdminPortal", ""
            $Outpath = Join-Path $FolderPath $_.Replace($RelPath, "")
            Copy-Item $_ $Outpath -Force
        }

    }
















    function Write-CustomerRow {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)] $Customer
        )

        @"
                    <tr class=`"CustomerRow`" id="row_$($Customer.CustomerId)">
                        <td class=`"CustomerName`">
                            $($Customer.Name)
                            <button title="fav_$($Customer.CustomerId)" class="FavoriteButton" value="fav_$($Customer.CustomerId)">Favorite</button>
                        </td>
                        <td class=`"CustomerInfo`">
                            Customer Domain: $($Customer.Domain)<br><br>
                            Tenant ID: $($Customer.CustomerId)<br><br>
                            <a href=`"https://partner.microsoft.com/commerce/customers/$($Customer.CustomerId)/subscriptions`" target=`"_blank`"><img src="img/Partner.svg" alt="Partner"> Partner Center</a>
                            -
                            <a href=`"https://partner.microsoft.com/commerce/customers/$($Customer.CustomerId)/devices`" target=`"_blank`"><img src="img/Autopilot.svg" alt="Autopilot"> Devices</a><br>
                        </td>
                        <td class=`"CustomerLinks`">
                            <div>
                                <div>
                                <a href=`"https://entra.microsoft.com/$($Customer.domain)`" target=`"_blank`"><img src="img/Entra.svg" alt="Entra"> Microsoft Entra</a>
                                </div>
                                <div>
                                <a href=`"https://Intune.microsoft.com/$($Customer.domain)`" target=`"_blank`"><img src="img/Intune.svg" alt="Intune"> Intune</a>
                                </div>
                            </div>
                            <div>
                                <div>
                                <a href=`"https://portal.azure.com/$($Customer.domain)`" target=`"_blank`"><img src="img/azure.svg" alt="azure"> Azure Management Portal</a>
                                </div>
                                <div>
                                <a href=`"https://portal.office.com/Partner/BeginClientSession.aspx?CTID=$($Customer.CustomerId)&CSDEST=o365admincenter`" target=`"_blank`"><img src="img/Admin.svg" alt="Admin"> Office365 Management</a>
                                </div>
                            </div>
                            <div>
                                <div>
                                <a href=`"https://admin.exchange.microsoft.com/?delegatedOrg=$($Customer.domain)#/homepage`" target=`"_blank`"><img src="img/Exchange.svg" alt="Exchange"> Exchange Management</a>
                                </div>
                                <div>
                                <a href=`"https://$(($Customer.domain -split '\.')[0])-admin.sharepoint.com/`" target=`"_blank`"><img src="img/SharePoint.svg" alt="SharePoint"> SharePoint Management</a>
                                </div>
                            </div>
                        </td>
                    </tr>


"@

    }


    $outputPath = "C:\Temp\AdminPortal"
    # $outputPathImages = Join-Path $outputPath "img"

    if ($DummyData) {
        $CustomerData = @"
"Name","Domain","CustomerId"
"De Zon","dezon.onmicrosoft.com","e815effe-4098-47bf-8fe0-99dab45ee4ea"
"Onze planeet Aarde","onzeplaneetaarde.onmicrosoft.com","b63d4967-8f1f-4d5b-a9b6-164a4aae47e1"
"De planeet Mars","deplaneetmars.onmicrosoft.com","85620ccd-32a7-4407-924a-d7495b1ef2a3"
"De planeet Venus","deplaneetvenus.onmicrosoft.com","d24deaea-c96f-470c-8f68-a7252fc30fb9"
"De planeet Saturnus","deplaneetsaturnus.onmicrosoft.com","f1be15b4-0c75-4327-b9a5-b6a6746bfa17"
"De planeet Uranus","deplaneeturanus.onmicrosoft.com","4959e05c-3ba7-47c8-8ece-e60b9d1957ab"
"De planeet Pluto","deplaneetpluto.onmicrosoft.com","48273dfb-b310-4e28-ae67-9b01f92f9b12"
"Een Ster","eenster.onmicrosoft.com","3debf136-6f12-4eac-ab3a-e0bba1de5cd6"
"De planeet Jupiter","deplaneetjupiter.onmicrosoft.com","b5ee263e-8207-4ab9-84b3-2ed8c5fa37aa"
"Zonnestelsel","zonnestelsel.onmicrosoft.com","1ec35423-62f8-403b-857f-b3de752edeac"
"Het zwarte gat","hetzwartegat.onmicrosoft.com","edc912a9-5420-48dc-b010-ec236ec3d67e"
"De Maan","demaan.onmicrosoft.com","fcad9d10-75a1-43b6-bdc9-53948eea317b"
"Een andere maan van een andere planeet","eenanderemaanvaneenandereplaneet.onmicrosoft.com","a695488e-c09e-43d9-ba18-4d720712b60d"
"Mercurius","mercurius.onmicrosoft.com","ece6170a-e387-474f-b777-36920576a2e2"
"Oerknal","oerknal.onmicrosoft.com","4aca4736-bb84-4d72-b6bd-497ee110710a"
"@

        $Customers = ConvertFrom-Csv $CustomerData | Sort-Object Name
    } else {
        if (!(Get-PartnerContext)) {
            Connect-PartnerCenter -UseDeviceAuthentication
        }

        $Customers = Get-PartnerCustomer | Sort-Object -Property Name
        if ($Customers.Count -eq 0) {
            throw "no customers found"
        }
    }

    $sb = [System.Text.StringBuilder]::new()

    $baseHtml = @"
<!DOCTYPE html>
<html lang=nl>

<head>
    <title>Parter Admin Portal</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="style.css">


</head>

<body>

    <main class="table">

        <div class="table_div" id="fav_div">

            <section class="table__header">
                <h1>Favorites</h1>
            </section>

            <section class="table__body">
                <table class="Customers FavoriteCustomers">
                    <tbody id="fav_table_body">

                    </tbody>
                </table>
            </section>
        </div>

        <div class="table_div">

            <section class="table__header">
                <h1>All Customers</h1>
                <div class="searchbox">
                    <input type="text" name="CustomerSearch" id="CustomerSearch" placeholder="Press 'F' to search...">
                </div>
                <div class="date"><a href="https://partner.microsoft.com/en-us/dashboard/commerce2/customers/list" target="_blank">Partner Center</a></div>
                <div class="date">Page generated on: $(Get-Date -Format "yyyy-MM-dd HH:mm")</div>
            </section>

            <section class="table__body">
                <table class="Customers">
                    <tbody>

        $( $Customers | ForEach-Object {
            # if($Favorites -notcontains $_.Domain) {
                Write-Verbose $_.Name
                Write-CustomerRow -Customer $_
            # }
        } )

                    </tbody>
                </table>
            </section>
        </div>
    </main>
    <script src="script.js"></script>
</body>

</html>
"@



    $sb.AppendLine($baseHtml) | Out-Null

    if (-NOT (Test-Path -Path $outputPath)) {
        Write-Output "Creating directory: $outputPath"
        New-Item -Path $outputPath -ItemType "Directory" -Force
    }

    Save-Files -FolderPath $outputPath

    $OutFile = Join-Path -Path $outputPath -ChildPath "AdminPortal.html"

    $sb.ToString() | Out-File $OutFile

    $OutFile

    if ($OpenInBrowser) {
        Start-Process "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" -ArgumentList "$OutFile"
    }

    $Customers | ConvertTo-Json | Out-File (Join-Path -Path $outputPath -ChildPath "PartnerTenants.json")

}