我在 Azure AD B2C 中创建了一个服务原则,并希望使用 GHA 中的
TrustFrameworkExtensions.xml
powershell cmdlet 将 Set-AzureADMSTrustFrameworkPolicy
文件上传到其中。除了最后一行之外,所有内容都对我有用。尽管我能够成功导入 Powershell Set-AzureADMSTrustFrameworkPolicy
模块而没有任何问题,但我收到错误 AzureADPreview
不知道 cmdlet。我正在拔头发来解决这个问题。
另请注意,这适用于我的笔记本电脑。但是我在 github actions 中运行它时遇到错误。我正在运行 Windows powershell
这是错误:
Set-AzureADMSTrustFrameworkPolicy : The term 'Set-AzureADMSTrustFrameworkPolicy' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
At D:\a\_temp\e4c3721f-2770-44f1-897e-b9434474d966.ps1:23 char:1
+ Set-AzureADMSTrustFrameworkPolicy -Id B2C_1A_TrustFrameworkExtensions ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-AzureADMSTrustFrameworkPolicy:String) [], ParentContainsErrorRecord
Exception
+ FullyQualifiedErrorId : CommandNotFoundException
Error: Process completed with exit code 1.
这是代码:
run-script:
runs-on: windows-latest # Run on Windows for PowerShell compatibility
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Azure login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Upload Files to Azure AD B2C
run: |
Install-Module -Name AzureADPreview -Scope CurrentUser -Force -AllowClobber
Import-Module AzureADPreview
if (Get-Module -Name AzureADPreview -ListAvailable) {
Write-Host "AzureADPreview module is installed."
} else {
Write-Host "AzureADPreview module is not installed."
}
az login --service-principal --username $service-principal-clientId --password $service-principal-password --tenant $tenantId --allow-no-subscriptions
$aadToken = az account get-access-token --resource-type aad-graph | ConvertFrom-Json
$graphToken = az account get-access-token --resource-type ms-graph | ConvertFrom-Json
Connect-AzureAD -AadAccessToken $aadToken.accessToken -AccountId $service-principal-clientId -TenantId $tenantId -MsAccessToken $graphToken.accessToken
Set-AzureADMSTrustFrameworkPolicy -Id B2C_1A_TrustFrameworkExtensions -InputFilePath .\Templates\TrustFrameworkExtensions.xml
shell: powershell
```
注意:
、AzureAD
模块已弃用,您需要使用 Microsoft Graph 模块或任何其他模块来执行该操作。请参考此博客AzureADPreview
因此,作为解决方法,我使用 Microsoft Graph API 查询上传自定义策略,如下所示:
我已将policy上传到GitHub存储库中:
并使用以下
yml
文件上传政策:
name: Azure AD B2C Policy Upload
on:
push:
branches:
- main
jobs:
install-and-upload:
runs-on: windows-latest # Use a Windows runner
steps:
- name: Checkout code
uses: actions/checkout@v3 # Checkout your repository code to access the PowerShell script
- name: Azure login
run: |
# Log in to Azure using the service principal credentials directly
az login --service-principal --username "B2CAppClientID" --password "B2CAppClientSecret" --tenant "B2CTenantID" --allow-no-subscriptions
# Get the access token for Microsoft Graph API
access_token=$(az account get-access-token --resource-type ms-graph --query accessToken -o tsv | tr -d '\r')
# Define the path to your policy file
policy_file="B2C_1A_TESTPOLICY.xml"
# Upload the policy file to Azure AD B2C
az rest --method post \
--uri "https://graph.microsoft.com/beta/trustFramework/policies" \
--headers "Content-Type=application/xml" "Authorization=Bearer $access_token" \
--body "@$policy_file"
shell: bash
保单上传成功:
Policy.ReadWrite.TrustFramework
API 权限:New-MgBetaTrustFrameworkPolicy
、Update-MgBetaTrustFrameworkPolicy
来分别创建、更新。请参考此GitHub 博客