Scaling CosmosDB Container using Powershell - azure

I am trying to scale the CosmosDB Container using Powershell but couldn't find anything in the docs. I tried the following script which didn't work.
$resourceName = $CosmosDB + "/sql/" + $CosmosDatabase + "/" + $CosmosContainer
$ContainerProperties = #{
"resource"=#{
"id"=$CosmosContainer;
"partitionKey"=#{
"paths"=#("/DefaultKey");
"kind"="Hash"
}
};
"options"=#{ "Throughput"=$CosmosScale }
}
Set-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroup -Name $resourceName -PropertyObject $ContainerProperties -Force
Any insights are appreciated.

Here is a PS script that will update throughput on either a database or container for a SQL (Core) API account.
# Update RU for an Azure Cosmos DB SQL (Core) API database or container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container1"
$databaseResourceName = $accountName + "/sql/" + $databaseName + "/throughput"
$containerResourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName + "/throughput"
$throughput = 500
$updateResource = "database" # or "container"
$properties = #{
"resource"=#{"throughput"=$throughput}
}
if($updateResource -eq "database"){
Set-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/settings" `
-ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
-Name $databaseResourceName -PropertyObject $properties
}
elseif($updateResource -eq "container"){
Set-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers/settings" `
-ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
-Name $containerResourceName -PropertyObject $properties
}
else {
Write-Host("Must select database or container")
}

I have used this to update the Azure CosmosDb (SQL API) Collections on automation account but which gets timed Out with the cmdlet Set-AzResource - sometimes it works
Get-AzResource -ResourceType Microsoft.DocumentDB/databaseAccounts
$containerResourceType = "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers/settings"
Using Set command with API Version in Runbook which got failed with ‘Operation failed because a request timed out.’
Set-AzResource -ResourceType $containerResourceType ***
Reference:
https://serverfault.com/questions/967942/scaling-cosmosdb-container-using-powershell

Related

Setup and deploy Azure function using PowerShell

I try to setup and deploy Azure Function by using PowerShell script based on this topic: Setup Azure Function from PowerShell
My script looks like this:
#=============Defining All Variables=========
$location = 'Central US'
$resourceGroupName = 'MyResourceGroup'
$subscriptionId = 'MysubscriptionId'
$functionAppName = 'MyfunctionAppName'
$appServicePlanName = 'ASP-test-8b50'
$tier = 'Dynamic'
$archivePath = 'd:\TestAzureFunc.zip'
Connect-AzAccount
#========Creating Azure Resource Group========
$resourceGroup = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $resourceGroupName }
if ($resourceGroup -eq $null)
{
New-AzResourceGroup -Name $resourceGroupName -Location $location -force
}
#selecting default azure subscription by name
Select-AzSubscription -SubscriptionID $subscriptionId
Set-AzContext $subscriptionId
#========Creating App Service Plan============
New-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $appServicePlanName -Location $location -Tier $tier
$functionAppSettings = #{
ServerFarmId="/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Web/serverfarms/$appServicePlanName";
alwaysOn=$True;
}
#========Creating Azure Function========
$functionAppResource = Get-AzResource | Where-Object { $_.ResourceName -eq $functionAppName -And $_.ResourceType -eq "Microsoft.Web/Sites" }
if ($functionAppResource -eq $null)
{
New-AzResource -ResourceType 'Microsoft.Web/Sites' -ResourceName $functionAppName -kind 'functionapp' -Location $location -ResourceGroupName $resourceGroupName -Properties $functionAppSettings -force
}
#========Defining Azure Function Settings========
$AppSettings =#{}
$AppSettings =#{'FUNCTIONS_EXTENSION_VERSION' = '~2';
'FUNCTIONS_WORKER_RUNTIME' = 'dotnet';}
Set-AzWebApp -Name $functionAppName -ResourceGroupName $resourceGroupName -AppSettings $AppSettings
#========Deploy Azure Function from zip========
Publish-AzWebapp -ResourceGroupName $resourceGroupName -Name $functionAppName -ArchivePath $archivePath
The script works without errors. Resource group and Function App created as needed. But the list of functions of the Function App is empty.
Function details here:
My intuition tells me that I've forgotten something. But I don't know what.
Could you advise me on how to deploy my Azure function properly?
One of the workaround you can follow ,
Looking at your script we need to ensure that we are providing function app configuration as below cmdlts the link you followed:-
$AzFunctionAppSettings = #{
APPINSIGHTS_INSTRUMENTATIONKEY = $AppInsightsKey;
AzureWebJobsDashboard = $AzFunctionAppStorageAccountConnectionString;
AzureWebJobsStorage = $AzFunctionAppStorageAccountConnectionString;
FUNCTIONS_EXTENSION_VERSION = "~4";
FUNCTIONS_WORKER_RUNTIME = "dotnet";
}
And also make sure that the storage account connection string you provided in the function is same as here providing.
And then you can navigate to Kudu API to check the wwwroot folder is exist or not.
For more information please refer the below links:-
SO THREAD|Powershell command Publish-AzWebApp not publishing apllication
BLOG|How to Deploy Azure Function Apps With Powershell.

Deploying multiple scripts with CustomScriptExtension on an Azure Windows VM

I'm trying to deploy multiple PowerShell scripts to an Azure Windows VM by using the CustomScriptExtension.
The scripts are independent from each other so the order of their deployment doesn't matter. Also the scripts are in same blob container.
I have already a working template as below for installing one script at the moment:
$resourceGroup = "myResourceGroup"
$vmName = "myVM"
$location = "easteurope"
$storageAcctName = "myStorageAcct"
$storageKey = $env:ARM_ACCESS_KEY
$fileUri = #("https://xxxxx.blob.core.windows.net/xxxx/01-installTools.ps1")
$protectedSettings = #{"storageAccountName" = $storageAcctName; "storageAccountKey" = $storageKey};
$settings = #{"fileUris" = $fileUri;"commandToExecute" ="powershell -ExecutionPolicy Unrestricted -File 01-installTools.ps1"};
#run command
Set-AzVMExtension -ResourceGroupName $resourceGroup `
-Location $location `
-ExtensionName "IIS" `
-VMName $vmName `
-Publisher "Microsoft.Compute" `
-ExtensionType "CustomScriptExtension" `
-TypeHandlerVersion "1.8" `
-Settings $settings `
-ProtectedSettings $protectedSettings;
Also is it possible to use 2 CustomScriptExtensions?
I get a TypeHandlerVersion error if i try to do that.

Attempting to enable web server logging on multiple on Azure web apps with quota and retention specified

Can someone please help me with my powershell script below? The issue that I'm facing is the "ResourceName" flag is the name of the web app that is being configured, so I need to have an array for that and have the ForEach-Object loop run against that array.
$ResourceGroupName = 'webapptestrg'
$webapps = Get-AzWebApp -ResourceGroupName 'webapptestrg'
$ResourceName = foreach ($webapp in $webapps) {$webapp.name}
$PropertiesObject = #{
httpLogs = #{
fileSystem = #{
enabled = $TRUE;
retentionInMb = 35;
retentionInDays = 7;
}
}
}
Get-AzResource -ResourceGroupName $ResourceGroupName | ForEach-Object {
Set-AzResource -Properties $PropertiesObject `
-ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/config `
-ResourceName $ResourceName.name/logs -ApiVersion 2018-11-01 -Force
}
Update. I had some help internally and we got it figured out. For anyone that needs this in the future, here's the updated code.
$ResourceGroupName = 'rgname'
$webapps = Get-AzWebApp -ResourceGroupName 'rgname'
$ResourceName = foreach ($webapp in $webapps) {$webapp.name}
$PropertiesObject = #{
httpLogs = #{
fileSystem = #{
enabled = $TRUE;
retentionInMb = 35;
retentionInDays = 7;
}
}
}
Foreach ($resource in $ResourceName) {
Set-AzResource -Properties $PropertiesObject `
-ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/config `
-ResourceName "$resource/logs" -ApiVersion 2018-11-01 -Force
}

Azure Automation Account - Runbook Error - Object reference not set to an instance of an object

Last week, I deployed a script to backup some disks using the New-AzSnapshot cmdlet.
I scheduled this script in the Azure Automation Account and for the first 2 days, the script worked very well!
Today, I analyzed the logs and saw that the script had the error below:
Connect-AzAccount : Object reference not set to an instance of an object. At line:12 char:25 + $connectionResult = Connect-AzAccount ` + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Connect-AzAccount], NullReferenceException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand
Does anyone have any idea what may be causing this error?
Below the script:
Param(
[string]$resourceGroupName
)
$connection = Get-AutomationConnection -Name AzureRunAsConnection
while (!($connectionResult) -And ($logonAttempt -le 10)) {
$LogonAttempt++
# Logging in to Azure...
$connectionResult = Connect-AzAccount `
-ServicePrincipal `
-Tenant $connection.TenantID `
-ApplicationID $connection.ApplicationID `
-CertificateThumbprint $connection.CertificateThumbprint
Start-Sleep -Seconds 30
}
# Remove old snapshots
$snapshotnames = (Get-AzSnapshot -ResourceGroupName $resourceGroupName).name
foreach($snapname in $snapshotnames)
{
Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapname | ?{($_.TimeCreated) -lt ([datetime]::UtcNow.AddMinutes(-10080))} | Remove-AzSnapshot -Force
}
foreach ($VMs in Get-AzVM -ResourceGroupName $resourceGroupName) {
#Set local variables
$location = $VMs.Location
#$resourceGroupName = $vmInfo.ResourceGroupName
$timestamp = Get-Date -f MM-dd-yyyy_HH_mm_ss
#Snapshot name of OS data disk
$snapshotName = "bkp-" + $VMs.Name + "-" + $timestamp
#Create snapshot configuration
$snapshot = New-AzSnapshotConfig -SourceUri $VMs.StorageProfile.OsDisk.ManagedDisk.Id -Location $location -CreateOption copy
#Take snapshot
New-AzSnapshot -Snapshot $snapshot -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName
if ($VMs.StorageProfile.DataDisks.Count -ge 1) {
#Condition with more than one data disks
for ($i = 0; $i -le $VMs.StorageProfile.DataDisks.Count - 1; $i++) {
#Snapshot name of OS data disk
$snapshotName = "bkp-" + $VMs.StorageProfile.DataDisks[$i].Name + "-" + $timestamp
#Create snapshot configuration
$snapshot = New-AzSnapshotConfig -SourceUri $VMs.StorageProfile.DataDisks[$i].ManagedDisk.Id -Location $location -CreateOption copy
#Take snapshot
New-AzSnapshot -Snapshot $snapshot -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName
}
}
else {
Write-Host $VMs.Name + " doesn't have any additional data disk."
}
}
You're having a nullreference exception, looking at the location inside the error message (At line:12 char:25). it shows that connection is null.
You need to declare connection first on the same page before you're able to use the variables inside.
See also: What is a NullReferenceException, and how do I fix it?
EDIT: At second notice, I think that you're already declaring the $connection at
$connection = Get-AutomationConnection -Name AzureRunAsConnection
but it looks like that's returning null. In that case, you'll need to figure out why that's returning null, because it should return a filled $connection if connected.
For a safe check to avoid errors: you should check first if the $connection is not null before entering the while, (for example $connection != null could work for Javascript). That way you won't get errors, but keep in mind that you won't get a result through this.

How to Create App Service with a subType of api

In powershell and using the new-azwebapp to create a website in our app service plan. But it only ever seems to create a type of app
I'm trying to create it with the type of api like you can when you select from the Azure Portal UI, but I can't see a setting in the configuration to do this.
After creating I set the AppSettings and then try to update the Type, but it doesn't seem to matter.
Hoping that I'm missing something simple.
$siteName = "namegoeshere"
$sitePlan = "myplanname"
$siteLocation = "Australia East"
$siteResourceGroup = "resourcegroupname"
$createdSite = new-azWebApp -Name $siteName -ResourceGroupName $siteResourceGroup -AppServicePlan $sitePlan -Location $siteLocation
$newAppSettings = #{ "name"="value"}
Set-AzWebApp -Name $siteName -ResourceGroupName $siteResourceGroup -AppSettings $newAppSettings -AppServicePlan Grower #-PhpVersion 0
$p = #{ 'type' = "api" }
$r = Set-AzResource -Name $siteName -ResourceGroupName $siteResourceGroup -ResourceType Microsoft.Web/sites -PropertyObject $p -ApiVersion 2016-03-01 -Kind "app"
echo $r.Properties
The type api is not set with type, in the New-AzureRmResource parameters there is a parameter to set it, it's [-Kind <String>]. You could check it here.
Here is an example.
# CREATE "just-an-api" API App
$ResourceLocation = "West US"
$ResourceName = "just-an-api"
$ResourceGroupName = "demo"
$PropertiesObject = #{
# serverFarmId points to the App Service Plan resource id
serverFarmId = "/subscriptions/SUBSCRIPTION-GUID/resourceGroups/demo/providers/Microsoft.Web/serverfarms/plan1"
}
New-AzureRmResource -Location $ResourceLocation `
-PropertyObject $PropertiesObject `
-ResourceGroupName $ResourceGroupName `
-ResourceType Microsoft.Web/sites `
-ResourceName "just-an-api/$ResourceName" `
-Kind 'api' `
-ApiVersion 2016-08-01 -Force

Resources