Set-AzDiagnosticSetting For Storage account - azure

I'm new to powershell and DevOps pipeline. Here I tried to set DiagnosticSetting for storage account, but I got some issue as I mentioned below. My Powershell script commands are.
$ResourceId = "/subscriptions/'xxxxxxxxxxxxxxx'/resourceGroups/'xxxxxxxxxxxx'/providers/Microsoft.Storage/storageAccounts/'xxxxxxxxxxxxxxxxx"
$WorkspaceId = "/subscriptions/'xxxxxxxxxxxxxxx'/resourcegroups/'xxxxxxxxxxxxxxx'/providers/microsoft.operationalinsights/workspaces/'xxxxxxxxxxxxxxx'"
$DiagnosticSettingName = "storagediag"
$metric = New-AzDiagnosticDetailSetting -Metric -RetentionEnabled -Category AllMetrics -Enabled
$setting = New-AzDiagnosticSetting -Name $DiagnosticSettingName -ResourceId $ResourceId -WorkspaceId $WorkspaceId -Setting $metric
Set-AzDiagnosticSetting -InputObject $setting -Debug
$metric = New-AzDiagnosticDetailSetting -Metric -RetentionEnabled -Category AllMetrics -Enabled
$readlog = New-AzDiagnosticDetailSetting -Log -RetentionEnabled -Category StorageRead -Enabled
$writelog = New-AzDiagnosticDetailSetting -Log -RetentionEnabled -Category StorageWrite -Enabled
$deletelog = New-AzDiagnosticDetailSetting -Log -RetentionEnabled -Category StorageDelete -Enabled
$Ids = #($ResourceId + "/blobServices/default")
$Ids | ForEach-Object {
$setting = New-AzDiagnosticSetting -Name $DiagnosticSettingName -ResourceId $_ -WorkspaceId $WorkspaceId -Setting $metric, $readlog, $writelog, $deletelog
Set-AzDiagnosticSetting -InputObject $setting
}
Exception type: PSInvalidOperationException, Message: System.Management.Automation.PSInvalidOperationException: PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.

Related

Powershell script to automate azure linux VM backup and restore from azure devops pipeline

I tried multiple powershell script to do azure VM backup and restore but none of them are seems working or not so effective. Could any one please share the powershell script to create backup of VM and restore in azure via azure devops pipeline.
PowerShell Script:
<#
.SYNOPSIS
Written By John Lewis
email: jonos#live.com
Ver 1.1
v 1.2 updates - minor fixes
v 1.1 updates - Added Pre-Checks
v 1.0 updates - RTM
Deploys Azure Backup Vault and configures Azure VMs to leverage vault. Provides automated restore to new VM.
.PARAMETER Action
.PARAMETER backupvmname
.PARAMETER backupvmrg
.PARAMETER rg
.PARAMETER policyname
.PARAMETER containertype
.PARAMETER Location
.PARAMETER wrkloadtype
.PARAMETER vaultname
.PARAMETER createvmname
.PARAMETER createvmrg
.PARAMETER vaultrg
.PARAMETER vnet
.PARAMETER vnetrg
.PARAMETER createvmsubnet
.EXAMPLE
.\AZRM-VMBackup.ps1 -csvimport -csvfile C:\temp\backupservers.csv
.EXAMPLE
.\AZRM-VMBackup.ps1 -action createpolicy -vaultname myvault -vaultrg myres -policyname mypolicy
.EXAMPLE
.\AZRM-VMBackup.ps1 -action createvault -vaultname myvault -vaultrg myres
.EXAMPLE
.\AZRM-VMBackup.ps1 -action addvmcreatevault -backupvmname myvm -backupvmrg myres -vaultname myvault -vaultrg myres
.EXAMPLE
.\AZRM-VMBackup.ps1 -action restorevm -createvmname myvm -createvmrg myres -vaultrg backuprg -vaultname myvaultname -vnet vnet -vnetrg myvnetrg
#>
[CmdletBinding(DefaultParameterSetName = 'default')]
Param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[ValidateSet("createpolicy","createvault","addvmcreatevault","addvmtovault","restorevm","executebackup","getstatus")]
[string]
$Action = 'createvault',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$vaultrg = "vault",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$storeagerg = $createvmrg,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$location = "East US",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$vaultname = "vault",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$backupvmname = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$backupvmrg = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$backupvmlocation = $location,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$createvmname = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$createvmrg = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$createvmlocation = $location,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true,Position=4)]
[ValidateNotNullorEmpty()]
[Alias("createvmvnet")]
[string]
$VNetName = 'vnet',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]
$vnetrg = $createvmrg,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$policyname = 'policy',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$containertype = 'AzureVM',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$wrkloadtype = 'AzureVM',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$provider = "Microsoft.RecoveryServices",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$Profile = "profile",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$csvfile = -join $workfolder + "\azrm-vmbackup.csv",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[Alias("csv")]
[switch]
$csvimport,
[Parameter(Mandatory=$False)]
[string]
$GenerateName = -join ((65..90) + (97..122) | Get-Random -Count 6 | % {[char]$_}) + "rmp",
[Parameter(Mandatory=$False)]
[string]
$StorageName = $createvmname + 'str',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateSet("Standard_LRS","Standard_GRS")]
[string]
$StorageType = 'Standard_GRS',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateNotNullorEmpty()]
[Alias("int1")]
[string]
$InterfaceName1 = $createvmname + '_nic1',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[Alias("int2")]
[string]
$InterfaceName2 = $createvmname + "_nic2",
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$SubscriptionID = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$TenantID = '',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateSet("InProgress","Completed")]
[string]
$status = 'InProgress',
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[ValidateRange(0,8)]
[ValidateNotNullorEmpty()]
[Alias("createvmsubnet")]
[Int]
$Subnet1 = 4
)
$workfolder = Split-Path $script:MyInvocation.MyCommand.Path
$ProfileFile = $workfolder+'\'+$profile+'.json'
$restorejson = $workfolder+'\'+ 'config.json'
Function validate-profile {
$comparedate = (Get-Date).AddDays(-14)
$fileexist = Test-Path $ProfileFile -NewerThan $comparedate
if($fileexist)
{
$az = Import-AzureRmContext -Path $ProfileFile
$subid = $az.Context.Subscription.Id
Set-AzureRmContext -SubscriptionId $subid | Out-Null
Write-Host "Using $ProfileFile"
}
else
{
Write-Host "Please enter your credentials"
Add-AzureRmAccount
Save-AzureRmContext -Path $ProfileFile -Force
Write-Host "Saved Profile to $ProfileFile"
exit
}
}
Function csv-run {
param(
[string] $csvin = $csvfile
)
$GetPath = test-path -Path $csvin
if(!$csvin)
{exit}
else {
Write-Host $GetPath "File Exists"
import-csv -Path $csvin -Delimiter ',' -ErrorAction SilentlyContinue -InformationAction SilentlyContinue | ForEach-Object{.\AZRM-VMBackup.ps1 -VMName $_.VMName -rg $_.rg -Action $_.Action -vmrg $_.vmrg -servaultname $_.servaultname }
}
}
Function Check-StorageName
{
param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]$StorageName = $StorageName
)
$checkname = Get-AzureRmStorageAccountNameAvailability -Name $StorageName | Select-Object -ExpandProperty NameAvailable
if($checkname -ne 'False') {
Write-Host "Storage Account Found..."
$script:StorageNameVerified = $StorageName.ToLower()
}
else
{
$script:StorageNameVerified = $StorageName.ToLower()
Create-Storage
}
}
#region Create Storage
Function Create-Storage {
param(
[string]$StorageName = $script:StorageNameVerified,
[string]$StorageType = $StorageType,
[string]$containerName = 'vhds',
[string]$Location = $Location,
[string]$storeagerg = $storeagerg
)
Write-Host "Starting Storage Creation..."
$script:StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $storeagerg -Name $StorageName.ToLower() -Type $StorageType -Location $Location -ErrorAction Stop -WarningAction SilentlyContinue
Write-Host "Completed Storage Creation" -ForegroundColor White
}
Function Check-Vnet {
$vnetexists = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if(!$vnetexists)
{
Write-Host "$VNetName does not exists!"
break
}
else
{ $existvnet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg
$addspace = $existvnet.AddressSpace | Select-Object -ExpandProperty AddressPrefixes
$namespace = $existvnet.Name
$existvnet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg
$addsubnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $existvnet
$sub = $addsubnet.AddressPrefix
$subname = $addsubnet.Name
$nsg = $addsubnet.NetworkSecurityGroup
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $existvnet | ft Name,AddressPrefix -AutoSize -Wrap -HideTableHeaders
Write-Host " "
Write-Host "VNET CONFIGURATION - Existing VNET" -ForegroundColor Cyan
Write-Host " "
Write-Host "Active VNET: $VnetName in resource group $vnetrg"
Write-Host "Address Space: $addspace "
Write-Host "Subnet Ranges: $sub "
Write-Host "Subnet Names: $subname "
}
}
Function Check-VaultExists {
$vaultexists = Get-AzureRmRecoveryServicesVault -Name $vaultname -ResourceGroupName $vaultrg -ErrorAction Stop -WarningAction SilentlyContinue -InformationAction SilentlyContinue
if(!$vaultexists)
{
Write-Host "Specified vault $vaultname does not exist!" -ForegroundColor Red
break
}
}
function Check-VaultNullValues {
if(!$vaultrg) {
Write-Host "Please select -vaultrg" -ForegroundColor Red
exit
}
elseif(!$vaultname) {
Write-Host "Please Enter -vaultname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-BackupVMNullValues {
if(!$backupvmrg) {
Write-Host "Please select -backupvmrg" -ForegroundColor Red
exit
}
elseif(!$backupvmname) {
Write-Host "Please Enter -backupvmname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-CreateVMNullValues {
if(!$createvmrg) {
Write-Host "Please select -createvmrg" -ForegroundColor Red
exit
}
elseif(!$createvmname) {
Write-Host "Please Enter -createvmname" -ForegroundColor Red
exit
}
if(!$backupvmrg) {
Write-Host "Please select -backupvmrg" -ForegroundColor Red
exit
}
elseif(!$backupvmname) {
Write-Host "Please Enter -backupvmname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-StorageNullValues {
if(!$storeagerg) {
Write-Host "Please select -storage" -ForegroundColor Red
exit
}
elseif(!$StorageName) {
Write-Host "Please Enter -createvmname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Check-VNETNullValues {
if(!$vnetrg) {
Write-Host "Please select -vnetrg" -ForegroundColor Red
exit
}
elseif(!$VNetName) {
Write-Host "Please Enter -vnetname" -ForegroundColor Red
exit
}
elseif(!$Location) {
Write-Host "Please Enter -Location" -ForegroundColor Red
exit
}
}
function Reg-Provider {
param($provider = $provider)
Register-AzureRmResourceProvider -ProviderNamespace $provider
}
Function Provision-RG
{
Param(
[string]$rg = $vaultrg
)
New-AzureRmResourceGroup -Name $rg -Location $Location –Confirm:$false -WarningAction SilentlyContinue -Force | Out-Null
}
Function Check-VMExists {
param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$Location = $Location,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$rg = $backupvmrg,
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$VMName = $backupvmname
)
$extvm = Get-AzureRmVm -Name $VMName -ResourceGroupName $rg -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if(!$extvm)
{
Write-Host "$VMName does not exist, please verify the VM exists" -ForegroundColor Yellow
Break
}
else {Write-Host "Restoration VM found" -ForegroundColor Green}
} #
#endregion
Function Configure-Backup {
param(
[Parameter(Mandatory=$False,ValueFromPipelinebyPropertyName=$true)]
[string]
$action = $action
)
switch ($action)
{
"createvault" {
Check-VaultNullValues
Write-Host "Creating new Backup Vault $vaultname"
Write-Config
Provision-RG
Create-Vault
Get-CurrentPolicies
Create-Policy
Write-Completion
}
"createpolicy" {
Write-Host "Creating new Policy"
Write-Config
Check-VaultExists
Get-CurrentPolicies
Create-Policy
Write-Completion
}
"addvmtovault" {
Check-VMExists
Check-VaultExists
Write-Host "Adding VM $backupvmname to $vaultname"
Write-Config
Get-CurrentPolicies
AddVM-Vault
Write-Completion
}
"executebackup" {
Write-Host "Executing backup of $backupvmname to $vaultname"
Check-VMExists
Write-Config
Check-BackupVMNullValues
Get-CurrentPolicies
TriggerBackup-Vault
Write-Completion
}
"addvmcreatevault" {
Check-VaultNullValues
Write-Host "Creating new Backup Vault $vaultname"
Write-Config
Provision-RG
Create-Vault
Create-Policy
Check-BackupVMNullValues
Check-VaultExists
Check-VMExists
Write-Host "Adding VM $backupvmname to $vaultname"
Get-CurrentPolicies
AddVM-Vault
Write-Completion
}
"restorevm" {
Check-CreateVMNullValues
Check-VNETNullValues
Check-Vnet
Check-StorageNullValues
Check-VMExists
Check-StorageName
Write-Host "Restoring VM Backup from $backupvmname to $createvmname"
Write-Config
Restore-VMVHD
Create-VM
Write-Completion
}
"getstatus" {
Write-Host "Obtaining current job information"
Get-JobProgress
}
default{"An unsupported backup command was used"}
}
exit
}
Function Get-JobProgress {
param(
$status = $status
)
Get-AzureRmRecoveryservicesBackupJob –Status $status
}
function Create-Vault {
New-AzureRmResourceGroup -Name $vaultrg -Location $Location –Confirm:$false -WarningAction SilentlyContinue -Force | Out-Null
New-AzureRmRecoveryServicesVault -Name $vaultname -ResourceGroupName $vaultrg -Location $location
$vault1 = Get-AzureRmRecoveryServicesVault –Name $vaultname
Set-AzureRmRecoveryServicesBackupProperties -Vault $vault1 -BackupStorageRedundancy GeoRedundant
}
function Get-Context {
param($vaultname = $vaultname)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
}
function Get-CurrentPolicies {
param(
$vaultname = $vaultname,
$wrkloadtype = $wrkloadtype
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$schPol = Get-AzureRmRecoveryServicesBackupSchedulePolicyObject -WorkloadType $wrkloadtype
$retPol = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType $wrkloadtype
}
function Create-Policy {
param(
$vaultname = $vaultname,
$policyname = $policyname,
$wrkloadtype = $wrkloadtype
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$schPol = Get-AzureRmRecoveryServicesBackupSchedulePolicyObject -WorkloadType $wrkloadtype
$retPol = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType $wrkloadtype
New-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname -WorkloadType $wrkloadtype -RetentionPolicy $retPol -SchedulePolicy $schPol -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
$script:pol = Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname
}
function Modify-Policy {
param(
$vaultname = $vaultname,
$policyname = $policyname,
$wrkloadtype = $wrkloadtype
)
$retPol = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType "AzureVM"
$retPol.DailySchedule.DurationCountInDays = 365
$pol= Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname
Set-AzureRmRecoveryServicesBackupProtectionPolicy -Policy $pol -RetentionPolicy $RetPol
}
function TriggerBackup-Vault {
param(
$vaultname = $vaultname,
$backupvmname = $backupvmname,
$containertype = "AzureVM"
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$namedContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType $containertype -Status "Registered" -FriendlyName $backupvmname
$item = Get-AzureRmRecoveryServicesBackupItem -Container $namedContainer -WorkloadType "AzureVM"
$job = Backup-AzureRmRecoveryServicesBackupItem -Item $item
}
function AddVM-Vault {
param(
$vaultname = $vaultname,
$backupvmname = $backupvmname,
$containertype = "AzureVM",
$vaultrg = $backupvmrg,
$policyname = $policyname
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$script:pol = Get-AzureRmRecoveryServicesBackupProtectionPolicy -Name $policyname
Enable-AzureRmRecoveryServicesBackupProtection -Policy $script:pol -Name $backupvmname -ResourceGroupName $vaultrg
$namedContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType $containertype -Status "Registered" -FriendlyName $backupvmname
$item = Get-AzureRmRecoveryServicesBackupItem -Container $namedContainer -WorkloadType "AzureVM"
$job = Backup-AzureRmRecoveryServicesBackupItem -Item $item
$job
}
Function Get-RestorePoint {
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime()
$rp[0]
}
Function Write-Config {
param(
)
Write-Host " "
$time = " Start Time " + (Get-Date -UFormat "%d-%m-%Y %H:%M:%S")
Write-Host BACKUP/RESTORE CONFIGURATION - $time -ForegroundColor Cyan
Write-Host " "
Write-Host "Operation: $Action " -ForegroundColor White
Write-Host " "
}
Function Write-Completion {
param(
)
Write-Host " "
$time = " End Time " + (Get-Date -UFormat "%d-%m-%Y %H:%M:%S")
Write-Host BACKUP/RESTORE CONFIGURATION - $time -ForegroundColor Cyan
Write-Host " "
Write-Host "Completed operation: $Action " -ForegroundColor White
Write-Host " "
}
function Restore-VMVHD {
param(
$vaultname = $vaultname,
$storeagerg = $storeagerg,
$StorageName = $script:StorageNameVerified,
$backupvmname = $backupvmname
)
Get-AzureRmRecoveryServicesVault -Name $vaultname | Set-AzureRmRecoveryServicesVaultContext
$namedContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType "AzureVM" –Status "Registered" -FriendlyName $backupvmname -WarningAction SilentlyContinue -InformationAction SilentlyContinue -ErrorAction Stop
$backupitem = Get-AzureRmRecoveryServicesBackupItem –Container $namedContainer –WorkloadType "AzureVM" -InformationAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction Stop
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime() -WarningAction SilentlyContinue -ErrorAction Stop -InformationAction SilentlyContinue
$rp[0]
Write-Host "Preparing Restore Job"
$restorejob = Restore-AzureRmRecoveryServicesBackupItem -RecoveryPoint $rp[0] -StorageAccountName $StorageName -StorageAccountResourceGroupName $storeagerg -WarningAction SilentlyContinue -InformationAction SilentlyContinue -ErrorAction Stop
Wait-AzureRmRecoveryServicesBackupJob -Job $restorejob -Timeout 43200 -WarningAction SilentlyContinue -InformationAction SilentlyContinue -ErrorAction Stop
Write-Host "Restore Job Running"
$script:restorejob = $restorejob
$restorejob = Get-AzureRmRecoveryServicesBackupJob -Job $script:restorejob -InformationAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction Stop
$JobDetails = Get-AzureRmRecoveryServicesBackupJobDetails -Job $restorejob -InformationAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction Stop
$properties = $JobDetails.properties
$properties
$storageAccountName = $properties["Target Storage Account Name"]
$containerName = $properties["Config Blob Container Name"]
$blobName = $properties["Config Blob Name"]
Write-Host $storageAccountName
Write-Host $blobName
Write-Host $containerName
Write-Host "Completed restore job"
Set-AzureRmCurrentStorageAccount -Name $storageaccountname -ResourceGroupName $storeagerg
$destination_path = $restorejson
Get-AzureStorageBlobContent -Container $containerName -Blob $blobName -Destination $destination_path -ErrorAction Stop -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Force -Confirm:$false
$obj = ((Get-Content -Path $destination_path -Raw -Encoding Unicode)).TrimEnd([char]0x00) | ConvertFrom-Json
Write-Host "Exported json configuration file to $destination_path"
$vm = New-AzureRmVMConfig -VMSize $obj.'properties.hardwareProfile'.vmSize -VMName $createvmname -ErrorAction Stop -WarningAction SilentlyContinue -InformationAction SilentlyContinue
Set-AzureRmVMOSDisk -VM $vm -Name "osdisk" -VhdUri $obj.'properties.StorageProfile'.osDisk.vhd.Uri -CreateOption "Attach" -WarningAction SilentlyContinue -ErrorAction Stop -InformationAction SilentlyContinue
$vm.StorageProfile.OsDisk.OsType = $obj.'properties.StorageProfile'.OsDisk.OsType
foreach($dd in $obj.'properties.StorageProfile'.DataDisks)
{
$vm = Add-AzureRmVMDataDisk -VM $vm -Name "datadisk1" -VhdUri $dd.vhd.Uri -DiskSizeInGB 128 -Lun $dd.Lun -CreateOption "Attach"
}
Write-Host "Completed data disk configuration"
$pip = New-AzureRmPublicIpAddress -Name $InterfaceName1 -ResourceGroupName $createvmrg -Location $createvmlocation -AllocationMethod "Dynamic" –Confirm:$false -WarningAction SilentlyContinue -ErrorAction Stop -Force -InformationAction SilentlyContinue
Write-Host "Completed public ip creation"
$script:VNet = Get-AzureRMVirtualNetwork -Name $VNetName -ResourceGroupName $vnetrg | Set-AzureRmVirtualNetwork
$script:Interface1 = New-AzureRmNetworkInterface -Name $InterfaceName1 -ResourceGroupName $createvmrg -Location $createvmlocation -SubnetId $VNet.Subnets[$Subnet1].Id -PublicIpAddressId $pip.Id –Confirm:$false -WarningAction SilentlyContinue -ErrorAction Stop -EnableIPForwarding -Force
$script:VirtualMachine = Add-AzureRmVMNetworkInterface -VM $vm -Id $script:Interface1.Id -Primary -WarningAction SilentlyContinue -ErrorAction Stop -InformationAction SilentlyContinue
Write-Host "Completed vm prep"
}
Function Login-AddAzureRmProfile
{
Add-AzureRmAccount -WarningAction SilentlyContinue
}
Function Create-VM
{
Write-Host "Creating VM"
New-AzureRmVM -ResourceGroupName $createvmrg -Location $createvmlocation -VM $script:VirtualMachine
}
try {
Get-AzureRmResourceGroup -Location $Location -ErrorAction Stop | Out-Null
}
catch {
Write-Host -foregroundcolor Yellow `
"User has not authenticated, use Add-AzureRmAccount or $($_.Exception.Message)"; `
Login-AddAzureRmProfile
}
Reg-Provider
if($csvimport) {
try {
csv-run
}
catch {
Write-Host -foregroundcolor Yellow `
"$($_.Exception.Message)"; `
break
}
}
try {
Configure-Backup
}
catch {
Write-Host -foregroundcolor Yellow `
"$($_.Exception.Message)"; `
break
}
Error Message:
The below error is coming while I run from Run books automation account. provided runtime parameters to run the above script and left the value for CSVIMPORT parameter.
System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'Path' because it is null.
at System.Management.Automation.ParameterBinderController.BindPositionalParametersInSet(UInt32 validParameterSets, Dictionary`2 nextPositionalParameters, CommandParameterInternal argument, ParameterBindingFlags flags, ParameterBindingException& bindingException)
at System.Management.Automation.ParameterBinderController.BindPositionalParameters(Collection`1 unboundArguments, UInt32 validParameterSets, UInt32 defaultParameterSet, ParameterBindingException& outgoingBindingException)
at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arguments)
at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)
at System.Management.Automation.CommandProcessor.BindCommandLineParameters()
at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)
at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Environments Context
------------ -------
{[AzureCloud, AzureCloud], [AzureChinaCloud, AzureChinaCloud], [AzureUSGovernment, AzureUSGovernment]} Microsoft.Azur...
A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Are you sure you want to register the provider 'Microsoft.RecoveryServices'
param (
[Parameter(Mandatory=$false)][String]$ResourceGroupName,
[Parameter(Mandatory=$false)][String]$ServicePrincipalName,
[Parameter(Mandatory=$false)][String]$ServicePrincipalPass,
[Parameter(Mandatory=$false)][String]$SubscriptionId,
[Parameter(Mandatory=$false)][String]$TenantId,
[Parameter(Mandatory=$false)][String]$VMname,
[Parameter(Mandatory=$false)][String]$vaultname
)
$targetVault = Get-AzRecoveryServicesVault -ResourceGroupName $ResourceGroupName -Name $vaultname
$targetVault.ID
Get-AzRecoveryServicesBackupProtectionPolicy -WorkloadType "AzureVM" -VaultId $targetVault.ID
Get-AzRecoveryServicesVault -Name $vaultname | Set-AzRecoveryServicesVaultContext
$policy = Get-AzRecoveryServicesBackupProtectionPolicy -Name $policyname
Enable-AzRecoveryServicesBackupProtection -ResourceGroupName $ResourceGroupName -Name $VMname -Policy $policy
$backupcontainer = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -FriendlyName $VMname
$item = Get-AzRecoveryServicesBackupItem -Container $backupcontainer -WorkloadType "AzureVM"
$backupjob=Backup-AzRecoveryServicesBackupItem -Item $item
echo $backupjob
#$Backupstatus=Get-AzRecoveryservicesBackupJob
#Wait-AzRecoveryServicesBackupJob -Job $joblist[0] -Timeout 43200 -VaultId $targetVault.ID
Get-AzRecoveryservicesBackupJob
$namedContainer = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered" -FriendlyName $VMname -VaultId $targetVault.ID
$backupitem = Get-AzRecoveryServicesBackupItem -Container $namedContainer -WorkloadType "AzureVM" -VaultId $targetVault.ID
echo "Please wait for backup to complete - Backup is in progress"
start-sleep -s 30
$joblist = Get-AzRecoveryservicesBackupJob –Status "InProgress" -VaultId $targetVault.ID
$joblist[0]
while (!$rp.ContainerName)
{
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime() -VaultId $targetVault.ID
start-sleep -s 30
$rp[0]
Write-Host -NoNewline "Waiting 30 seconds for" $VMName "VM to backup"
}
Get-AzRecoveryservicesBackupJob
echo "Backup is completed successfully"

Azure-DevOps Run powershell Getting Error while running

I have below command which is used for change vm size on azure:
Install-PackageProvider -name nuget -MinimumVersion 2.8.5.201 -Force
IF(!(Get-InstalledModule PSExcel -ErrorAction SilentlyContinue)){
Install-Module -Name PSExcel -ErrorAction SilentlyContinue -Force
Import-Module PSExcel -Force
}
IF(!(Get-InstalledModule ImportExcel -ErrorAction SilentlyContinue)){
Install-Module -Name ImportExcel -ErrorAction SilentlyContinue -Force
Import-Module ImportExcel -Force
}
IF(!(Get-InstalledModule az -ErrorAction SilentlyContinue)){
Install-Module -Name az -ErrorAction SilentlyContinue -Force
Import-Module az -Force
}
Import-Module PSExcel -Force
Import-Module ImportExcel -Force
$ExcelFile = "D:\test-data (3).xlsx"
$objExcel = New-Excel -Path $ExcelFile
$WorkBook = $objExcel | Get-Workbook
$NewExcelFile ="D:\test-data1.xlsx"
copy-item $ExcelFile -Destination $NewExcelFile
$sheetName = "Sheet2"
$newExcel = New-Object -ComObject Excel.Application
$newwb = $newExcel.Workbooks.Open($NewExcelFile)
$WorkSheet = $newwb.sheets.item($sheetName)
$WorksheetRange = $workSheet.UsedRange
$RowCount = $WorksheetRange.Rows.Count
$sheet=$newwb.Worksheets.Item(1)
$q=$RowCount
write-host 260
$q1="R"+$q
$r1 = $sheet.Range("R2:$q1")
$r1.cut()
$i1="I"+$q
$r2 = $sheet.Range("I2:$i1")
$sheet.Paste($r2)
$newwb.Close($true)
$newExcel.Quit()
#Import-Module az -Force
#Connect-AzAccount
Write-Host 273
#Loop through all items in the excel
ForEach($Worksheet in #($Workbook.Worksheets))
{
$totalNoOfRecords = $Worksheet.Dimension.Rows
$totalNoOfItems = $totalNoOfRecords - 1
# Declaring starting positions first row and column names
$rowNo, $Instance_Type_Current = 1, 9
$rowNo, $Instance_Type_Recommended = 1, 18
$rowNo, $Resource_Group= 1,42
$rowNo, $Resource_Name = 1,2
$rowNo, $osname = 1,8
if ($totalNoOfRecords -gt 1)
{
#Loop to get values from excel file
for ($i = 1; $i -le $totalNoOfRecords - 1; $i++)
{
$newvmsize = $WorkSheet.Cells.Item($rowNo + $i, $Instance_Type_Recommended).text
$vmName = $WorkSheet.Cells.Item($rowNo + $i, $Resource_Name).text
$resourceGroup = $WorkSheet.Cells.Item($rowNo + $i, $Resource_Group).text
$currentvmsize = $WorkSheet.Cells.Item($rowNo + $i, $Instance_Type_Current).text
$Checkwindowsos = $WorkSheet.Cells.Item($rowNo + $i, $osname).text
$currentsizefromazure = (Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName -ErrorAction SilentlyContinue).HardwareProfile.VmSize
#If only windows OS then VM size will update on azure
IF($Checkwindowsos -match 'windows'){
IF($currentvmsize -eq $currentsizefromazure) {
Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName -ErrorAction SilentlyContinue
$vm.HardwareProfile.VmSize = $newvmsize
Write-Host 304
$vmstatus = $null
while($vmstatus -notmatch 'deallocated'){
$vmstatus=((get-azvm -Name $VmName -ResourceGroupName $resourceGroup -Status).Statuses[1]).code
}
#Start VM Snapshot(Backup)
$vm1 = get-azvm -Name $VmName -ResourceGroupName $resourceGroup
write-host $VmName
$snapshotdisk = $vm1.StorageProfile
$vmlocation =$vm1.location
#backup OS DISK
$OSDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $snapshotdisk.OsDisk.ManagedDisk.id -CreateOption Copy -Location $vmlocation -OsType Windows
$snapshotNameOS = "$($snapshotdisk.OsDisk.Name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $resourceGroup -SnapshotName $snapshotNameOS -Snapshot $OSDiskSnapshotConfig
Write-Output "VM-Name $($vm1.name) OS Disk Snapshot End & Snapshot name is $snapshotNameOS"
Write-Host 319
#backup Data DISK
$dataDisks = ($snapshotdisk.DataDisks).name
IF($dataDisks -ne $null){
foreach ($datadisk in $datadisks) {
$dataDisk = Get-AzDisk -ResourceGroupName $vm1.ResourceGroupName -DiskName $datadisk
Write-Output "VM $($vm1.name) data Disk Name $($datadisk.Name) Snapshot Begin"
$DataDiskSnapshotConfig = New-AzSnapshotConfig -SourceUri $dataDisk.Id -CreateOption Copy -Location $vmlocation
$snapshotNameData = "$($datadisk.name)_snapshot_$(Get-Date -Format ddMMyy)"
New-AzSnapshot -ResourceGroupName $resourceGroup -SnapshotName $snapshotNameData -Snapshot $DataDiskSnapshotConfig
Write-Output "VM $($vm1.name) data Disk $($datadisk.Name) Snapshot End"
}}
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup -ErrorAction SilentlyContinue
Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName -ErrorAction SilentlyContinue
$vmstatus = $null
while($vmstatus -notmatch 'running'){
$vmstatus=((get-azvm -Name $VmName -ResourceGroupName $resourceGroup -Status).Statuses[1]).code
}
Write-Host Your VM name $vmName is started and changed size from $currentvmsize changed to $newvmsize
}
elseif($currentsizefromazure -eq $newvmsize) { Write-host Your VM $vmName is already Upgraded}
else{Write-host VM current size is mismatched from azure size}
}
}
}
}
Giving below error while running this script on azure devops:
Please help to resolve error.
Unable to get the Open property of the Workbooks class
At C:\Users\azureadmin\Downloads\vsts-agent-win-x64-2.195.2\_work\1\s\Automatic VM backup.ps1:28 char:1
+ $newwb = $newExcel.Workbooks.Open($NewExcelFile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Note: Script is running on self hosted agent and this script is running when normal ISE also azconnection through devops azure
p.s. This script is change vm size and get backup

Enabling diagnostic settings for Azure Storage Account using PowerShell

I am trying to write a PowerShell script to enable Diagnostic settings for Azure Storage Accounts and send the logs to log analytics. For each storage account you can enable diagnostic for the storage account itself, blob, queue, table and file. I need to enable it for all 5 and configure to log read, write and delete, then send these logs to a Log Analytic workspace.
Here is a quick screenshot of the settings I want to enable.
I found couple examples on how to enable diagnostic using set-azdiagnosticsetting but they don't seem to work.
Set-AzDiagnosticSetting -ResourceId "Resource01" -Enabled $True
Set-AzDiagnosticSetting: Exception type: ErrorResponseException, Message: Null/Empty, Code: Null, Status code:Forbidden, Reason phrase: Forbidden
Next tried a different set of script, Create the metric, settings then apply. This example was also obtained from the reference link below.
$metric = New-AzDiagnosticDetailSetting -Metric -RetentionEnabled -Category AllMetrics -Enabled
$setting = New-AzDiagnosticSetting -Name $DiagnosticSettingName -ResourceId $ResourceId -WorkspaceId $WorkspaceId -Setting $metrics
Set-AzDiagnosticSetting -InputObject $setting
The only reference I found was:
https://learn.microsoft.com/en-us/powershell/module/az.monitor/set-azdiagnosticsetting?view=azps-6.0.0
https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/diagnostic-settings?tabs=PowerShell
Any one have better references or experience doing this??
The storage account and each storage(blob, file, queue, table) have different resource ids, so you need to use a loop to set the DiagnosticSettings for them, just use the script below, replace the values of yours, it works fine on my side.
$ResourceId = "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Storage/storageAccounts/joystoragev2"
$WorkspaceId = "/subscriptions/xxx/resourcegroups/xxx/providers/microsoft.operationalinsights/workspaces/joyana"
$DiagnosticSettingName = "testdia123"
$metric = New-AzDiagnosticDetailSetting -Metric -RetentionEnabled -Category AllMetrics -Enabled
$setting = New-AzDiagnosticSetting -Name $DiagnosticSettingName -ResourceId $ResourceId -WorkspaceId $WorkspaceId -Setting $metric
Set-AzDiagnosticSetting -InputObject $setting
$metric = New-AzDiagnosticDetailSetting -Metric -RetentionEnabled -Category AllMetrics -Enabled
$readlog = New-AzDiagnosticDetailSetting -Log -RetentionEnabled -Category StorageRead -Enabled
$writelog = New-AzDiagnosticDetailSetting -Log -RetentionEnabled -Category StorageWrite -Enabled
$deletelog = New-AzDiagnosticDetailSetting -Log -RetentionEnabled -Category StorageDelete -Enabled
$Ids = #($ResourceId + "/blobServices/default"
$ResourceId + "/fileServices/default"
$ResourceId + "/queueServices/default"
$ResourceId + "/tableServices/default"
)
$Ids | ForEach-Object {
$setting = New-AzDiagnosticSetting -Name $DiagnosticSettingName -ResourceId $_ -WorkspaceId $WorkspaceId -Setting $metric,$readlog,$writelog,$deletelog
Set-AzDiagnosticSetting -InputObject $setting
}

Need powershell script to enable diagnostic logging for Storage account and Key vault

I'm using below script to create a storage account, Key Vault and ADF. I would also like to enable diagnostic logging on both Storage account and Key Vault. Script runs fine and creates the resources however it does not enable the diagnostic logs for KV and Storage account. Would appreciate if you can help.
$subscription="Azure subscription 1"
$rgName = "Test"
$location = "eastus"
$storageaccountName = "tempaccountlogs"
$adfName = "tempdpadf"
$department = "Testtemp"
$kvname = "kvnamAkbt"
$sa = New-AzStorageAccount -ResourceGroupName $rgName -AccountName $storageaccountName -Location $location -SkuName Standard_LRS -Kind BlobStorage -AccessTier Hot -Tag #{department=$department}
$DataFactory = Set-AzDataFactoryV2 -Name $adfName -ResourceGroupName $rgName -Location $location -Tag #{chargecode=$chargeCode;department=$department;environment=$environment;project=$project}
$kv = New-AzKeyVault -VaultName $kvname -ResourceGroupName $rgName -Location $location
set-AzDiagnosticSetting -ResourceId $kv.ResourceId -StorageAccountId $sa.Id -Enabled $true -Categories AuditEvent
set-AzDiagnosticSetting -ResourceId $kv.ResourceId -StorageAccountId $sa.Id -RetentionEnabled $true -RetentionInDays 90
The problem with your script is that it gives error:
A parameter cannot be found that matches parameter name 'Categories'.
You are using "Categories" parameter instead of "Category". If you check this documentation correct parameter is -Category, use this as shown below:
set-AzDiagnosticSetting -ResourceId $kv.ResourceId -StorageAccountId $sa.Id -Enabled $true -Category AuditEvent
set-AzDiagnosticSetting -ResourceId $kv.ResourceId -StorageAccountId $sa.Id -RetentionEnabled $true -RetentionInDays 90
To enable logging for storage account, Please look at this documentation.
$diagname = "storage logs"
$ErrorActionPreference = "SilentlyContinue"
Import-Module -Name Az
Import-Csv "$home\azuresubscription.csv" |`
ForEach-Object{
#CentralLogAnalytics
$workspaceid = "your central logging resource id - exact object"
Select-AzSubscription -Subscription $_.Name
$storageAccounts = Get-AzStorageAccount | Select-Object Id
foreach ($stor in $storageAccounts)
{
Set-AzDiagnosticSetting -Name $diagname -ResourceId $stor.Id -WorkspaceId $workspaceid -Enabled $true
$blobid = -join($stor.id,"/blobServices/default")
$fileid = -join($stor.id, "/fileServices/default")
$queueid = -join($stor.id, "/queueServices/default")
$tableid = -join($stor.id, "/tableServices/default")
$resourcetypeid = #($blobid, $fileid, $queueid, $tableid)
foreach ($item in $resourcetypeid)
{
Set-AzDiagnosticSetting -Name $diagname -ResourceId $item -WorkspaceId $workspaceid -Enabled $true
}
}
}
Prerequisite:
The script expects a list of azure subscription in a CSV file. Putting in CSV is best to easy test in NonProd subscriptions. An object of the subscriptions can also be provided here.
Functionality:
This will enable the metrics for blob, queue, file, and table and at the parent level as well.
You should include -WorkspaceId parameter in the cmd. See reference here.
My example which runs successfully:
set-AzDiagnosticSetting -ResourceId $kv.ResourceId -StorageAccountId $sa.Id -Enabled $true -Category AuditEvent -WorkspaceId {resource id of the Log Analytics workspace}
For how to create a Log Analytics workspace, please refer to Create workspace.
Update:
For how to enable diagnostic logs for ADF, please refer to this sample:
$ws = Get-AzOperationalInsightsWorkspace -Name "testLAW" -ResourceGroupName "test"
$DataFactory = Set-AzDataFactoryV2 -ResourceGroupName "test" -Name "testADF" -Location "WestUS"
set-AzDiagnosticSetting -ResourceId $DataFactory.DataFactoryId -Enabled $true -WorkspaceId $ws.ResourceId

How to upload liquid files in an integration account via powershell script or any other automated way?

My liquid files are in git_hub repo and I want to upload liquid files in an integration account via powershell or cli
I'm using "New-AzResource"
New-AzResource -Location $ResourceLocation -PropertyObject $PropertiesObject -ResourceGroupName $ResouceGroupname -ResourceType Microsoft.Logic/integrationAccounts/maps -ResourceName "$IntegrationAccountName/$ResourceName" -ApiVersion 2016-06-01 -Force
Login-AzureRmAccount
$IntegrationAccountName = "Integration Account name"
$ResouceGroupname = "ResourcegroupName"
$ResourceLocation = "westus" # location
$ResourceName = "liquid name"
$Content = Get-Content -Path "C:\Tom\simple.liquid" | Out-String
Write-Host $Content
$PropertiesObject = #{
mapType = "liquid"
content = "$Content"
contentType = "text/plain"
}
New-AzureRmResource -Location $ResourceLocation -PropertyObject $PropertiesObject -ResourceGroupName $ResouceGroupname -ResourceType Microsoft.Logic/integrationAccounts/maps -ResourceName " $IntegrationAccountName/$ResourceName" -ApiVersion 2016-06-01 -Force
source: https://stackoverflow.com/a/49063466/1384539

Resources