Azure / Azure/azure-powershell

Invoke-Command and AzureRM.Automation Issue

Open
#5,363 10 comments 0 reactions 1 assignee Assigned to @GaneshMSAzure View on GitHub
act-codegen-extensibility-squad Automation automation-modules Service Attention
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
2d 17h
Merged PRs (30d)
51

Description

### Cmdlet(s)
Start-AzureRMAutomationRunbook but other AzureRM.Automation cmdlets fail too.

### PowerShell Version
5.1.14393.1944

### Module Version
5.1.1 AzureRM
4.1.1 AzureRM.Automation
4.1.1 AzureRM.profile

### OS Version
10.0.14393.1944

### Description
I'm trying to start a runbook in Azure Automation using invoke-command on a remote computer after logging in with a service principal. Other cmdlets work but Azure Automation based cmdlets fail with:

`Your Azure credentials have not been set up or have expired, please run Login-AzureRMAccount to set up your Azure credentials.`

My uneducated guess is that it's not using the saved AzureRM context.

### Debug Output
Just FYI, I sanitized the GUIDs in this debug output
```

DEBUG: 6:33:18 PM - StartAzureAutomationRunbook begin processing with ParameterSet 'ByAsynchronousReturnJob'.
DEBUG: 6:33:21 PM - using account id 'accountid'...
DEBUG: [Common.Authentication]: Authenticating using Account: 'accountid', environment: 'AzureUSGovernment', tenant: tenantid'
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
DEBUG: 01/24/2018 18:33:21: - TokenCache: Serializing token cache with 1 items.

DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
DEBUG: 01/24/2018 18:33:21: - TokenCache: Serializing token cache with 1 items.

DEBUG: [Common.Authentication]: Authenticating using configuration values: Domain: 'domainid', Endpoint: 'https://login.microsoftonline.us/',
ClientId: 'clientid', ClientRedirect: 'urn:ietf:wg:oauth:2.0:oob', ResourceClientUri: 'https://management.core.usgovcloudapi.net/', ValidateAu
thrity: 'True'
DEBUG: [Common.Authentication]: Renewing token using AppId: 'appid', AdalConfiguration with ADDomain: 'domainid', AdEndpoint: 'https://login.microsoftonline.us/', ClientId: 'clientid', RedirectUri: 'urn:ietf:wg:oauth:2.0:oob'
DEBUG: [Common.Authentication]: Received exception Unable to retrieve service key for ServicePrincipal account accountid. Please log in again to supply the credentials for this service principal. In PowerShell, execute Login-AzureRMAccount for Azure Resource Manager cmdlets or Add-AzureAccount for service management cmdlets., while authenticating.
Your Azure credentials have not been set up or have expired, please run Login-AzureRMAccount to set up your Azure credentials.
+ CategoryInfo : CloseError: (:) [Start-AzureRmAutomationRunbook], ArgumentException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Automation.Cmdlet.StartAzureAutomationRunbook
+ PSComputerName : adbdc

```
### Script/Steps for Reproduction

I'm using the following script:

```
param(
[string]$tenantId,
[string]$environmentName,
[string]$applicationID,
[string]$password,
[string]$invokeComputer,
[string]$azureRunbookName,
[string]$automationAccountName,
[string]$automationRG,
[hashtable]$runbookParams
)

$ScriptBlock = {

$secpasswd = ConvertTo-SecureString $using:password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($using:applicationID, $secpasswd)

login-azurermaccount -tenantid $using:tenantId -environmentname $using:environmentName -Credential $mycreds -ServicePrincipal

$job = Start-AzureRmAutomationRunbook -Name $using:azureRunbookName -Parameters $using:runBookParams -AutomationAccountName $using:automationAccountName -ResourceGroupName $using:automationRG -ErrorAction "Stop"
}

Invoke-Command -ComputerName $invokeComputer -ScriptBlock $ScriptBlock

```

You can also use:
```

Enter-PSSession -Computer
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($applicationid, $secpasswd)
login-azurermaccount -tenantid $using:tenantId -environmentname $using:environmentName -Credential $mycreds -ServicePrincipal
Get-AzureRMAutomationAccount
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.