I'm trying to setup a Powershell Runbook to access Azure resources.
I'm following instructions on this page.
I've assigned a system identity to the Automation account, the the sample script simply doesn't work.
# Sign in to your Azure subscription
$sub = Get-AzSubscription -ErrorAction SilentlyContinue
if(-not($sub))
{
Connect-AzAccount
}
Output:
Run Connect-AzAccount to login.
Port 8400 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
Port 8401 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
<snip>
Port 8998 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
Port 8999 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
Unable to acquire token for tenant 'organizations' with error 'Cannot find an open port.'
Cannot find an open port.
I tried adding Contributor and Owner role to the identity, and that didn't help.
I'm not sure what I'm missing, help?
I am using terraform to stand up the infrastructure, so it's possible I have a setting out of whack that terraform is setting.
CodePudding user response:
The snippet of code you have used is for authentication to enable the identity.
The snippet of code further down that page is what you need to use:
You'll notice it utilises Connect-AzAccount -Identity
instead.
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext