Run cosmosdb commands from powershell - powershell

I am trying to connect to azure cosmosdb from my local machine via powershell but every command I tried to run it returns the "Argument passed in is not serializable."
Here are a few of my commands,
Get-AzCosmosDBAccount -ResourceGroupName "cosmosbackup"
Invoke-AzCosmosDBSqlDatabaseThroughputMigration -ResourceGroupName "cosmosbackup" -AccountName "liabilitydata" -Name liability
New-AzCosmosDBSqlContainer -AccountName "liabilitydata"-DatabaseName "dailyliability"-ResourceGroupName "cosmosbackup"-Name schemes -PartitionKeyPath /Id -PartitionKeyKind Hash
Get-AzCosmosDBSqlContainer `
-ResourceGroupName "cosmosbackup" `
-AccountName "liabilitydata" `
-DatabaseName "dailyliability"
All of them fail for the same reason Argument passed in is not serializable.
Am I missing something? Please help

The issue here is that you need to set the context for running the script,
Step 1 : Connect with your Azure account
Connect-AzAccount
Step 2 : Pass the resource group and the cosmosdb account name as follows,
Get-AzCosmosDBAccount -ResourceGroupName cosmosbackup

Related

Switch-AzureRmWebAppSlot does not swap the physical site only the application settings

I have been running the following command via powershell for AZURE but all that gets swapped are the application settings:
Switch-AzureRmWebAppSlot -ResourceGroupName 'myresourcegroup' -Name 'mywebsitename' -SourceSlotName "staging" -DestinationSlotName "production" -confirm -verbose
The same thing happens when I run this command:
Switch-AzureRmWebAppSlot -ResourceGroupName 'myresourcegroup' -Name 'mywebsitename' -SourceSlotName "staging" -DestinationSlotName "production" -SwapWithPreviewAction CompleteSlotSwap -confirm -verbose
I cannot use Switch-AzureWebsite as I cannot set a default subscription with my permissions.
Using the Login-AzureRMAccount the only way I found to switch slots is as follows:
$ParametersObject = #{targetSlot = "production"}
$RGN = 'resource-group-name-'
Invoke-AzureRmResourceAction -ResourceGroupName $RGN -ResourceType Microsoft.Web/sites/slots -ResourceName website-name/staging -Action slotsswap -Parameters $ParametersObject -Verbose -force
This was hard to find, in part because many examples required you to set your Azure default subscription prior to executing Switch-AzureWebsite and the other switch only moved the configuration elements over. I am curious if anyone has a clever way to discover power shell commands or ARM template commands aside from the ones generated for deployments on Azure. Ideally, I could perform an action on Azure an then see same thing scripted as PowerShell.
REF: Microsoft Docs Here

Update-AzureRmVmss : Required parameter 'adminPassword' is missing (null)

I am trying to run a PowerShell vmss custom extension script on scale set.
I get this error when it tries to run the Update-AzureRmVmss command
Update-AzureRmVmss : Required parameter 'adminPassword' is missing (null).
ErrorCode: InvalidParameter
ErrorMessage: Required parameter 'adminPassword' is missing (null).
StatusCode: 400
ReasonPhrase: Bad Request
$customConfig = #{
"fileUris" = #("https://$storageAccountName.blob.core.windows.net/scripts/script.ps1");
"commandToExecute" = "PowerShell -ExecutionPolicy Unrestricted .\script.ps1";
};
# Add the extension to the config
$vmss = Get-AzureRmVmss -ResourceGroupName $resourceGroup -VMScaleSetName $vmssname
Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss -Publisher Microsoft.Compute -Type CustomScriptExtension -TypeHandlerVersion 2.0 -Name "runscript" -Setting $customConfig
# Send the new config to Azure
Update-AzureRmVmss -ResourceGroupName $resourceGroup -Name "runscript" -VirtualMachineScaleSet $vmss
I figured out the issue.
The -Name needs to be scaleset name. The code I got from online had the name as the name of the script which was wrong.
Update-AzureRmVmss -ResourceGroupName $resourceGroup -Name "scalsetname" -VirtualMachineScaleSet $vmss
It might be easier to use the PowerShell cmdlet or CLI commands to add an extension directly..
PowerShell: Add-AzureRmVmssExtension
CLI: az vmss extension set
The Azure Cloud Shell has a built in authenticated version of CLI.
The correct parameter for Update-AzureRmVmss is -VMScaleSetName which also has an alias called Name.
I was also getting the same error using -Name parameter,
but when I tried using -VMScaleSetName in place of -Name, I do not see the error.
Here is the official documentation for reference: https://learn.microsoft.com/en-us/powershell/module/azurerm.compute/update-azurermvmss?view=azurermps-6.9.0
Update-AzureRmVmss -ResourceGroupName $resourceGroup -VMScaleSetName "scalesetname" -VirtualMachineScaleSet $vmss
If found this thread by searching for Required parameter 'adminPassword' is missing (null), but my solution was a different one.
Turns out that you get this message also if your chosen password does not match the security requirements of the VM (or whatever) you are installing. It is a confusing way to express that issue, to say the least, so maybe this helps someone.

Azure Rotate Storage Keys and Update ADF Linked Service

I am looking for a way to implement doing key rotation in an Azure Automation I have found a way to create a powershell runbook and have implemented the following code:
$azureAccountName = <acct_name>
$azurePassword = ConvertTo-SecureString <pass> -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAccountName, $azurePassword)
Login-AzureRmAccount -ServicePrincipal -Credential $psCred -TenantId <tenant id> -SubscriptionId <sub id>
#Optionally you may set the following as parameters
$StorageAccountName = <storage acct name>
$RGName = <rg name>
#Key name. For example key1 or key2 for the storage account
New-AzureRmStorageAccountKey -ResourceGroupName $RGName -Name $StorageAccountName -KeyName "key1" -Verbose
New-AzureRmStorageAccountKey -ResourceGroupName $RGName -Name $StorageAccountName -KeyName "key2" -Verbose
When I ran this, it worked, however, it broke my Azure Data Factory Linked Service. I realized that the connection string for the linked service is broken, so I set out to try to reset the connection string in the automation script. I was able to get the connection string by doing:
(Get-AzureRmDataFactoryLinkedService -DataFactoryName <adf name> -ResourceGroupName <rg name> -Name <ls name>).Properties.TypeProperties.ConnectionString
I cannot find a way to set this connection string using powershell and azure automation.
You could use Power Shell to rest this connection. But you need use Remove-AzureRmDataFactoryLinkedService (Removes a linked service from Azure Data Factory.) and use New-AzureRmDataFactoryLinkedService to re-link your storage account to data factory.
Please refer to this tutorial.
You need create a json file like below:
{
"name": "AzureStorageLinkedService",
"properties": {
"type": "AzureStorage",
"typeProperties": {
"connectionString": "DefaultEndpointsProtocol=https;AccountName=<accountname>;AccountKey=<accountkey>"
}
}
}
Use New-AzureRmDataFactoryLinkedService to link.
New-AzureRmDataFactoryLinkedService -ResourceGroupName ADFTutorialResourceGroup -DataFactoryName <Name of your data factory> -File .\AzureStorageLinkedService.json
But if you use Azure automation to execute this, there is a issue you will meet. On runbook, you could not store a json file, maybe you could save on a public github(no safe). Another solution is use Hybrid Runbook Worker.

List instances of Azure App Service with Powershell RM

My aim is to find all the instance Ids of a particular App Service in Azure so I can write a warm-up routine and test it against all running instances (ARRAfinity).
I can do the following with ASM Powershell but I need it in ARM (RM) as Octopus is configured for it.
(Get-AzureWebsite -Name "site-name" -Slot "Production").Instances
I have found the documentation around RM sparing, and the following hasn't led me to anything helpful:
Get-AzureRmWebApp -Name "site-name"
Any help would be really helpful.
Try something like this:
Get-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/instances -Name $WebAppName -ApiVersion 2016-03-01
See also here for a helper function that also works on slots.
Or you can use:
Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName
Resource cmdlets should run faster than Get-AzResource (Get-AzureRmResource), specially with heavy scripts.

AzureRM Automation PSWorkflow Runbook - error while attaching NIC to subnet

I'm trying to create a parallel Workflow script but facing some challenges while attaching NIC to existing Subnet. I'm getting the following error.
Microsoft.PowerShell.Utility\Write-Error : Cannot validate argument on
parameter 'SubnetId'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
Following is my Workflow script which creates VNet, Subnet, NSG, PIP and tries to create NIC from AzureRM automation Runbook.
"Get Virtual Network Information" $gvnet = Get-AzureRmVirtualNetwork
-Name $VNetName -ResourceGroupName $SharedResourcesRGName
$nic = New-AzureRmNetworkInterface -Name "$VMName-NIC"
-ResourceGroupName $VMName -Location $VMLocation `
        -SubnetId $gvnet.Subnets[0].ID -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $gnsg.Id -Force
How do I get the value of Subnet in AzureRM Workflow runbook
Gulab Pasha