How to run azure power shell by ansible playbook? - azure

Now i want to run some power shell scipts like New-AzAutomationRunbook -AutomationAccountName 'Testing' -Name 'Runbook02' -ResourceGroupName 'rg' -Type Python3
How to do it by ansible-playbook ?

We can achieve the above requirement by following this Blog here to configure in our local.
The same command we can run with PowerShell as well to create runbook for our Automation Accounts.
Screenshot for reference:-
For more information please refer this MICROSOFT DOCUMENTATION:- Using Ansible with Azure

Related

Error when run Az powershell create Runbook

I create new runbook by ansible-playbook like that
name: Connect azure account
command: pwsh -Command "(Connect-AzAccount -Identity).context"
name: Create runbook
command: pwsh -Command "New-AzAutomationRunbook -AutomationAccountName 'Testing' -Name 'Runbook02' -ResourceGroupName 'hoadtn_ansible_rg' -Type Python3"g
But when I run , it show error
how to resolve this error ?
thanks
For the above error please make sure that you have updated Ansible version which is 2.9 or above.
Then run the command az login once successfully run the command then run the following command to create runbook in your azure automation .
We have tried the same and works fine :
For more information please refer the below links:-
Ansible Installation guide
SO THREAD: Ansible install all required Azure modules

Run PowerShell from Azure DevOps

I have a task: to install solution (wsp) on remote machine using Azure DevOps Pipelines.
I have folder with *.wsp. And I have a PS-script. If I do it manually on this machine it works fine. No errors.
But when I use Azure DevOps I have a message:
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
At C:\Windows\SysWOW64\WindowsPowerShell\v1.0\profile.ps1:1 char:1
+ Add-PSSnapin "Microsoft.SharePoint.Powershell"
At the very beginning of my PS script I have this:
Add-PSSnapin "Microsoft.SharePoint.Powershell"
I need it, because I use Uninstall-SPSolution, Remove-SPSolution, Add-SPSolution and Install-SPSolution cmdlets inside script.
I've tried to add
Add-PSSnapin "Microsoft.SharePoint.Powershell"
to "profile.ps1", I've tried to run powershell inside powershell. Again and again error is the same. I don't understand how to fix it.
https://i.stack.imgur.com/UNM7R.png
https://i.stack.imgur.com/NTiWs.png
https://i.stack.imgur.com/sA6tU.png
https://i.stack.imgur.com/bK9kq.png
Try preceding your command with Get-PSSnapIn:
if (Get-PSSnapin "Microsoft.SharePoint.Powershell") {
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
At C:\Windows\SysWOW64\WindowsPowerShell\v1.0\profile.ps1:1 char:1
Based on the error message, when the powershell execute the ps file, it get the error.
In Auzre Devops Powershell task, it will use the powershell.exe from C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
The exe path you use to execute the powershell file is different from the default.
You could run the following command in azure devops:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Get-PSSnapin -Registered
and
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Get-PSSnapin -Registered
You can check if the Microsoft.SharePoint.Powershell exists.
Then you could use the correct powershell.exe path to execute the ps file.
For example:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe "path/xx.ps"

Use the az installer on a VM - through azure powershell

I am attempting to install an artifact through an azure VM - I have remoted into the VM through the powershell on azure and am running the following command:
Enter-AzVm -Name <MyVM> -ResourceGroupName <MyResourceGroup> -Credential (get-credential)
When I'm connected onto the VM I'm attempting this download command which works on my local machine:
az artifacts universal download --organization <MyOrganization> --project=<MyProject> --scope project --feed <MyFeed> --name <MyFirstPackage> --version 0.0.1 --path .
This is the error I'm getting while attempting that download :
The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program
I tried to run the command to install CLI on the VM which runs, but after that installation run I cannot see az --version with the same error. Any help would be appreciated.
I have never used Enter-AzVm to remote into the VM to install Azure CLI, seems the Enter-AzVm just works in Azure Cloud Shell(not sure).
I installed the Azure CLI successfully via Invoke-AzVMRunCommand command in a Windows VM, when I test az --version in cloud shell after remoting into the VM with Enter-AzVm, it works fine, you could follow the steps below.
1.Save the command below in local as a installcli.ps1 file.
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
2.Run the command below in local.
Invoke-AzVMRunCommand -ResourceGroupName 'groupname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joyw\Desktop\installcli.ps1'
3.After the command completed, navigate to the cloud shell, use Enter-AzVm to remote into the VM, then run az --version, it works fine.
Actually, if the steps above not work for you, you can also store the azure cli command as a .ps1 file like step 1, then use Invoke-AzVMRunCommand to run it, it will work.

Powershell PSPKI module is not working in azure automation account

I've imported PSPKI modules in to azure automation account, when using the cmdlets of PSPKI I always get command not recognized. I could see the module is installed successfully and could see the cmdlets in my runbook, but somehow the cmdlets are not recognized.
for example Test-WebServerSSL -URL login.live.com
I can reproduce your issue, to fix the issue, run Import-Module -Name PSPKI first.
Import-Module -Name PSPKI
Test-WebServerSSL -URL login.live.com

Azure runbook automation - Invoke-AzureRmVMRunCommand error

I have an Azure runbook that runs on schedule. Its in powershell and this runbook starts a VM and executes a script on the VM started. How I achieve this is
1) Store the script to be run on the VM in a storage account
2) Run powershell runbook
3) Powershell runbook uses wget command to copy the script from step 1
4) Invoke-AzureRmVMRunCommand in the Azure automation powershell commands as shown below
wget "https://utilitystorageaccnt.blob.core.windows.net/utilitycontainer/token" -outfile ((Get-Location).path + "\Reporting Copy.ps1") -UseBasicParsing
Invoke-AzureRmVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VmName -CommandId 'RunPowerShellScript' -ScriptPath ((Get-Location).path + '\Reporting Copy.ps1') -ErrorVariable result
Please not that the above two commands are in the powershell runbook script and not the actual script that is run on the VM.
Facing two issues
1) When this script Reporting Copy.ps1 runs standalone on the VM, then it works properly and it has no issues. When it is run using the runbook, I get these errors in the log file on the target vm.
"New-AzStorageContext : The term 'New-AzStorageContext' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again."
2) Even after this error occurs, it doesnt terminate and runs in loops. This script does a copy operation and it keeps looping until all the copy is complete. I can handle code to terminate but I would like to know how to force terminate a runbook. I tried to stop the VM for even a hour and it resumes the copy operation. The runbook status in Azure shows as completed. There are two python processes that show in explorer and terminating them doesn't work either.
Any help or hint is appreciated.
Thanks.
Look like you did not imported Az PowerShell module into our Automation Account.
Please, follow this tutorial : Az module support in Azure Automation
Try to use only Az module and not AzureRM
The issue was because I had not installed the AZ module for all users like this.
Install-Module -Name Az -AllowClobber -Scope AllUsers
Instead I had used
Install-Module -Name Az -AllowClobber -Scope CurrentUser
and since the automation runs on a different user, the issue occurred. Thanks for your help.

Resources