首页 > 解决方案 > 3 Graph API Endpoints - 匹配后将列从一个插入到另一个

问题描述

目前,我正在查询 2 个不同的 Graph API 端点,从用户端点获取 M365 活动用户详细信息以及用户详细信息,并将 userPrincipalName 与employeeNumber 匹配。employeeNumber 是一个当前是扩展属性的值。

我正在尝试查询 3rd Graph API 端点以获取 Office 激活详细信息:

https://graph.microsoft.com/v1.0/reports/getOffice365ActivationsUserDetail

目前的逻辑如下所示:

#queries
$graphApiUri = "https://graph.microsoft.com/v1.0/reports/getOffice365ActiveUserDetail(period='D90')"
$activationsUri = "https://graph.microsoft.com/v1.0/reports/getOffice365ActivationsUserDetail"

$Uri = "https://graph.microsoft.com/v1.0/users?`$select=userPrincipalName,extension_335d2348b8fbb5d75_employeeNumber"

Write-Output "Querying Graph API for Office activations..."

$ActivationReport = Invoke-RestMethod -Method Get -Uri $activationsUri -Headers $headerParams | ConvertFrom-Csv

# If the result is more than 999, we need to read the @odata.nextLink to show more than one side of users
$UserDetails = while (-not [string]::IsNullOrEmpty($uri)) {
    # API Call
    $apiCall = try {
        Invoke-RestMethod -Headers $headerParams -Uri $uri -Method Get
    }
    catch {
        $errorMessage = $_.ErrorDetails.Message | ConvertFrom-Json
    }
    $uri = $null
    if ($apiCall) {
        # Check if any data is left
        $uri = $apiCall.'@odata.nextLink'
        $apiCall
    }
}

$O365Report = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $headerParams | ConvertFrom-Csv
Write-Output "Matching UPN to employeeNumber..."

$O365Report | ForEach-Object {
    $CurrentEmpNumber = $UserDetails.value |
        Where-Object userPrincipalName -eq $_.'User Principal Name' |
            Select-Object -ExpandProperty extension_335d2348b8fbb5d75_employeeNumber -ErrorAction SilentlyContinue
    $_ | Add-Member -MemberType NoteProperty -Name extension_335d2348b8fbb5d75_employeeNumber -Value $CurrentEmpNumber
}

$O365Report | Export-Csv $ReportCSV -NoTypeInformation
Write-Output "Report saved to $ReportCSV."

我试图简单地抓取"Last Activation Date"从 getOffice365ActivationsUserDetail 报告中调用的列,使用 匹配它User Principal Name,然后作为单独的列插入到最终的 CSV 输出中。我不确定如何继续,因为我已经将这些成员添加为前一个 foreach 循环的一部分。两个报告都包含一个带有标题的列"User Principal Name"

对 Graph API 端点的调用如下:

$ActivationReport = Invoke-RestMethod -Method Get -Uri $activationsUri -Headers $headerParams | ConvertFrom-Csv

不确定它是否像这样简单:

$LAD = $activationReport | Select-Object 'Last Activated Date'

$O365Report | ForEach-Object {
    $CurrentEmpNumber = $UserDetails.value |
        Where-Object userPrincipalName -eq $_.'User Principal Name' |
            Select-Object -ExpandProperty extension_335d2348b8fbb5d75_employeeNumber -ErrorAction SilentlyContinue
    $_ | Add-Member -MemberType NoteProperty -Name extension_335d2348b8fbb5d75_employeeNumber -Value $CurrentEmpNumber
    $_ | Add-Member -MemberType NoteProperty -Name 'Last Activated Date' -Value $LAD
}

任何指导将不胜感激!

标签: powershellmicrosoft-graph-api

解决方案


您在底部提到的代码可能不起作用。原因是 $activationReport 和 $o365 报告之间没有联系

您可以使用以下代码段来实现您的目标。

 $O365Report | select 'User Principal Name', @{N="EmpNum";E={$tmp=$_.'User Principal Name';($UserDetails|?{$_.'User Principal Name' -eq $tmp}).extension_335d2348b8fbb5d75_employeeNumber}}, @{N="LastActivatedDate";E={$tmp=$_.'User Principal Name';($ActivationReport|?{$_.'User Principal Name' -eq $tmp}).'Last Activated Date'}}

解释 :

LastActDate它通过评估表达式来创建列

E={$tmp=$_.'User Principal Name';
($ActivationReport|?{$_.'User Principal Name' -eq $tmp}).'Last Activated Date'}}

表达评估:

$tmp使用用户主体名称的值存储$O365Report

然后进入$ActivationReport过滤器,用户主体名称等于$tmp

过滤的对象。“上次激活日期”* 存储在LastActDate


推荐阅读