Skip to content

Commit

Permalink
Refactor: All internal access token has been refactored
Browse files Browse the repository at this point in the history
While extending the Security Role cmdlet
  • Loading branch information
Splaxi committed Aug 21, 2024
1 parent 74c3856 commit 8a304ee
Show file tree
Hide file tree
Showing 59 changed files with 1,064 additions and 76 deletions.
3 changes: 2 additions & 1 deletion d365bap.tools/d365bap.tools.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RootModule = 'd365bap.tools.psm1'

# Version number of this module.
ModuleVersion = '0.0.20'
ModuleVersion = '0.0.10'

# ID used to uniquely identify this module
GUID = 'adfc3aa2-1269-4648-a3d6-0342d5ef00bf'
Expand Down Expand Up @@ -58,6 +58,7 @@
, 'Get-BapEnvironmentLinkEnterprisePolicy'

, 'Get-BapEnvironmentSecurityRole'
, 'Get-BapEnvironmentSecurityRoleMember'
, 'Get-BapEnvironmentSolution'

, 'Get-BapEnvironmentUser'
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Compare-BapEnvironmentD365App.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Compare environment D365 Apps
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Compare-BapEnvironmentUser.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Compare the environment users
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Compare environment Virtual Entities
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Test the integration status
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Export-BapEnvironmentSolution.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Export PowerPlatform / Dataverse Solution from the environment
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Get-BapEnvironment.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get environment info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get application users from environment
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Get-BapEnvironmentD365App.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get D365 App from the environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get Enterprise Policy
Expand Down
15 changes: 8 additions & 7 deletions d365bap.tools/functions/Get-BapEnvironmentSecurityRole.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get Security Roles from environment
Expand Down Expand Up @@ -35,8 +35,8 @@
Sample output:
Id Name IsManaged RoleType
-- ---- --------- --------
5a8c8098-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature… True Environment
1cbf96a1-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature… True Environment
5a8c8098-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature True Environment
1cbf96a1-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature True Environment
d364ba1c-1bfb-eb11-94f0-0022482381ee Accounts Payable Admin True Environment
.EXAMPLE
Expand All @@ -48,8 +48,8 @@
Sample output:
Id Name IsManaged RoleType
-- ---- --------- --------
5a8c8098-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature… True Environment
1cbf96a1-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature… True Environment
5a8c8098-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature True Environment
1cbf96a1-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature True Environment
d364ba1c-1bfb-eb11-94f0-0022482381ee Accounts Payable Admin True Environment
.EXAMPLE
Expand All @@ -61,15 +61,15 @@
Sample output:
Id Name IsManaged RoleType
-- ---- --------- --------
5a8c8098-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature… True Environment
5a8c8098-b933-eb11-a813-000d3a8e7ded (Deprecated) Marketing Realtime Feature True Environment
4758a2be-ccd8-ea11-a813-000d3a579805 App Profile Manager Administrator True Environment
470a750f-d810-4ee7-a64a-ec002965c1ec Copilot for Service Administrator True Environment
5e4a9faa-b260-e611-8106-00155db8820b IoT - Administrator True Environment
947229e9-e868-45cf-a361-5635eaf35ee2 Microsoft Copilot Administrator True Environment
f7f90019-dc14-e911-816a-000d3a069ebd Omnichannel administrator True Environment
6beb51c1-0eda-e911-a81c-000d3af75d63 Productivity tools administrator True Environment
ebbb3fcb-fcd7-4bf8-9a48-7b5a9878e79e Sales Copilot Administrator True Environment
abce3b01-5697-4973-9d7d-fca48ca84445 Survey Services Administrator(Deprecate… True Environment
abce3b01-5697-4973-9d7d-fca48ca84445 Survey Services Administrator(Deprecat True Environment
63e389ae-bc55-ec11-8f8f-6045bd88b210 System Administrator True Environment
.EXAMPLE
Expand Down Expand Up @@ -106,6 +106,7 @@
#>
function Get-BapEnvironmentSecurityRole {
[CmdletBinding()]
[OutputType('System.Object[]')]
param (
[parameter (mandatory = $true)]
[string] $EnvironmentId,
Expand Down
190 changes: 190 additions & 0 deletions d365bap.tools/functions/Get-BapEnvironmentSecurityRoleMember.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@

<#
.SYNOPSIS
Get users/members from security role
.DESCRIPTION
Enables the user to fetch all users/members from the security role in the environment
Utilizes the built-in "roles" OData entity
Allows the user to include all users/members, based on those who has the ApplicationId property filled
.PARAMETER EnvironmentId
The id of the environment that you want to work against
This can be obtained from the Get-BapEnvironment cmdlet
.PARAMETER SecurityRoleId
The id of the security role that you want to work against
This can be obtained from the Get-BapEnvironmentSecurityRole cmdlet
.PARAMETER UserId
The (SystemUser)Id or email of the user that you want to filter on
This can be obtained from the Get-BapEnvironmentUser cmdlet
Default value is "*" - which translates into all available users/members
Wildcard search is supported
.PARAMETER IncludeAppIds
Instruct the cmdlet to include all users that are members of the security role
Simply includes those who has the ApplicationId property filled
.PARAMETER AsExcelOutput
Instruct the cmdlet to output all details directly to an Excel file
This makes it easier to deep dive into all the details returned from the API, and makes it possible for the user to persist the current state
.EXAMPLE
PS C:\> Get-BapEnvironmentSecurityRoleMember -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -SecurityRoleId 'System Administrator'
This will fetch all oridinary users that are members of the security role 'System Administrator' from the environment.
Sample output:
Email Name AppId SystemUserId
----- ---- ----- ------------
[email protected] # D365Admin 58879b65-65ca-45f7-bf8e-9550e241083e
[email protected] Delegated Admin 58879b65-65ca-47f5-bf8e-9550e241083e
.EXAMPLE
PS C:\> Get-BapEnvironmentSecurityRoleMember -EnvironmentId *uat* -SecurityRoleId 'System Administrator'
This will fetch all oridinary users that are members of the security role 'System Administrator' from the environment.
Sample output:
Email Name AppId SystemUserId
----- ---- ----- ------------
[email protected] # D365Admin 58879b65-65ca-45f7-bf8e-9550e241083e
[email protected] Delegated Admin 58879b65-65ca-47f5-bf8e-9550e241083e
.EXAMPLE
PS C:\> Get-BapEnvironmentSecurityRoleMember -EnvironmentId *uat* -SecurityRoleId 'System Administrator' -UserId '*@contoso.com'
This will fetch all oridinary users that are members of the security role 'System Administrator' from the environment.
It will only include the ones that have an email address that contains '@contoso.com'.
Sample output:
Email Name AppId SystemUserId
----- ---- ----- ------------
[email protected] # D365Admin 58879b65-65ca-45f7-bf8e-9550e241083e
.EXAMPLE
PS C:\> Get-BapEnvironmentSecurityRoleMember -EnvironmentId *uat* -SecurityRoleId 'System Administrator' -IncludeAppIds
This will fetch all users that are members of the security role 'System Administrator' from the environment.
It will include the ones with the ApplicationId property filled.
Sample output:
Email Name AppId SystemUserId
----- ---- ----- ------------
CatalogServiceEur@onmicrosoft… # CatalogServiceEur ac22509c-8d51-4169-… 330297ba-cbf6-ed11-8849-6045bd8e42bc
[email protected] # CCaaSCRMClient edfdd43b-45b9-498b-… f4f45a4b-f8b7-ed11-9886-6045bd8e42bc
[email protected] # D365Admin 58879b65-56ca-45f7-bf8e-9550e241083e
.EXAMPLE
PS C:\> Get-BapEnvironmentSecurityRoleMember -EnvironmentId *uat* -SecurityRoleId 'System Administrator' -AsExcelOutput
This will fetch all oridinary users that are members of the security role 'System Administrator' from the environment.
Will output all details into an Excel file, that will auto open on your machine.
.NOTES
Author: Mötz Jensen (@Splaxi)
#>
function Get-BapEnvironmentSecurityRoleMember {
[CmdletBinding()]
[OutputType('System.Object[]')]
param (
[parameter (mandatory = $true)]
[string] $EnvironmentId,

[parameter (mandatory = $true)]
[string] $SecurityRoleId,

[string] $UserId = "*",

[switch] $IncludeAppIds,

[switch] $AsExcelOutput
)

begin {
# Make sure all *BapEnvironment* cmdlets will validate that the environment exists prior running anything.
$envObj = Get-BapEnvironment -EnvironmentId $EnvironmentId | Select-Object -First 1

if ($null -eq $envObj) {
$messageString = "The supplied EnvironmentId: <c='em'>$EnvironmentId</c> didn't return any matching environment details. Please verify that the EnvironmentId is correct - try running the <c='em'>Get-BapEnvironment</c> cmdlet."
Write-PSFMessage -Level Host -Message $messageString
Stop-PSFFunction -Message "Stopping because environment was NOT found based on the id." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
}

if (Test-PSFFunctionInterrupt) { return }

$secRoleObj = Get-BapEnvironmentSecurityRole -EnvironmentId $EnvironmentId -Name $SecurityRoleId | Select-Object -First 1

if ($null -eq $secRoleObj) {
$messageString = "The supplied SecurityRoleId: <c='em'>$SecurityRoleId</c> didn't return any matching security details from the Environment. Please verify that the EnvironmentId & SecurityRoleId is correct - try running the <c='em'>Get-BapEnvironment</c> or <c='em'>Get-BapEnvironmentSecurityRole</c> cmdlets."
Write-PSFMessage -Level Host -Message $messageString
Stop-PSFFunction -Message "Stopping because environment was NOT found based on the id." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
}

if (Test-PSFFunctionInterrupt) { return }

$baseUri = $envObj.LinkedMetaPpacEnvUri

$secureToken = (Get-AzAccessToken -ResourceUrl $baseUri -AsSecureString).Token
$tokenWebApiValue = ConvertFrom-SecureString -AsPlainText -SecureString $secureToken

$headersWebApi = @{
"Authorization" = "Bearer $($tokenWebApiValue)"
}
}

process {
if (Test-PSFFunctionInterrupt) { return }

$resRoleObj = Invoke-RestMethod -Method Get -Uri $($baseUri + "/api/data/v9.2/roles($($secRoleObj.Id))?`$expand=systemuserroles_association") -Headers $headersWebApi

$resCol = @(
$resRoleObj.systemuserroles_association | Select-PSFObject -TypeName "D365Bap.Tools.User" `
-ExcludeProperty "@odata.etag" `
-Property "systemuserid as Id",
"internalemailaddress as Email",
"fullname as Name",
"applicationid as AppId",
"azureactivedirectoryobjectid as EntraIdObjectId",
@{Name = "NameSortable"; Expression = { $_.fullname.Replace("# ", "") } },
*
)

$resCol = $resCol | Sort-Object -Property NameSortable

if (-not $IncludeAppIds) {
$resCol = $resCol | Where-Object applicationid -eq $null
}

if ($UserId.Contains("@")) {
$resCol = $resCol | Where-Object Email -like $UserId
}
else {
$resCol = $resCol | Where-Object Id -like $UserId
}

if ($AsExcelOutput) {
$resCol | Export-Excel
return
}

$resCol
}

end {

}
}
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Get-BapEnvironmentSolution.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get PowerPlatform / Dataverse Solution from the environment
Expand Down
8 changes: 6 additions & 2 deletions d365bap.tools/functions/Get-BapEnvironmentUser.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get users from environment
Expand Down Expand Up @@ -103,10 +103,14 @@ function Get-BapEnvironmentUser {
foreach ($usrObj in $($resUsers.value | Sort-Object -Property internalemailaddress)) {

$usrObj | Add-Member -MemberType NoteProperty -Name "lang" -Value $($languages | Where-Object { ($_.localeid -eq $usrObj.user_settings[0].uilanguageid) -or ($_.BaseLocaleId -eq $usrObj.user_settings[0].uilanguageid) } | Select-Object -First 1 -ExpandProperty code)
$usrObj | Select-PSFObject -TypeName "D365Bap.Tools.User" -ExcludeProperty "@odata.etag" -Property "internalemailaddress as Email",
$usrObj | Select-PSFObject -TypeName "D365Bap.Tools.User" `
-ExcludeProperty "@odata.etag" `
-Property "systemuserid as Id",
"internalemailaddress as Email",
"fullname as Name",
"applicationid as AppId",
"lang as Language",
"azureactivedirectoryobjectid as EntraIdObjectId",
*
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get Virtual Entity from environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Invoke the installation of a D365 App in a given environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Set the link between Dataverse and the Enterprise Policy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Set Virtual Entity configuration in environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Update the meta data for an Virtual Entity in an environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get Business Units from Environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


<#
.SYNOPSIS
Get language from Environment
Expand Down
Loading

0 comments on commit 8a304ee

Please sign in to comment.