Friday 26 June 2020

Create XML from class or Object in C-Sharp

function to serialize object to xml in c#

public static string SerializeObjectToXML(object item)
        {
            try
            {
                string xmlText;
                Type objectType = item.GetType();
                XmlSerializer xmlSerializer = new XmlSerializer(objectType);
                MemoryStream memoryStream = new MemoryStream();
                using (XmlTextWriter xmlTextWriter =
                    new XmlTextWriter(memoryStream, Encoding.UTF8) { Formatting = Formatting.Indented })
                {
                    xmlSerializer.Serialize(xmlTextWriter, item);
                    memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                    xmlText = new UTF8Encoding().GetString(memoryStream.ToArray());
                    memoryStream.Dispose();
                    return xmlText;
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Write(e.ToString());
                return null;
            }
        }

Powershell script to create OS disk in azure from storage account blob image file (vhd)

Powershell script to create OS disk in azure from storage account blob image file (vhd)


#Provide the subscription Id where Managed Disks will be created
$subscriptionId = 'd7x2bxx8-62xe-4xdx-xa8x-20x0x3x2x5xx'

#Provide the name of your resource group where Managed Disks will be created. 
$resourceGroupName ='MYResourceGroupName'

#Provide the name of the Managed Disk
$diskName = 'Project_Disk2'

#Provide the size of the disks in GB. It should be greater than the VHD file size.
$diskSize = '90'

#Provide the storage type for Managed Disk. PremiumLRS or StandardLRS.
$storageType = 'Premium_LRS'

#Provide the Azure region (e.g. westus) where Managed Disk will be located.
#This location should be same as the storage account where VHD file is stored
#Get all the Azure location using command below:
#Get-AzLocation
$location = 'ukwest'

#Provide the URI of the VHD file (page blob) in a storage account. Please not that this is NOT the SAS URI of the storage container where VHD file is stored. 
#e.g. https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd
#Note: VHD file can be deleted as soon as Managed Disk is created.
$sourceVHDURI = 'https://sanjeebojha.blob.core.windows.net/myproject/myoriginalVM.vhd'

#Provide the resource Id of the storage account where VHD file is stored.
#e.g. /subscriptions/6472s1g8-h217-446b-b509-314e17e1efb0/resourceGroups/MDDemo/providers/Microsoft.Storage/storageAccounts/contosostorageaccount
#This is an optional parameter if you are creating managed disk in the same subscription
$storageAccountId = '/subscriptions/d7x2bxx8-62xe-4xdx-xa8x-20x0x3x2x5xx/resourceGroups/MYResourceGroupName/providers/Microsoft.Storage/storageAccounts/sanjeebojha'

#Set the context to the subscription Id where Managed Disk will be created
Select-AzSubscription -SubscriptionId $SubscriptionId

$diskConfig = New-AzDiskConfig -AccountType $storageType -OsType Windows -Location $location -CreateOption Import -StorageAccountId $storageAccountId -SourceUri $sourceVHDURI 

New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName

Solution to the CORS issue

CORS:Cross-Origin Resource Sharing

In the web.config file, add the following:


<system.webServer>
   <httpProtocol>
     <customHeaders>
        <add name="access-control-allow-origin" value="*" />
        <add name="access-control-allow-headers" value="content-type" />
     </customHeaders>
   </httpProtocol>
</system.webServer>  
Enable anonymous authentication 
    <anonymousAuthentication enabled="true"/>

Convert Premium SSD to Standard HDD in Azure

Powershell script to convert Premium SSD disk to standard HDD 


# Name of the resource group that contains the VM
$rgName = 'MYResourceGroupName'

# Name of the your virtual machine
$vmName = 'MyVirtualMachineName'

# Choose between Standard_LRS and Premium_LRS based on your scenario
$storageType = 'Standard_LRS'

# Premium capable size
# Required only if converting storage from Standard to Premium
#$size = 'Standard_DS2_v2'


#Provide the subscription Id where Managed Disks will be created
$subscriptionId = 'd7x2bxx8-62xe-4xdx-xa8x-20x0x3x2x5xx'

Select-AzSubscription -SubscriptionId $SubscriptionId



# Stop and deallocate the VM before changing the size
Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force

$vm = Get-AzVM -Name $vmName -resourceGroupName $rgName

# Change the VM size to a size that supports Premium storage
# Skip this step if converting storage from Premium to Standard
#$vm.HardwareProfile.VmSize = $size
#Update-AzVM -VM $vm -ResourceGroupName $rgName

# Get all disks in the resource group of the VM
$vmDisks = Get-AzDisk -ResourceGroupName $rgName 

# For disks that belong to the selected VM, convert to Premium storage
foreach ($disk in $vmDisks)
{
if ($disk.ManagedBy -eq $vm.Id)
{
$diskUpdateConfig = New-AzDiskUpdateConfig –AccountType $storageType
Update-AzDisk -DiskUpdate $diskUpdateConfig -ResourceGroupName $rgName `
-DiskName $disk.Name
}
}

Start-AzVM -ResourceGroupName $rgName -Name $vmName

Generate SQL script from entity framework using powershell/visual studio

to run the below command , start PowerShell in visual studio. Select the database project. In PowerShell:   Script-Migration this command wi...