使用PowerShell从Microsoft Azure活动目录检索数据

huangapple go评论72阅读模式
英文:

Using PowerShell to retrieve data from Microsoft Azure Active Directory

问题

I cannot retrieve all the data that I want even though I can see that the data exists while I'm viewing through the portal.

$users = Get-AzureADUser -All $true | `
    Where-Object {$_.CompanyName -like 'CompanyName*'} | `
    Get-Random -Count 1000 | `
    Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, OfficeLocation

I've changed the Company name and removed some of the objects I'm retrieving for privacy reasons.
This script above is used in my Powershell window and it successfully retrieves the data I want.

EmployeeId did not work at first, and by googling and testing around, I found that I have to use the "ExtensionProperty" in order to retrieve that data. The problem I'm having now is that I cannot retrieve "OfficeLocation" no matter what I try.

I've double-checked to make sure that I have proper access to my account, and all seems to be in order. Why can I not retrieve any data for "officeLocation"?

--

I have tried the following:

  • I tried using a variety of names for OfficeLocation, examples are = OfficeLocation, officeLocation, Office, office_location, Office_Loc, Location, etc., etc. I've spent almost two days trying to figure this out.

I tried writing:

@{N="OfficeLocation";E={$_.AdditionalProperties["officeLocation"]}}

I've also tried:

@{N="OfficeLocation";E={$_.ExtensionProperty.officeLocation}}

Neither works.
This is how the field looks in my AzureAD

So in this example (I've covered the actual location) I can see that data exists about the "officeLocation," but I cannot retrieve it using my PowerShell script.

Is this not possible through PowerShell? Do I need to use Microsoft Graph API?

Thankful for any assistance or guidance you can point me towards!

Edit Spelling

英文:

I cannot retrieve all the data that I want even though I can see that the data exists while I'm viewing through the portal.

$users = Get-AzureADUser -All $true | `
    Where-Object {$_.CompanyName -like 'CompanyName*' | `
    Get-Random -Count 1000 | `
    Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, OfficeLocation

I've changed the Company name and removed some of the objects I'm retrieving for privacy reasons.
This script above is used in my Powershell window and it successfully retrieves the data I want.

EmployeeId did not work at first and by googling and testing around I found that I have to use the "ExtensionProperty" in order to retrieve that data. The problem I'm having now is that I cannot retrieve "OfficeLocation" no matter what I try.

I've double-checked to make sure that I have proper access to my account and all seems to be in order, why can I not retrieve any data for "officeLocation"?

--
I have tried the following

  • I tried using a variety of names for OfficeLocation.. examples are = OfficeLocation, officeLocation, Office, office_location, Office_Loc, Location etc etc... I've spent almost two days trying to figure this out.

I tried writing:

@{N="OfficeLocation";E={$_.AdditionalProperties["officeLocation"]}}

I've also tried:

@{N="OfficeLocaton";E={$_.ExtensionProperty.officeLocation}}

Neither works.
This is how the field looks in my AzureAD

So in this example (I've covered the actual location) I can see that data exists about the "officelocation" but I cannot retrieve it by using my PowerShell script.

Is this not possible through Powershell? Do I need to use Microsoft Graph API?

Thankful for any assistance or guidance you can point me towards!

Edit Spelling

答案1

得分: 0

I think that you need to update the PowerShell module for Azure because the last version has a different command: Get-AzADUser.

尝试这个:

Update-Module -Name Az -Force

然后:

$users = Get-AzADUSer  | `
    Where-Object {$_.CompanyName -like 'CompanyName*'} | `
    Get-Random -Count 1000 | `
    Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, OfficeLocation
英文:

I think that you need to update the PowerShell module for Azure because the last version has a different command: Get-AzADUser.

Try this:

Update-Module -Name Az -Force

And then:

$users = Get-AzADUSer  | `
    Where-Object {$_.CompanyName -like 'CompanyName*' | `
    Get-Random -Count 1000 | `
    Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, OfficeLocation

答案2

得分: 0

OfficeLocation 在使用 Azure AD PowerShell v2 模块时显示在 PhysicalDeliveryOfficeName 属性中。

在您的情况下,您需要通过以下方式修改您的代码,将 OfficeLocation 替换为 PhysicalDeliveryOfficeName

$users = Get-AzureADUser -All $true | `
    Where-Object {$_.CompanyName -like 'CompanyName*' | `
    Get-Random -Count 1000 | `
    Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, PhysicalDeliveryOfficeName

或者,您可以使用 Microsoft Graph API 来获取用户,运行以下查询:

GET https://graph.microsoft.com/v1.0/users?$filter=companyName in ('CompanyName')&$select=givenName,surname,officeLocation,employeeId&$count=true
ConsistencyLevel: Eventual
英文:

Note that, OfficeLocation is displayed in PhysicalDeliveryOfficeName property while using Azure AD PowerShell v2 module.

In your case, you need to modify your code by replacing OfficeLocation with PhysicalDeliveryOfficeName like below:

$users = Get-AzureADUser -All $true | `
    Where-Object {$_.CompanyName -like 'CompanyName*' | `
    Get-Random -Count 1000 | `
    Select-Object @{N="EmployeeID";E={$_.ExtensionProperty.employeeId}}, GivenName, Surname, PhysicalDeliveryOfficeName

Response:

使用PowerShell从Microsoft Azure活动目录检索数据

Alternatively, you can make use of Microsoft Graph API to fetch users by running below query:

GET https://graph.microsoft.com/v1.0/users?$filter=companyName in ('CompanyName')&$select=givenName,surname,officeLocation,employeeId&$count=true
ConsistencyLevel: Eventual

Response:

使用PowerShell从Microsoft Azure活动目录检索数据

答案3

得分: -1

以下是已翻译的内容:

正确的管道对象语法是 $_,而不仅仅是 $。所以如果没有其他选项,请尝试:

@{n="EmployeeID";n={$_.ExtensionProperty.employeeId}}

要查看扩展属性对象中的实际键,我建议使用:

(Get-AzureADUser | select -first 1).ExtensionProperty.Keys
英文:

The correct syntax for the piped object is $_, not just $. So if nothing else, try:

@{n="EmployeeID";n={$_.ExtensionProperty.employeeId}}

To see the actual keys within the extension property object I suggest:

(Get-AzureADUser | select -first 1).ExtensionProperty.Keys

huangapple
  • 本文由 发表于 2023年5月25日 21:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定