Skip to content

Fix OdataId uris #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ function Add-EntraDirectoryRoleMember {
[System.String] $MemberId
)

begin {
# Ensure connection to Microsoft Entra
if (-not (Get-EntraContext)) {
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes RoleManagement.ReadWrite.Directory' to authenticate."
Write-Error -Message $errorMessage -ErrorAction Stop
return
}

# Get the Graph endpoint from the current environment
$environment = (Get-EntraContext).Environment
$graphEndpoint = (Get-EntraEnvironment | Where-Object Name -eq $environment).GraphEndPoint

# Default to global endpoint if not found
if (-not $graphEndpoint) {
$graphEndpoint = "https://graph.microsoft.com"
Write-Verbose "Using default Graph endpoint: $graphEndpoint"
}
else {
Write-Verbose "Using environment-specific Graph endpoint: $graphEndpoint"
}
}

PROCESS {
$params = @{}
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
Expand Down Expand Up @@ -59,7 +81,7 @@ function Add-EntraDirectoryRoleMember {
}
if ($null -ne $PSBoundParameters["MemberId"]) {
$TmpValue = $PSBoundParameters["MemberId"]
$Value = "/v1.0/directoryObjects/$TmpValue"
$Value = "$graphEndpoint/v1.0/directoryObjects/$TmpValue"
$params["OdataId"] = $Value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ function Add-EntraBetaApplicationOwner {
[System.String] $OwnerId
)

begin {
# Ensure connection to Microsoft Entra
if (-not (Get-EntraContext)) {
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Application.ReadWrite.All' to authenticate."
Write-Error -Message $errorMessage -ErrorAction Stop
return
}

# Get the Graph endpoint from the current environment
$environment = (Get-EntraContext).Environment
$graphEndpoint = (Get-EntraEnvironment | Where-Object Name -eq $environment).GraphEndPoint

# Default to global endpoint if not found
if (-not $graphEndpoint) {
$graphEndpoint = "https://graph.microsoft.com"
Write-Verbose "Using default Graph endpoint: $graphEndpoint"
}
else {
Write-Verbose "Using environment-specific Graph endpoint: $graphEndpoint"
}
}

PROCESS {
$params = @{}
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand
Expand All @@ -35,7 +57,7 @@ function Add-EntraBetaApplicationOwner {
}
if ($null -ne $PSBoundParameters["OwnerId"]) {
$TmpValue = $PSBoundParameters["OwnerId"]
$Value = "/beta/directoryObjects/$TmpValue"
$Value = "$graphEndpoint/beta/directoryObjects/$TmpValue"
$params["OdataId"] = $Value
}
if ($null -ne $PSBoundParameters["ApplicationId"]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ function Add-EntraBetaDirectoryRoleMember {
[System.String] $MemberId
)

begin {
# Ensure connection to Microsoft Entra
if (-not (Get-EntraContext)) {
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes RoleManagement.ReadWrite.Directory' to authenticate."
Write-Error -Message $errorMessage -ErrorAction Stop
return
}

# Get the Graph endpoint from the current environment
$environment = (Get-EntraContext).Environment
$graphEndpoint = (Get-EntraEnvironment | Where-Object Name -eq $environment).GraphEndPoint

# Default to global endpoint if not found
if (-not $graphEndpoint) {
$graphEndpoint = "https://graph.microsoft.com"
Write-Verbose "Using default Graph endpoint: $graphEndpoint"
}
else {
Write-Verbose "Using environment-specific Graph endpoint: $graphEndpoint"
}
}

PROCESS {
$params = @{}
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand
Expand All @@ -35,7 +57,7 @@ function Add-EntraBetaDirectoryRoleMember {
}
if ($null -ne $PSBoundParameters["MemberId"]) {
$TmpValue = $PSBoundParameters["MemberId"]
$Value = "/beta/directoryObjects/$TmpValue"
$Value = "$graphEndpoint/beta/directoryObjects/$TmpValue"
$params["OdataId"] = $Value
}
if ($null -ne $PSBoundParameters["DirectoryRoleId"]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ function Add-EntraBetaFeatureRolloutPolicyDirectoryObject {
[System.String] $Id
)

begin {
# Ensure connection to Microsoft Entra
if (-not (Get-EntraContext)) {
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Directory.ReadWrite.All' to authenticate."
Write-Error -Message $errorMessage -ErrorAction Stop
return
}

# Get the Graph endpoint from the current environment
$environment = (Get-EntraContext).Environment
$graphEndpoint = (Get-EntraEnvironment | Where-Object Name -eq $environment).GraphEndPoint

# Default to global endpoint if not found
if (-not $graphEndpoint) {
$graphEndpoint = "https://graph.microsoft.com"
Write-Verbose "Using default Graph endpoint: $graphEndpoint"
}
else {
Write-Verbose "Using environment-specific Graph endpoint: $graphEndpoint"
}
}

PROCESS {
$params = @{}
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand
Expand All @@ -33,7 +55,7 @@ function Add-EntraBetaFeatureRolloutPolicyDirectoryObject {
}
if ($null -ne $PSBoundParameters["RefObjectId"]) {
$TmpValue = $PSBoundParameters["RefObjectId"]
$Value = "/beta/directoryObjects/$TmpValue"
$Value = "$graphEndpoint/beta/directoryObjects/$TmpValue"
$params["OdataId"] = $Value
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ BeforeAll {
}
Import-Module (Join-Path $PSScriptRoot "..\..\Common-Functions.ps1") -Force

Mock -CommandName Get-EntraContext -MockWith {
@{
Scopes = @("RoleManagement.ReadWrite.Directory")
Environment = "Global" # Add the Environment property to the mock
}
} -ModuleName Microsoft.Entra.DirectoryManagement

# Mock the Get-EntraEnvironment command if needed
Mock -CommandName Get-EntraEnvironment -MockWith {
@{
Name = "Global"
GraphEndPoint = "https://graph.microsoft.com"
}
} -ModuleName Microsoft.Entra.DirectoryManagement

Mock -CommandName New-MgDirectoryRoleMemberByRef -MockWith {} -ModuleName Microsoft.Entra.DirectoryManagement
}

Expand Down Expand Up @@ -48,7 +63,7 @@ Describe "Add-EntraDirectoryRoleMember" {

$result = Add-EntraDirectoryRoleMember -DirectoryRoleId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -MemberId "bbbbbbbb-1111-2222-3333-cccccccccccc"
$params = Get-Parameters -data $result
$value = "/v1.0/directoryObjects/"
$value = "https://graph.microsoft.com/v1.0/directoryObjects/"
$params.OdataId | Should -Be $value"bbbbbbbb-1111-2222-3333-cccccccccccc"
}
It "Should contain 'User-Agent' header" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ BeforeAll {
}
Import-Module (Join-Path $PSScriptRoot "..\..\Common-Functions.ps1") -Force

Mock -CommandName Get-EntraContext -MockWith {
@{
Scopes = @("Directory.ReadWrite.All")
Environment = "Global" # Add the Environment property to the mock
}
} -ModuleName Microsoft.Entra.Beta.SignIns

# Mock the Get-EntraEnvironment command if needed
Mock -CommandName Get-EntraEnvironment -MockWith {
@{
Name = "Global"
GraphEndPoint = "https://graph.microsoft.com"
}
} -ModuleName Microsoft.Entra.Beta.SignIns

Mock -CommandName New-MgBetaDirectoryFeatureRolloutPolicyApplyToByRef -MockWith {} -ModuleName Microsoft.Entra.Beta.SignIns
}

Expand Down Expand Up @@ -48,7 +63,7 @@ Describe "Add-EntraBetaFeatureRolloutPolicyDirectoryObject" {
Mock -CommandName New-MgBetaDirectoryFeatureRolloutPolicyApplyToByRef -MockWith {$args} -ModuleName Microsoft.Entra.Beta.SignIns

$result = Add-EntraBetaFeatureRolloutPolicyDirectoryObject -Id "aaaabbbb-0000-cccc-1111-dddd2222eeee" -RefObjectId "bbbbcccc-1111-dddd-2222-eeee3333ffff"
$value = "/beta/directoryObjects/bbbbcccc-1111-dddd-2222-eeee3333ffff"
$value = "https://graph.microsoft.com/beta/directoryObjects/bbbbcccc-1111-dddd-2222-eeee3333ffff"
$params= Get-Parameters -data $result
$params.OdataId | Should -Be $value
}
Expand Down