locked
Adding custom policies to Azure B2C portal with PowerShell RRS feed

  • Question

  • I got the below script from a resource that was able to successfully upload custom B2C policies to a tenant. I'm running into an error I can't figure out. I'm basically logging in, grabbing an access token from my connection to the tenant, then testing the XML repo (Git in VSTS), splitting the files, then trying to post via HttpUtility. The error is Invoke-WebRequest: An error has occurred.The 'content' field is invalid in a request. Please check the request body and parameters.

    Here is my code, any help would be greatly appreciated.


    param(
        [string] $azureTenantID,
        [string] $b2cFilePolicy,
        [string] $azureTenantName,
        [string] $strUserID,
        [string] $strPw
        )
    
    # sign in
    Write-Host "Logging in...";
    
    $secpasswd = ConvertTo-SecureString -String $strPw -AsPlainText -Force
    
    $creds = New-Object Management.Automation.PSCredential ($strUserID,$secpasswd)
    Login-AzureRmAccount -Credential $creds
    
    <# $context = Set-AzureRmContext -Tenant $azureTenantID
    $context | Select-AzureRmContext | Out-Null #>
    
    $TenantId = $context.Tenant.TenantId
    $token = $context.TokenCache.ReadItems() | Where-Object { $_.Resource -ilike "*/management.core.windows.net/*" -and $_.AccessToken -ne $null -and $azureTenantId -ieq $_.Authority.Split('/')[3] } | sort -Property ExpiresOn -Descending | select -First 1
    $strAccessToken = $token.AccessToken
    
    if (Test-Path $b2cFilePolicy)
    {
        Write-Output "Uploading policy: $($b2cFilePolicy.Split('`\')[-1])"
    
        $strPolicy = (Get-Content -Path $b2cFilePolicy) -join "`n"
        Add-Type -AssemblyName System.Web
        $strBody = "<string xmlns=`"http://schemas.microsoft.com/2003/10/Serialization/`">$([System.Web.HttpUtility]::HtmlEncode($strPolicy))</string>"
     
    
        $htHeaders = @{ "Authorization" = "Bearer $strAccessToken" }
    
        $response = $null
        $response = Invoke-WebRequest -Uri "https://main.b2cadmin.ext.azure.com/api/trustframework?tenantId=$TenantID&overwriteIfExists=true" -Method POST -Body $strBody -ContentType "application/xml" -Headers $htHeaders -UseBasicParsing
    
        if ($response.StatusCode -ge 200 -and $response.StatusCode -le 299)
        {
            Write-Output "Policy successfully uploaded"
        }
        else
        {
            Write-Output "Failed to upload policy"
        }
    }
    
    else
    {
        Write-Error "Cannot find file: $b2cFilePolicy"
    }
    
    
    


    • Edited by Brian Batchelder Tuesday, February 6, 2018 4:03 PM
    • Moved by Bill_Stewart Monday, March 12, 2018 9:28 PM This is not "fix/debug/rewrite my script for me" forum
    Tuesday, February 6, 2018 3:54 PM

All replies

  • We don't rewrite scripts that you have found either on the internet or elsewhere.

    The error message is pretty specific as to what is happening.

    Tuesday, February 6, 2018 4:10 PM
  • You would do best posting this in the Azure PowerShell forum.


    \_(ツ)_/

    Tuesday, February 6, 2018 6:50 PM
  • Thanks, will post there. Although I don;t see PowerShell anywhere but under Windows Server. I'll keep looking.
    Tuesday, February 6, 2018 8:40 PM
  • Every script comes from the internet or elsewhere...so what do you do here in the Scripting Guys forum? Just for my edification.
    Tuesday, February 6, 2018 8:57 PM
  • Every script comes from the internet or elsewhere...so what do you do here in the Scripting Guys forum? Just for my edification.

    The forum is to assist techs that are trained in scripting and have a technical question about scripting or a script they are writing.

    If you have issues with a script you have found you will need to ask the author of the original script for help. We cannot waste time debugging or fixing any script you may find.

    If you are not a tech then you will have to contact a co-worker or consultant for help.

    If you have a specific question then we can help.


    \_(ツ)_/

    Tuesday, February 6, 2018 9:35 PM