Importing module command line in Azure Devops pipeline - powershell

I have set of unit test case and they are invoking a set of PowerShell commands. everything works fine on local machine but once pushed in to Azure pipelines, I get error as "New-CosmosDbContext is not recognized. Please check the spelling". It seems the Import command is not working when pushed to azure devops pipeline. Any idea as how we can fix this?
The lines what I am using is :
Import-Module "$env:ProgramFiles\Azure Cosmos DB Emulator\PSModules\Microsoft.Azure.CosmosDB.Emulator"
Start-CosmosDbEmulator
Get-CosmosDbEmulatorStatus
$cosmosDbContext = New-CosmosDbContext -Emulator

As your follow-up question implies, you have since realized that two different modules are involved, and that you also need to import the third-party CosmosDB module in order to use its New-CosmosDbContext command:
Microsoft.Azure.CosmosDB.Emulator is the official Azure CosmosDB module.
CosmosDB is a third-party module.
Therefore, you need to first install the module (from the Powershell Gallery) - e.g., Install-Module CosmosDB -Scope CurrentUser - and then import it - ImportModule CosmosDB - before you can call New-CosmosDbContext

Related

Getting Database names from SQLAzure using powershell in azure devops

I have a need to list down all the database names of a particular server on Azure.
$databases = Get-AzSqlDatabase -ServerName servername -ResourceGroupName resourcegroupname
foreach($dbs in $databases)
{
$dbs.DatabaseName
}
this script works well run from local.
My question is how to run this from an Azure DevOps release using the powershell task?
You have to use the Azure-Powershell task.
That way the pipeline can connect to the Proper Azure Subscirption, just like you connected to the Subscription manually from Powershell
Details about the Azure Powershell Task: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops
But them I wonder what you want to do with the List of the databases.
Going out on a limb here, but you'd probably want the output back in the Pipeline. In which case please check out
https://github.com/microsoft/azure-pipelines-agent/blob/master/docs/preview/outputvariable.md
Write-Host "##vso[task.setvariable variable=var01;isOutput=true]123"

How to transfer a html file from Azure VM via Azure powershell or Azure CLI to a local machine

I am working on developing a Automated QA script for my project for my organisation. My goal is to execute pester scripts through custom script extension feature of azure vms. I got the Pester executed and result exported as a nunit xml. I would like to fetch the xml back from VM to my local machine. One way of doing that is by uploading the xml into blob storage from VMs. but since it requires azure connection to be established in VM using SP account. I dont prefer this method.
I would like to know the best way to retrive pester results and get it outside VM.
Any help is much appreciated. Thanks .
I'd use a shared access signature token for that (link). that way your script doesnt really need SP, it just needs the token. that token would limit permissions to only upload file to specific container (or even blob).
$sascontext = New-AzureStorageContext -StorageAccountName accountname -SasToken '?tokenvalue'
Set-AzureStorageBlobContent -File path -Container name -Context $sascontext -Force
You can create new token with New-AzureStorageBlobSASToken or New-AzureStorageContainerSASToken
Your only requirement would be to install Azure.Storage module before hand.

ServiceEndpoint and ResourceManagerEndpoint values do not match existing environment. Please use Environment parameter

I have a PowerShell script running in Octopus Deploy as part of my deployment process. An extract of the script is below:
Import-AzurePublishSettingsFile "myAzurePublishSetting.PublishSettings"
Select-AzureSubscription 'mySubscription'
Set-AzureSubscription -SubscriptionName 'mySubscription' -Environment 'myEnvironment' -CurrentStorageAccountName 'myStorageAccount'
I'm now getting the below error from the Set-AzureSubscription cmdlet:
ServiceEndpoint and ResourceManagerEndpoint values do not match existing environment. Please use Environment parameter.
at Microsoft.WindowsAzure.Commands.Profile.SetAzureSubscriptionCommand.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Octopus Deploy is hosted in an Azure virtual machine. This script worked fine until a few days ago so maybe Azure has changed something since nothing else has changed.
It's even more puzzling since I can run this script successfully on the virtual machine in both a PowerShell window and using Calamari.exe which is apparently what Octopus uses under the hood to call the script.
Any ideas?
This can happen if you have made changes to Azure subscriptions, for example disabling a subscription. Powershell still has a cache of the previous subscriptions. Use Get-AzureAccount to get the Id of the account and then Remove-AzureAccount. Finally, add the account again using Add-AzureAccount.

Powershell Azure : The term 'Get-AutomationConnection' is not recognized as the name of a cmdlet, function, script file, or operable program

I am trying to connect to an Azure Run As connection, as part of a Powershell script that does a backup of a database.
This script attempts to call Get-AutomationConnection
As seen in the screenshot, Get-Module does return that Azure / Azure.Storage and AzureRM shows.
What module should I import in addition for this to work?
If you want to connect to an Azure Run As connection from Windows PowerShell, you should use New-AzureRmAutomationConnection.
$ConnectionAssetName = "AzureRunAsConnection"
$ConnectionFieldValues = #{"ApplicationId" = $Application.ApplicationId; "TenantId" = $TenantID.TenantId; "CertificateThumbprint" = $Cert.Thumbprint; "SubscriptionId" = $SubscriptionId}
New-AzureRmAutomationConnection -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccountName -Name $ConnectionAssetName -ConnectionTypeName AzureServicePrincipal -ConnectionFieldValues $ConnectionFieldValues
You are able to use the script to create the connection asset because when you create your Automation account, it automatically includes several global modules by default along with the connection type AzurServicePrincipal to create the AzureRunAsConnection connection asset.
Get-AutomationConnection runs in Azure runbook internally.
Please refer to connection assets in Azure Automation.
If you want similar functionality to runbooks on-premise, you can install AzureAutomationAuthoringToolkit. It will give you very similar functionality. I have one script that logs in using the service principal, whether it is running on-premise or in an Azure runbook. It uses the resources provided by AAATK when running on-premise, that simulate a runbook.
I did try using the version of Get-AutomationConnection that comes with the "Microsoft Monitoring agent" (Hybrid worker), but I have since read that it is different to the one that comes with AzureAutomationAuthoringToolkit, detailed in the "Known Issues" in the GitHub readme. I couldn't get it to work, so I reverted to AAATK's version.

Get Azure VM Detail by PowerShell

I am trying to run Get-AzureVM PowerShell command, it is running fine but not return any output.
Also tried in following flavor but still blank result any idea?
Get-AzureVM -Name "vmname" |Select-Object name,instancesize,location
You should call Select-AzureSubscription "subscription name" first.
It likely is defaulting to a subscription that doesn't have any virtual machines in it.
To view your current subscription names call:
Get-AzureSubscription | select SubscriptionName
Actually the answer above is only semi-correct.
This had me pulling my virutal hair out trying to do automation (which took 7 hours of manual fudging to get working!).
Simply, you have two types of virtual machine in Azure; Classic, and Resource Manager.
If you Switch-AzureMode -name AzureServiceManagement then use Get-AzureVM you will list all of the classic VM's you have created.
If you Switch-AzureMode -name AzureResourceManager then use Get-AzureVM you will list all of the Resource Manager (or new) VM's you have created.
And remember, if you are trying to do automation, then you need the VM's in the new mode available through the portal, your old VM's (classic) that you created through management are not visable in this mode and you will have to recreate them.
Azure has two types of Management System: AzureServiceManagement (ASM) and AzureResourceManager (ARM)
In order to control these two different type of management systems you should switch between them as described in the main page of the Azure Powershell Github project page, but this is true for the azure powershell versions lower than 1.0.0, you can find more explanation in here
For those who are interested to control ARM (AzureResourceManager) with the powershell version greter than 1.0.0, they should use all Cmdlets with the following format : [Verb]-AzureRm[Noun], for example New-AzureVm becomes New-AzureRmVm, in our case Get-AzureVM became Get-AzureRmVm
In summary:
Powershell versions lower than 1.0.0 you should switch between modes and use Get-AzureVM, which is very confusing in my and lots of others opinion
Powershell versions equal or greater than 1.0.0 you should use Get-AzureVM for ASM and Get-AzureRmVm for ARM.
I know this question has been answered but I tried the answer given and it did not work for me. I found, I needed to switch my AzureMode.
To resolve, I ran the following powershell script.
Switch-AzureMode -Name AzureResourceManager
Switching Azure Powershell mode between AzureServiceManagement and AzureResourceManger is a possible solution if your script is using older features as well as new Azure Resource Manager cmdlets. The switch is needed only for Microsoft Azure Powershell version 0.9.8 or older.