how to transfer powershell runbook to powershell workflow runbook in azure automation

Rory_Feng 66 Reputation points
2021-06-28T10:04:10.393+00:00

Hello everyone

I created a powershell runbook below. can someone tell me how to transfer the powershell runbook to powershell workflow runbook.
The error was happened when I copied the code to powershell workflow runbook and run it.

error: Runbook definition is invalid,Could not find type Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient
Could tell me why it cannot run in powershell workflow runbook
(it is can run in powershell runbook)

param(
[ Parameter (Mandatory= $true)]
[string] $ResourceGroupName,
[ Parameter (Mandatory= $true)]
[string] $VmName)

$ConnectionName = "AzureRunAsConnection"
$Conn = Get-AutomationConnection -Name $ConnectionName
Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
$authHeader = @{
'Content-Type'='application/json'
'Authorization'='Bearer ' + $token.AccessToken
}

$body = @{
xxxxxxxxxxx
}
$restUri = 'https://management.azure.com/subscriptions/{subscriptionId}?api-version=2020-01-01'
$response = Invoke-webrequest -Uri $restUri -Methodpost -Headers $authHeader -Body $($body | convertto-Json) -UseBasicParsing

Best wishes!

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
{count} votes

Answer accepted by question author
  1. Stanislav Zhelyazkov 29,306 Reputation points MVP Volunteer Moderator
    2021-07-06T13:06:14.613+00:00

    Hi,
    There is no automatic conversation tool that moves PS runbook to PS Workflow runbook. Overall my suggestion is to use PowerShell runbooks rather Workflows as workflow is old concept that has a lot of limitations and issues in order to use it.

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


1 additional answer

Sort by: Most helpful
  1. tbgangav-MSFT 10,431 Reputation points Moderator
    2021-07-06T13:36:50.817+00:00

    Hi @Rory_Feng ,

    Update:

    In this scenario or use case, if you want a token to use it further with ARM request in Azure sandbox environment then the recommended way is to go with Get-AzAccessToken cmdlet.

    For illustration, please find below screenshots.

    112168-image.png

    112140-image.png

    You can find the runbook content below. Note that you may have to slightly tweak the runbook to work in your environment i.e., updating subscription id in request URI, body, etc.

    workflow test4  
    {  
        $ConnectionName = "AzureRunAsConnection"  
        $Conn = Get-AutomationConnection -Name $ConnectionName  
        Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint  
      
        $GetAccessToken = Get-AzAccessToken  
        $auth = $GetAccessToken.token  
      
        $authHeader = @{  
        'Content-Type'='application/json'  
        'Accept'='application/json'  
        'Authorization'= "Bearer $auth"  
        }  
      
        $request = 'https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx?api-version=2020-01-01'  
        $Body = @{  
            'testkey' = 'testkeyvalue'  
        }      
        Invoke-RestMethod -Uri $request -Headers $authHeader -Method Get -Body $Body  
    }  
    

    Also note that before executing the above runbook you would have to give Contributor role access to your RunAsAccount as prerequisite to avoid authorization error i.e.,

    1. Go to Azure Portal Home -> Your Automation account -> Connections tile -> Click on AzureRunAsConnection -> Copy the ApplicationID.
    2. Go to Azure Portal Home -> Azure Active Directory -> App registrations -> All applications -> Paste the copied ApplicationID from the above step -> Click on the listed Application -> Copy the Display name.
    3. Go to Azure Portal Home -> Subscriptions -> Click on your subscription -> Access control (IAM) -> Add -> Add role assignment -> Role: Contributor -> Paste the copied App Display name in Select section -> Click on it and click save.

    For illustration, please find below screenshots.

    112232-image.png

    112251-image.png

    112233-image.png


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.