This post is all about connecting runbook to the Azure and perform the
task in simple words you can create your script and put it in the runbook panel
but it wouldn't work until you will set the authentication. There are ways you
can authenticate your runbooks and we will be discussing PS credential and
automation connection.
Below is the code that would help you creating the PS
credential for your runbook
$user =
"MyDomain\MyUser"
$pw =
ConvertTo-SecureString "PassWord!" -AsPlainText -Force
$cred = New-Object
–TypeName System.Management.Automation.PSCredential –ArgumentList $user, $pw
New-AzureAutomationCredential
-AutomationAccountName "MyAutomationAccount" -Name
"MyCredential" -Value $cred
Now once the PS Cred is created you can retrieve these via Get-automationPSCrendential.
Your code in the runbook would look like this :
$myCredential =
Get-AutomationPSCredential -Name 'MyCredential'
$userName =
$myCredential.UserName
$securePassword =
$myCredential.Password
$password =
$myCredential.GetNetworkCredential().Password
$myPsCred = New-Object
System.Management.Automation.PSCredential ($userName,$password)
Connect-AzureRmAccount -Credential $myPsCred
Now lets talk about authenticating via Azure connections.
Below is the code that would help you get it done and once authentication is
done then you can perform your script like starting or stopping VM. You can
also link you runbook with the schedule as well.
$connection =
Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzureRmAccount
-ServicePrincipal -Tenant $connection.TenantID `
-ApplicationID
$connection.ApplicationID -CertificateThumbprint
$connection.CertificateThumbprint
Most common error that we face is mentioned below and error
itself says something is wrong with the module means module is not updated and
you need to update module and you can not update it from the runbook or
automation account panel like all days but you can import it from PS Gallery
and better use Update
Azure modules runbook repository on GitHub
and you also refer below links -
“Connect-AzureRmAccount
not recoConnect-AzureRmAccount : The term 'Connect-AzureRmAccount' is not
recognized as the name of a cmdlet, function, script Azure runbook”
Once you update your module above mentioned error would be
fixed and your runbook will work like charm.
No comments:
Post a Comment