[英]3 Graph API Endpoints - inserting column from one to another after matching
At the moment, I am querying 2 different Graph API endpoints, getting M365 active user details as well as user detail from the users endpoint, and matching the userPrincipalName to the employeeNumber.目前,我正在查询 2 个不同的 Graph API 端点,从用户端点获取 M365 活动用户详细信息以及用户详细信息,并将 userPrincipalName 与employeeNumber 匹配。 employeeNumber is a value that is currently an extension attribute. employeeNumber 是一个当前是扩展属性的值。
I am trying to query a 3rd Graph API endpoint to get Office activation details:我正在尝试查询 3rd Graph API 端点以获取 Office 激活详细信息:
https://graph.microsoft.com/v1.0/reports/getOffice365ActivationsUserDetail
The logic currently looks like this:目前的逻辑如下所示:
#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."
I am trying to simply grab the column called "Last Activation Date"
from the getOffice365ActivationsUserDetail report, match it using User Principal Name
, and insert into the final CSV output as a separate column.我试图简单地从 getOffice365ActivationsUserDetail 报告中获取名为"Last Activation Date"
的列,使用User Principal Name
进行匹配,然后将其插入到最终的 CSV output 作为单独的列中。 I am unsure how to proceed, as I already have those members added as part of the previous foreach loop.我不确定如何继续,因为我已经将这些成员添加为前一个 foreach 循环的一部分。 Both reports contain a column with the header "User Principal Name"
两个报告都包含一个带有 header "User Principal Name"
的列
The call to the Graph API endpoint is as follows:对 Graph API 端点的调用如下:
$ActivationReport = Invoke-RestMethod -Method Get -Uri $activationsUri -Headers $headerParams | ConvertFrom-Csv
Not sure if it is as simple as:不确定它是否像这样简单:
$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
}
Any guidance would be much appreciated!任何指导将不胜感激!
The code that you have mentioned at the bottom may not work.您在底部提到的代码可能不起作用。 The reason being there is no connection between the $activationReport and $o365 repport原因是 $activationReport 和 $o365 报告之间没有联系
You could use the below snippet to achieve your goal.您可以使用以下代码段来实现您的目标。
$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'}}
Explanation:解释:
It creates column LastActDate
by evaluating the expression它通过评估表达式创建列LastActDate
E={$tmp=$_.'User Principal Name';
($ActivationReport|?{$_.'User Principal Name' -eq $tmp}).'Last Activated Date'}}
Expression Evaluation:表达评估:
It stores the $tmp
with the value of User Principal Name of the $O365Report
它将$tmp
与$O365Report
的用户主体名称的值一起存储
Then goes to the $ActivationReport
filters the user Principal name equal to the $tmp
然后转到$ActivationReport
过滤用户主体名称等于$tmp
Filtered object.'Last Activated Date'* is stored in the column of the LastActDate
过滤后的 object.'Last Activated Date'* 存储在LastActDate
的列中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.