Last active
January 23, 2024 19:09
-
-
Save achingono/75f711c3ebee80a95cf0ed33c09651dd to your computer and use it in GitHub Desktop.
Clone production virtual machine to a development subscription
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory = $true)] | |
[string]$name, | |
[Parameter(Mandatory = $true)] | |
[string]$resourceGroup, | |
[Parameter(Mandatory = $true)] | |
[string]$subscription, | |
[Parameter(Mandatory = $true)] | |
[string]$targetResourceGroup, | |
[Parameter(Mandatory = $true)] | |
[string]$targetSubscription | |
) | |
# Get the location of the resource group | |
$location = az group show --name $resourceGroup --subscription $subscription --query "location" -o tsv; | |
$snapshotName = $targetResourceGroup -replace "rg-", "ss-"; | |
# Get the Id of the source disk | |
$sourceDisk = az vm show --name $name --resource-group $resourceGroup --subscription $subscription --query 'storageProfile.osDisk.managedDisk.id' -o tsv; | |
# Create the target resource group | |
az group create --name $targetResourceGroup --subscription $targetSubscription --location $location | |
# Create a snapshot in target subscription | |
az snapshot create --name $snapshotName --resource-group $targetResourceGroup --subscription $targetSubscription --source $sourceDisk --sku Standard_LRS; | |
# Create a new OS disk from the snapshot using Azure CLI | |
$diskName = $targetResourceGroup -replace "rg-", "vhd-"; | |
$snapshotId = az snapshot show --name $snapshotName --resource-group $targetResourceGroup --subscription $targetSubscription --query 'id' -o tsv; | |
az disk create --name $diskName --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--source $snapshotId --sku Standard_LRS --location $location --zone "1"; | |
# Create a public IP | |
$ipName = $targetResourceGroup -replace "rg-", "ip-"; | |
az network public-ip create --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--name $ipName --version IPv4 --sku Standard; | |
# Create a Virtual Network | |
$vnetName = ($targetResourceGroup -replace "rg-", "vnet-"); | |
az network vnet create --name $vnetName --resource-group $targetResourceGroup --subscription $targetSubscription; | |
# Create a Virtual Network | |
$subnetName = 'default'; | |
az network vnet subnet create --name $subnetName --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--vnet-name $vnetName --address-prefixes '10.0.0.0/24'; | |
# Create a Network Security Group | |
$nsgName = ($targetResourceGroup -replace "rg-", "nsg-"); | |
az network nsg create --name $nsgName --resource-group $targetResourceGroup --subscription $targetSubscription; | |
az network nsg rule create --name "AllowAnyRDPInbound" --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--nsg-name $nsgName --priority 100 --protocol Tcp --access Allow --direction Inbound ` | |
--source-port-ranges '*' --destination-port-ranges '3389'; | |
az network nsg rule create --name "AllowAnyWinRMInbound" --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--nsg-name $nsgName --priority 110 --protocol Tcp --access Allow --direction Inbound ` | |
--source-port-ranges '*' --destination-port-ranges '5986'; | |
az network nsg rule create --name "AllowAnyHttpInbound" --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--nsg-name $nsgName --priority 120 --protocol Tcp --access Allow --direction Inbound ` | |
--source-port-ranges '*' --destination-port-ranges '80'; | |
az network nsg rule create --name "AllowAnyHttpInbound" --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--nsg-name $nsgName --priority 130 --protocol Tcp --access Allow --direction Inbound ` | |
--source-port-ranges '*' --destination-port-ranges '443'; | |
# Create a new NIC | |
$nicName = ($targetResourceGroup -replace "rg-", "nic-"); | |
$subnetId = az network vnet subnet show --name $subnetName --resource-group $targetResourceGroup --subscription $targetSubscription --vnet-name $vnetName --query 'id' -o tsv; | |
$nsgId = az network nsg show --name $nsgName --resource-group $targetResourceGroup --subscription $targetSubscription --query 'id' -o tsv; | |
az network nic create --name $nicName --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--subnet $subnetId --network-security-group $nsgId --public-ip-address $ipName; ; | |
#Create a new virtual machine from the disk using Azure CLI | |
$vmName = ($targetResourceGroup -replace "rg-", "vm-"); | |
az vm create --name $vmName --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--location $location --attach-os-disk $diskName --os-type windows --nics $nicName ` | |
--size Standard_B4ms --public-ip-sku Standard --security-type Standard; | |
# Set the auto-shutdown and auto-start properties for the VM | |
az vm auto-shutdown --name $vmName --resource-group $targetResourceGroup --subscription $targetSubscription ` | |
--time "00:00" --email "development@vcasoftware.com"; | |
# Enable WinRM on the new virtual machine | |
az vm run-command invoke --command-id EnableRemotePS --name $vmName --resource-group $targetResourceGroup --subscription $targetSubscription; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment