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

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.

Related

How to set the ComputeModel property to Serverless on an Azure SQL Database using PowerShell?

I'm restoring an Azure SQL Database (Serverless) from a deleted database backup using Get-AzSqlDeletedDatabaseBackup and Restore-AzSqlDatabase PowerShell commandlets. The restore works, but the tags and ComputeModel are not restored with the database.
I've tried using Set-AzSqlDatabase:
Set-AzSqlDatabase -ResourceGroupName $resourcegroupname -DatabaseName $databasename -ServerName $servername -ComputeModel "Serverless" -AutoPauseDelayInMinutes 45
Update: I tried the following code and the Kind is set prior to using the Set-AzResource cmdlet, but it doesn't stick
$resource = Get-AzResource -ResourceGroupName $resourcegroupname -ResourceType "Microsoft.Sql/servers/databases" -Name "$servername/$databasename"
Write-Host "Setting ComputeModel to Serverless..."
$resource.Kind = "v12.0,user,vcore,serverless"
$resource
# resource.Kind is successfully set on the $resource object
Write-Host "Set-AzResource..."
$resource | Set-AzResource -Force
Anyone have any ideas?
Thank you.
Cheers,
Andy
The Get-AzSqlDeletedDatabaseBackup and Restore-AzSqlDatabase PowerShell cmdlets are doesn't contain a property to get the ComputeModel.
While Restore and Delete Backup database we don't require the ComputeModel properties. while setting database we need to require the ComputeModel.
If you want to get the compute model for the Azure SQL Database you can use, Get-AzResource command to fetch the specific information.
Thanks #joy wang SO Solution we can get the serverless Azure SQL Database.
Thanks to #holger and #Delliganesh Sevanesan for the help, I was able implement a solution that restores the most recent deleted database (SQL Database), adds some resource tags, and sets the ComputeModel to serverless.
Here's the code:
<#
Purpose: Restore the most recently deleted Azure Sql Database
Dependencies:
Az.Sql PowerShell module
#>
# Set variables first
$resourcegroupname = 'myresourcegroup'
$servername = 'myservername'
$databasename = 'mydatabasename'
[hashtable]$tags = #{
application = "testing"
}
try {
$deleteddatabases = Get-AzSqlDeletedDatabaseBackup -ResourceGroupName $resourcegroupname -ServerName $servername -DatabaseName $databasename
} catch {
Write-Error "Error getting database backups [Get-AzSqlDeletedDatabaseBackup]: " + $_.Exception.Message
exit(1)
}
# Get most recent backup in case there are multiple copies
# Assumes index and order in foreach is the same - proven in test
Write-Host "Database backups:"
$index = 0
$MostRecentBackupIndex = 0
$MostRecentBackupDate = (Get-date).AddDays(-2) # initialize variable with date from two days ago
foreach ($db in $deleteddatabases) {
if ($db.CreationDate -ge $MostRecentBackupDate) {
$MostRecentBackupIndex = $index
$MostRecentBackupDate = $db.CreationDate
Write-Host "Most Recent Database: $($db.DatabaseName) : Created: $($db.CreationDate) : DeleteDate: $($db.DeletionDate)"
}
$index++
}
$deleteddatabase = $deleteddatabases[$MostRecentBackupIndex]
Write-Host "----------------------------------------------------------------------------------"
Write-Host "Restoring: $($deleteddatabase.DatabaseName) from: $($deleteddatabase.CreationDate) backup"
Write-Host "----------------------------------------------------------------------------------"
Write-Host "Deleted database info ResourceId: "
Write-Host $deleteddatabase.ResourceId
try {
Restore-AzSqlDatabase -FromDeletedDatabaseBackup `
-DeletionDate $deleteddatabase.DeletionDate `
-ResourceGroupName $resourcegroupname `
-ServerName $servername `
-TargetDatabaseName $databasename `
-ResourceId $deleteddatabase.ResourceID `
-Edition $deleteddatabase.Edition `
-Vcore 2 `
-ComputeGeneration "Gen5"
} catch {
Write-Error "Error restoring database [Restore-AzSqlDatabase]: " + $_.Exception.Message
exit(1)
}
# Wait a few minutes to allow restore to complete before applying the tags
Start-Sleep -Seconds 180
Write-Host "Applying tags to database..."
try {
$resource = Get-AzResource -ResourceGroupName $resourcegroupname -ResourceType "Microsoft.Sql/servers/databases" -Name "$servername/$databasename"
New-AzTag -ResourceId $resource.Id -Tag $tags
} catch {
Write-Error "Error adding tags to database [Get-AzResource, New-AzTag]: " + $_.Exception.Message
exit(1)
}
Write-Host "Setting ComputeModel to Serverless..."
try {
# Important - must include -AutoPauseDelayInMinutes 60, -MinVcore, and MaxVcore parameters (thanks holger)
Set-AzSqlDatabase -ResourceGroupName $resourcegroupname -DatabaseName $databasename -ServerName $servername -ComputeModel Serverless -AutoPauseDelayInMinutes 60 -MinVcore 1 -MaxVcore 2
} catch {
Write-Error "Error setting serverless mode [Set-AzSqlDatabase]: " + $_.Exception.Message
exit(1)
}
Write-Host "Database restore complete."

Azure PowerShell Runbook Get Set Az-Disk dynamically

The team is trying to automate a snapshot restore, which was achieved successfully. However I am not able to figure out how to dynamically get the previous disk within the resource group; As well as set the next disk with a new name.
In the code below "AZR-001_OsDisk_7" has to set dynamically to "AZR-001_OsDisk_8" the next time it runs:
$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName "ETD-RFT" -DiskName "AZR-001_OsDisk_7"
$disk1 = Get-AzDisk -ResourceGroupName "ETD-RFT" -Name "AZR-001_OsDisk_7"
Not a final solution, but I have a quick idea. You may use Get-AzDisk -ResourceGroupName 'ResourceGroupName ' to get all the disks. And then you can get the disk name.
As you named the disk with appropriate rule, you may split the name string by _, then you will get a string array which contains all the parts. In this way, you will be able to get the version.
A sample:
$disks = Get-AzDisk -ResourceGroupName JackVS2019
foreach($disk in $disks){
$arr = $disk.Name.Split('_')
Write-Host $arr[2]
}
The output:
1
Then you can generate the new disk name.
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$account = Connect-AzAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
Select-AzSubscription -SubscriptionName 101-Prototyping
$vm = Get-AzVM -ResourceGroupName "ETD-RFT" -Name "AZR-101-007-001"
$snapshot = Get-AzSnapshot -ResourceGroupName "ETD-RFT" -SnapshotName "SNAPSHOT-DEC-2019"
$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disks = Get-AzDisk -ResourceGroupName "ETD-RFT"
$attached = $disks | Where-Object ManagedBy -ne $null
foreach($disk in $attached)
{
$arr = $disk.Name.Split('_')
$arr[2]
}
$a = $arr[2] -as [int]
$a=$a+1
Write-Host $a
$newname = $arr[0] + "_" + $arr[1] + "_" + $a
$disknew = New-AzDisk -Disk $diskConfig -ResourceGroupName "ETD-RFT" -DiskName $newname
$disk1 = Get-AzDisk -ResourceGroupName "ETD-RFT" -Name $newname
Set-AzVMOSDisk -VM $vm -ManagedDiskId $disk1.Id -Name $disk1.Name
Update-AzVM -ResourceGroupName "ETD-RFT" -VM $vm

Scaling CosmosDB Container using Powershell

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

PowerShell script to move data disk from one azure vm to another vm

I am developing a script to copy data disks from one Azure VM to another Azure VM. Below is the task:
Create SNAPSHOTS of existing data disks from source VM
Create new DATADISKS from the SNAPSHOTS created from step 1
Attach the new DATADISKS to the destination VM
I have written the complete code. However Step 3 is throwing an error.
## Create Snapshot from a Managed Disk ##
$resourceGroupName = 'manju_copy_disk'
$location = 'east us 2'
$source_vm_name = 'server1'
$destination_vm_name = 'server3'
$source_vm_object = get-azurermvm -ResourceGroupName $resourceGroupName -Name $source_vm_name
$data_disk_list = Get-AzureRmDisk | where {$_.ManagedBy -match $source_vm_name -and $_.OsType -eq $null}
$snapshot_list = New-Object System.Collections.ArrayList($null)
foreach($data_disk_list_iterator in $data_disk_list){
$snapshotName = $destination_vm_name + "_Snapshot_" + $data_disk_list_iterator.Name
$snapshot_config = New-AzureRmSnapshotConfig -SourceUri $data_disk_list_iterator.id -Location $location -CreateOption copy
$snapshot_object = New-AzureRmSnapshot -Snapshot $snapshot_config -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName
$snapshot_list.Add($snapshot_object.Id)
}
## Create Managed disk from snap shot created above ##
$storageType = 'StandardLRS'
$count=0
$destination_datadisk_list = New-Object System.Collections.ArrayList($null)
#$destination_datadisk_Name_list = New-Object System.Collections.ArrayList($null)
foreach($snapshot_list_iterator in $snapshot_list){
$disk_name = $destination_vm_name + "_datadisk_" + $count
$count += 1
$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot_list_iterator
$datadisk_object = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $disk_name
$destination_datadisk_ID_list.Add($datadisk_object.Id)
}
## Attach Managed disk to destination vm
$destination_vm_object = Get-AzureRmVM -Name $destination_vm_name -ResourceGroupName $resourceGroupName
$lun_count = 1
foreach($destination_datadisk_list_iterator in $destination_datadisk_list){
$destination_datadisk_name = $destination_vm_name + "_datadisk_"+$lun_count
$destination_vm_object = Add-AzureRmVMDataDisk -VM $destination_vm_object -Name $destination_datadisk_name -CreateOption Attach -ManagedDiskId $destination_datadisk_list_iterator -Lun $lun_count
$lun_count += 1
}
Update-AzureRmVM -VM $destination_vm_object -ResourceGroupName $resourceGroupName ## --> LINE CODE NOT WORKING
Below is the error:
Update-AzureRmVM : Changing property 'dataDisk.name' is not allowed.
ErrorCode: PropertyChangeNotAllowed
ErrorMessage: Changing property 'dataDisk.name' is not allowed.
StatusCode: 409
ReasonPhrase: Conflict
OperationID : e8a0a8de-0cdd-4ba0-90bc-883d37e374af
At line:1 char:1
+ Update-AzureRmVM -VM $destination_vm_object -ResourceGroupName $resou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Update-AzureRmVM], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.UpdateAzureVMCommand
When you attach the data disks which you create from the snapshot, you can not change the name again. Take a look at this.
So I suggest you can create the data disk from the snapshot and attach them to the destination VM in the same foreach loop with the same names.

Create HDCluster using powershell

I am trying to create cluster using powershell. Here is script I am executing:
$containerName = "hdfiles"
$location = "Southeast Asia"
$clusterNodes = 2
$userName = "HDUser"
#Generate random password
$rand = New-Object System.Random
$pass = ""
$pass = $pass + [char]$rand.next(97,121) #lower case
$pass = $pass + [char]$rand.next(48,57) #number
$pass = $pass + [char]$rand.next(65,90) #upper case
$pass = $pass + [char]$rand.next(58,62) #special character
1..6 | ForEach { $pass = $pass + [char]$rand.next(97,121) } #6 lower-case characters
$password = ConvertTo-SecureString $pass -AsPlainText -Force
# generate unique random cluster and storage account names
do
{
$clusterName = "hd"
1..6 | ForEach { $clusterName = $clusterName + [char]$rand.next(48,57) }
$storageAccountName = $clusterName + "store"
}
while ((Test-AzureName -Name $storageAccountName -Storage) -and (Test-AzureName -Name $clusterName -Service))
# Create a storage account
Write-Host "Creating storage account..."
New-AzureStorageAccount -StorageAccountName $storageAccountName -Location $location
# Create a Blob storage container
Write-Host "Creating container..."
$storageAccountKey = Get-AzureStorageKey $storageAccountName | %{ $_.Primary }
$destContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
New-AzureStorageContainer -Name $containerName -Context $destContext
# Create a cluster
Write-Host "Creating HDInsight cluster..."
$credential = New-Object System.Management.Automation.PSCredential ($userName, $password)
New-AzureHDInsightCluster -Name $clusterName -Location $location -DefaultStorageAccountName "$storageAccountName.blob.core.windows.net" -DefaultStorageAccountKey $storageAccountKey -DefaultStorageContainerName $containerName -ClusterSizeInNodes $clusterNodes -Credential $credential -Version 3.2
But on last line I am getting exception:
New-AzureHDInsightCluster : Validating connection to 'hd662173store.blob.core.windows.net' failed. Inner exception:Could not load file or assembly 'Microsoft.WindowsAzure.Storage, Version=3.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
At D:\ProgrammingWorkspace\Edx\Processing BigData with HDInsight\HDILabs\Lab02A\Provision HDInsight.ps1:38 char:1 + New-AzureHDInsightCluster -Name $clusterName -Location $location -Def ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureHDInsightCluster], ConfigurationErrorsException
+ FullyQualifiedErrorId : System.Configuration.ConfigurationErrorsException,Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.PSCmdlets.NewAzureHDInsightClusterCmdlet
I am using Azure Powershell release 0.9.7 and Azure SDK 2.7
Looking back on this, the issue you encountered is likely now fixed with the latest version of Azure PowerShell.
To install the latest version of Azure PowerShell, see here:
Installing Azure PowerShell
For samples of how to use HDInsight PowerShell cmdlets (e.g., New-AzureRmHDInsightCluster), see here: Using HDInsight Azure PowerShell cmdlets
I hope this helps!

Resources