图 API - 从预建脚本添加带有 employeeid 的自定义列
Graph API - adding custom column with employeeid from prebuilt script
我正在使用 Microsoft 通过 Graph API 提供的 365 活跃用户报告,但是它只缺少一个我需要的东西 - 这就是 employeeNumber。我们最近扩展了架构,我可以像这样查询 employeeNumber:
https://graph.microsoft.com/v1.0/users?$select=extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
下面代码的输出为我提供了所有获得许可的活跃 365 用户及其许可类型,但我想以某种方式还包括 employeeNumber 列。考虑到这是一份微软报告,我不确定这是否可能。
我是否只使用 $reports
的结果构建一个报表对象,然后 运行 一个单独的查询来获取所有 employeeNumbers,并以某种方式插入该列?我想我可以使用 userPrincipalName
.
来匹配它们
# CHANGE THESE VALUES
$TenantID = 'tenantid' #The Directory ID from Azure AD
$ClientID = 'ClientID ' #The Application ID of the registered app
$ClientSecret = 'ClientSecret ' #The secret key of the registered app
# ------------------------------------------------------
# DO NOT CHANGE THESE
$body = @{grant_type="client_credentials";scope="https://graph.microsoft.com/.default";client_id=$ClientID;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token -Body $body
$token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
# ------------------------------------------------------
$graphApiUri = "https://graph.microsoft.com/v1.0/reports/getOffice365ActiveUserDetail(period='D90')"
$Reports = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $token | ConvertFrom-Csv
$Reports | Export-Csv "c:\temp\GraphAPI365UsersReport.csv" -NoTypeInformation
更新的脚本,效果很好。
# Application (client) ID, Directory (tenant) ID, and secret
$clientID = "clientID"
$tenantID = "tenantID"
$ClientSecret = "ClientSecret"
$resource = "https://graph.microsoft.com/"
#Get token
Write-Output "Acquire Graph Token."
try {
$body = @{grant_type="client_credentials";scope="https://graph.microsoft.com/.default";client_id=$ClientID;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token -Body $body
$token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
}
catch {
Write-Output "Error getting Graph Token."
Write-Output $_.Exception.Message
EXIT
}
# ------------------------------------------------------
$graphApiUri = "https://graph.microsoft.com/v1.0/reports/getOffice365ActiveUserDetail(period='D90')"
$Uri = "https://graph.microsoft.com/v1.0/users?`$select=userPrincipalName,extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber"
$O365Report = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $token | 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 $token -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 | ForEach-Object {
$CurrentEmpNumber = $UserDetails.value |
Where-Object userPrincipalName -eq $_.'User Principal Name' |
Select-Object -ExpandProperty extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
$_ | Add-Member -MemberType NoteProperty -Name extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber -Value $CurrentEmpNumber
}
$O365Report | Export-Csv "c:\temp\GraphAPI365UsersReport.csv" -NoTypeInformation
不幸的是,这些报告确实非常静态,不过获取您想要的信息应该不难。我没有你的分机,但这样的东西应该可以工作:
$Reports = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $token | ConvertFrom-Csv
$Uri = 'https://graph.microsoft.com/v1.0/users?$select=userPrincipalName,extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber'
$UserDetails = Invoke-RestMethod -Method Get -Uri $Uri -Headers $token
$Reports | ForEach-Object {
$CurrentEmpNumber = $UserDetails.value |
Where-Object userPrincipalName -eq $_.'User Principal Name' |
Select-Object -ExpandProperty extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
$_ | Add-Member -MemberType NoteProperty -Name EmployeeNumber -Value $CurrentEmpNumber
}
$Reports | Export-Csv "c:\temp\GraphAPI365UsersReport.csv" -NoTypeInformation
在相关说明中,您为什么要为此扩展架构? Azure AD 中已经有一个 EmployeeId 属性,因此上面代码中的 extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
可能只是 employeeId
.
我正在使用 Microsoft 通过 Graph API 提供的 365 活跃用户报告,但是它只缺少一个我需要的东西 - 这就是 employeeNumber。我们最近扩展了架构,我可以像这样查询 employeeNumber:
https://graph.microsoft.com/v1.0/users?$select=extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
下面代码的输出为我提供了所有获得许可的活跃 365 用户及其许可类型,但我想以某种方式还包括 employeeNumber 列。考虑到这是一份微软报告,我不确定这是否可能。
我是否只使用 $reports
的结果构建一个报表对象,然后 运行 一个单独的查询来获取所有 employeeNumbers,并以某种方式插入该列?我想我可以使用 userPrincipalName
.
# CHANGE THESE VALUES
$TenantID = 'tenantid' #The Directory ID from Azure AD
$ClientID = 'ClientID ' #The Application ID of the registered app
$ClientSecret = 'ClientSecret ' #The secret key of the registered app
# ------------------------------------------------------
# DO NOT CHANGE THESE
$body = @{grant_type="client_credentials";scope="https://graph.microsoft.com/.default";client_id=$ClientID;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token -Body $body
$token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
# ------------------------------------------------------
$graphApiUri = "https://graph.microsoft.com/v1.0/reports/getOffice365ActiveUserDetail(period='D90')"
$Reports = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $token | ConvertFrom-Csv
$Reports | Export-Csv "c:\temp\GraphAPI365UsersReport.csv" -NoTypeInformation
更新的脚本,效果很好。
# Application (client) ID, Directory (tenant) ID, and secret
$clientID = "clientID"
$tenantID = "tenantID"
$ClientSecret = "ClientSecret"
$resource = "https://graph.microsoft.com/"
#Get token
Write-Output "Acquire Graph Token."
try {
$body = @{grant_type="client_credentials";scope="https://graph.microsoft.com/.default";client_id=$ClientID;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token -Body $body
$token = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
}
catch {
Write-Output "Error getting Graph Token."
Write-Output $_.Exception.Message
EXIT
}
# ------------------------------------------------------
$graphApiUri = "https://graph.microsoft.com/v1.0/reports/getOffice365ActiveUserDetail(period='D90')"
$Uri = "https://graph.microsoft.com/v1.0/users?`$select=userPrincipalName,extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber"
$O365Report = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $token | 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 $token -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 | ForEach-Object {
$CurrentEmpNumber = $UserDetails.value |
Where-Object userPrincipalName -eq $_.'User Principal Name' |
Select-Object -ExpandProperty extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
$_ | Add-Member -MemberType NoteProperty -Name extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber -Value $CurrentEmpNumber
}
$O365Report | Export-Csv "c:\temp\GraphAPI365UsersReport.csv" -NoTypeInformation
不幸的是,这些报告确实非常静态,不过获取您想要的信息应该不难。我没有你的分机,但这样的东西应该可以工作:
$Reports = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $token | ConvertFrom-Csv
$Uri = 'https://graph.microsoft.com/v1.0/users?$select=userPrincipalName,extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber'
$UserDetails = Invoke-RestMethod -Method Get -Uri $Uri -Headers $token
$Reports | ForEach-Object {
$CurrentEmpNumber = $UserDetails.value |
Where-Object userPrincipalName -eq $_.'User Principal Name' |
Select-Object -ExpandProperty extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
$_ | Add-Member -MemberType NoteProperty -Name EmployeeNumber -Value $CurrentEmpNumber
}
$Reports | Export-Csv "c:\temp\GraphAPI365UsersReport.csv" -NoTypeInformation
在相关说明中,您为什么要为此扩展架构? Azure AD 中已经有一个 EmployeeId 属性,因此上面代码中的 extension_335d4df9847945fbaa472c8b8fbb5d75_employeeNumber
可能只是 employeeId
.