{"id":3731,"date":"2016-12-21T19:41:29","date_gmt":"2016-12-21T19:41:29","guid":{"rendered":"http:\/\/www.tsls.co.uk\/?p=3731"},"modified":"2024-04-22T08:57:32","modified_gmt":"2024-04-22T08:57:32","slug":"restore-azure-arm-virtual-machine-special-configuration","status":"publish","type":"post","link":"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/","title":{"rendered":"Restore Azure ARM Virtual Machine with Special Configuration"},"content":{"rendered":"<p>Below is a handy script for restoring a VM with Special Configuration<\/p>\n<p>Please Note: this will only work where the VM still exists in the Azure. this process would be used to recover the VM from a previous day or if became corrupt. If the VM completely disappeared then additional steps will be required to manually recreate Availability Sets and NICs.<\/p>\n<p>This script focus on restore NIC, ILB, Availability Sets , OMS, Diagnostic Settings and Antimalware<\/p>\n<p><code><br \/>\n# Login to Azure PowerShell<br \/>\nLogin-AzureRmAccount<br \/>\n$YourSubscriptionName= '<SUBSCRIPTIONNAME>'<br \/>\nGet-AzureRmSubscription \u2013SubscriptionName $YourSubscriptionName | Select-AzureRmSubscription<br \/>\nGet-AzureSubscription \u2013SubscriptionName $YourSubscriptionName | Select-AzureRmSubscription<br \/>\nGet-AzureRmContext<\/p>\n<p>#Set the restore date<br \/>\n$startDate = Get-Date -Date '21 December 2016 00:00:00'<br \/>\n$endDate = Get-Date -Date '21 December 2016 23:59:59'<\/p>\n<p>#Global Settings<br \/>\n$loc = 'North Europe'<br \/>\n$rgname = '<RGNAME>'<br \/>\n$VMName = '<VMNAME>'<br \/>\n$RestoreVMName = $VMName #Only Change if you want to give the VM a New Name (for Testing)<br \/>\n$VaultName = '<BACKUPVAULTNAME>'<br \/>\n$StorageAccount = '<STORAGEACCOUNTNAME>'<br \/>\n$workspaceName = \"<OMSWORKSPACE>\" #The OMS Name<\/p>\n<p>#XML Download name and location<br \/>\n$diagnosticsconfig_path = 'DiagnosticsPubConfig.xml'<br \/>\n$VMNameConfig_path = 'VMConfig.json'<br \/>\n$RestoreVMNameConfig_path = 'RestoreVMConfig.json'<\/p>\n<p>#Backup Diagnostic Settings<br \/>\n$publicsettings = (Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname).PublicSettings<br \/>\n$encodedconfig = (ConvertFrom-Json -InputObject $publicsettings).xmlCfg<br \/>\n$xmlconfig = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedconfig))<br \/>\n$xmlconfig | Export-Clixml -Path $diagnosticsconfig_path<\/p>\n<p>#Backup VM Settings<br \/>\n$VmObject = Get-AzureRmVM -ResourceGroupName $rgname -Name $Vmname<br \/>\nConvertTo-Json -InputObject $VmObject | Out-File -FilePath $VMNameConfig_path<br \/>\n$destination_path = $VMNameConfig_path<br \/>\n$oldconfobj = ((Get-Content -Path $destination_path -Encoding Unicode)).TrimEnd([char]0x00) | ConvertFrom-Json<\/p>\n<p>#Remember to remove the remark to remove the VM<br \/>\n#Remove-AzureRmVM -ResourceGroupName $rgname -Name $VMName<\/p>\n<p>$vault = Get-AzureRmRecoveryServicesVault -Name $VaultName -ResourceGroupName $rgname<br \/>\nSet-AzureRmRecoveryServicesVaultContext -Vault $vault<\/p>\n<p>$namedContainer = Get-AzureRmRecoveryServicesBackupContainer  -ContainerType AzureVM \u2013Status Registered -FriendlyName $RestoreVMName<br \/>\n$backupitem = Get-AzureRmRecoveryServicesBackupItem \u2013Container $namedContainer  \u2013WorkloadType \"AzureVM\"<\/p>\n<p>#Select the backup Set<br \/>\n$rp = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime()<br \/>\n$rp[0]<\/p>\n<p>#Start the Restore Process<br \/>\n$restorejob = Restore-AzureRmRecoveryServicesBackupItem -RecoveryPoint $rp[0] -StorageAccountName $StorageAccount -StorageAccountResourceGroupName $rgname<br \/>\n$restorejob<\/p>\n<p>#Wait for Restore to complete<br \/>\n$restorejob = Get-AzureRmRecoveryServicesBackupJob -Job $restorejob<br \/>\n$details = Get-AzureRmRecoveryServicesBackupJobDetails -Job $restorejob<\/p>\n<p>#Get Details from Backup Configuration Properties<\/p>\n<p>$properties = $details.properties<br \/>\n$storageAccountName = $properties[\"Target Storage Account Name\"]<br \/>\n$containerName = $properties[\"Config Blob Container Name\"]<br \/>\n$blobName = $properties[\"Config Blob Name\"]<\/p>\n<p>Set-AzureRmCurrentStorageAccount -Name $storageaccountname -ResourceGroupName $rgname<br \/>\n$Restoredestination_path = $RestoreVMNameConfig_path<br \/>\nGet-AzureStorageBlobContent -Container $containerName -Blob $blobName -Destination $Restoredestination_path<br \/>\n$obj = ((Get-Content -Path $Restoredestination_path -Encoding Unicode)).TrimEnd([char]0x00) | ConvertFrom-Json<br \/>\n$vm = New-AzureRmVMConfig -VMSize $obj.HardwareProfile.VirtualMachineSize -VMName $VMName -AvailabilitySetId $oldconfobj.AvailabilitySetReference.id #$AvailabilitySet.Id<\/p>\n<p>Set-AzureRmVMOSDisk -VM $vm -Name \"osdisk\" -VhdUri $obj.StorageProfile.OSDisk.VirtualHardDisk.Uri -CreateOption \u201cAttach\u201d<br \/>\n$vm.StorageProfile.OsDisk.OsType = $obj.StorageProfile.OSDisk.OperatingSystemType foreach($dd in $obj.StorageProfile.DataDisks)<br \/>\n{<br \/>\n $vm = Add-AzureRmVMDataDisk -VM $vm -Name \"datadisk1\" -VhdUri $dd.VirtualHardDisk.Uri -DiskSizeInGB 127 -Lun $dd.Lun -CreateOption Attach<br \/>\n}<\/p>\n<p>#Connect existing NIC<br \/>\n$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $oldconfobj.NetworkInterfaceIDs[0]<\/p>\n<p>#CreateVM<br \/>\n$vm.StorageProfile.OsDisk.OsType = $obj.StorageProfile.OSDisk.OperatingSystemType<br \/>\nNew-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm<\/p>\n<p>#Attach Antimalware Extension<br \/>\n$RegularServer = @'<br \/>\n{<br \/>\n    \"AntimalwareEnabled\": true,<br \/>\n    \"RealtimeProtectionEnabled\": true,<br \/>\n    \"ScheduledScanSettings\": {<br \/>\n        \"isEnabled\": false,<br \/>\n        \"day\": 1,<br \/>\n        \"time\": 10,<br \/>\n        \"scanType\": \"Full\"<br \/>\n    },<br \/>\n    \"Exclusions\": {<br \/>\n        \"Extensions\": \".MDF;.LDF;.NDF\",<br \/>\n        \"Paths\": \"%systemroot%:\\\\inetpub\\\\temp\\\\IIS\",<br \/>\n        \"Processes\": \"\"<br \/>\n    }<br \/>\n}<br \/>\n'@<br \/>\n$TypeHandlerVersion = ((Get-AzureRmVMExtensionImage -Location 'North Europe' -PublisherName 'Microsoft.Azure.Security' -Type 'IaaSAntimalware').Version[-1][0..2] -join '')<br \/>\nSet-AzureRmVMExtension -ResourceGroupName $rgname -VMName $vmname -Name 'IaaSAntimalware' -Publisher 'Microsoft.Azure.Security' -ExtensionType 'IaaSAntimalware' -SettingString $RegularServer -Location $loc -TypeHandlerVersion $TypeHandlerVersion<\/p>\n<p>#Enable Diagnostic Logging<br \/>\nSet-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname -DiagnosticsConfigurationPath $diagnosticsconfig_path<\/p>\n<p>#Attach OMS Resource<br \/>\n$workspace = (Get-AzureRmOperationalInsightsWorkspace).Where({$_.Name -eq $workspaceName})<br \/>\nif ($workspace.Name -ne $workspaceName)<br \/>\n{<br \/>\n    Write-Error \"Unable to find OMS Workspace $workspaceName. Do you need to run Select-AzureRMSubscription?\"<br \/>\n}<br \/>\n$workspaceId = $workspace.CustomerId<br \/>\n$workspaceKey = (Get-AzureRmOperationalInsightsWorkspaceSharedKeys -ResourceGroupName $workspace.ReourceGroupName -Name $workspace.Name).PrimarySharedKey<br \/>\nSet-AzureRmVMExtension -ResourceGroupName $rgname -VMName $VMName -Name 'MicrosoftMonitoringAgent' -Publisher 'Microsoft.EnterpriseCloud.Monitoring' -ExtensionType 'MicrosoftMonitoringAgent' -TypeHandlerVersion '1.0' -Location $loc -SettingString \"{'workspaceId': '$workspaceId'}\" -ProtectedSettingString \"{'workspaceKey': '$workspaceKey'}\"<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a handy script for restoring a VM with Special Configuration Please Note: this will only work where the VM still exists in the Azure. this process would be used to recover the VM<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[21],"tags":[],"class_list":["post-3731","post","type-post","status-publish","format-standard","hentry","category-microsoft"],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>Restore Azure ARM Virtual Machine with Special Configuration - TSLS - Luke Smith<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_GB\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Restore Azure ARM Virtual Machine with Special Configuration - TSLS - Luke Smith\" \/>\r\n<meta property=\"og:description\" content=\"Below is a handy script for restoring a VM with Special Configuration Please Note: this will only work where the VM still exists in the Azure. this process would be used to recover the VM\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/\" \/>\r\n<meta property=\"og:site_name\" content=\"TSLS - Luke Smith\" \/>\r\n<meta property=\"article:published_time\" content=\"2016-12-21T19:41:29+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2024-04-22T08:57:32+00:00\" \/>\r\n<meta name=\"author\" content=\"Luke Smith\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Luke Smith\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/\",\"url\":\"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/\",\"name\":\"Restore Azure ARM Virtual Machine with Special Configuration - TSLS - Luke Smith\",\"isPartOf\":{\"@id\":\"https:\/\/www.tsls.co.uk\/#website\"},\"datePublished\":\"2016-12-21T19:41:29+00:00\",\"dateModified\":\"2024-04-22T08:57:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.tsls.co.uk\/#\/schema\/person\/e4d7dac4fe1b3f8df31f3857bb3ebda7\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.tsls.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Restore Azure ARM Virtual Machine with Special Configuration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.tsls.co.uk\/#website\",\"url\":\"https:\/\/www.tsls.co.uk\/\",\"name\":\"TSLS - Luke Smith\",\"description\":\"- Knowledge - Thoughts - Microsoft -\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.tsls.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.tsls.co.uk\/#\/schema\/person\/e4d7dac4fe1b3f8df31f3857bb3ebda7\",\"name\":\"Luke Smith\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/www.tsls.co.uk\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/29abc50f07a4ebe68cb4f31981884f89b2157d7e4ed63b09631d40c0717faa94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/29abc50f07a4ebe68cb4f31981884f89b2157d7e4ed63b09631d40c0717faa94?s=96&d=mm&r=g\",\"caption\":\"Luke Smith\"},\"description\":\"I\u2019ve been working with Microsoft Technologies for over 20 years, my main focus now being Microsoft Online Services. I manage the Cloud Services at ElysianIT Limited and as a P-SELLER at Microsoft. I have worked with many organisations from SMC to Enterprise. I\u2019ve been working with Microsoft Technologies since DOS 5.0, to date I have been working on Microsoft\u2019s latest cloud technology Windows Azure, Windows 10 Office 365 and Microsoft SharePoint\",\"sameAs\":[\"http:\/\/www.tsls.co.uk\"],\"url\":\"https:\/\/www.tsls.co.uk\/index.php\/author\/luke\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Restore Azure ARM Virtual Machine with Special Configuration - TSLS - Luke Smith","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/","og_locale":"en_GB","og_type":"article","og_title":"Restore Azure ARM Virtual Machine with Special Configuration - TSLS - Luke Smith","og_description":"Below is a handy script for restoring a VM with Special Configuration Please Note: this will only work where the VM still exists in the Azure. this process would be used to recover the VM","og_url":"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/","og_site_name":"TSLS - Luke Smith","article_published_time":"2016-12-21T19:41:29+00:00","article_modified_time":"2024-04-22T08:57:32+00:00","author":"Luke Smith","twitter_misc":{"Written by":"Luke Smith","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/","url":"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/","name":"Restore Azure ARM Virtual Machine with Special Configuration - TSLS - Luke Smith","isPartOf":{"@id":"https:\/\/www.tsls.co.uk\/#website"},"datePublished":"2016-12-21T19:41:29+00:00","dateModified":"2024-04-22T08:57:32+00:00","author":{"@id":"https:\/\/www.tsls.co.uk\/#\/schema\/person\/e4d7dac4fe1b3f8df31f3857bb3ebda7"},"breadcrumb":{"@id":"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.tsls.co.uk\/index.php\/2016\/12\/21\/restore-azure-arm-virtual-machine-special-configuration\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.tsls.co.uk\/"},{"@type":"ListItem","position":2,"name":"Restore Azure ARM Virtual Machine with Special Configuration"}]},{"@type":"WebSite","@id":"https:\/\/www.tsls.co.uk\/#website","url":"https:\/\/www.tsls.co.uk\/","name":"TSLS - Luke Smith","description":"- Knowledge - Thoughts - Microsoft -","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.tsls.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.tsls.co.uk\/#\/schema\/person\/e4d7dac4fe1b3f8df31f3857bb3ebda7","name":"Luke Smith","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.tsls.co.uk\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/29abc50f07a4ebe68cb4f31981884f89b2157d7e4ed63b09631d40c0717faa94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/29abc50f07a4ebe68cb4f31981884f89b2157d7e4ed63b09631d40c0717faa94?s=96&d=mm&r=g","caption":"Luke Smith"},"description":"I\u2019ve been working with Microsoft Technologies for over 20 years, my main focus now being Microsoft Online Services. I manage the Cloud Services at ElysianIT Limited and as a P-SELLER at Microsoft. I have worked with many organisations from SMC to Enterprise. I\u2019ve been working with Microsoft Technologies since DOS 5.0, to date I have been working on Microsoft\u2019s latest cloud technology Windows Azure, Windows 10 Office 365 and Microsoft SharePoint","sameAs":["http:\/\/www.tsls.co.uk"],"url":"https:\/\/www.tsls.co.uk\/index.php\/author\/luke\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2gf1k-Yb","_links":{"self":[{"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/3731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=3731"}],"version-history":[{"count":3,"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/3731\/revisions"}],"predecessor-version":[{"id":3761,"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/3731\/revisions\/3761"}],"wp:attachment":[{"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=3731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=3731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tsls.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=3731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}