0% found this document useful (0 votes)
79 views

Powershell Commands

The document outlines the steps to create a virtual machine in Azure including: 1) Creating a resource group and storage account 2) Setting up a virtual network with a subnet and network security group 3) Creating a network interface and attaching it to the virtual machine 4) Configuring and creating the virtual machine 5) Getting the public IP address to connect to the virtual machine 6) Deleting the resource group

Uploaded by

Pavan P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Powershell Commands

The document outlines the steps to create a virtual machine in Azure including: 1) Creating a resource group and storage account 2) Setting up a virtual network with a subnet and network security group 3) Creating a network interface and attaching it to the virtual machine 4) Configuring and creating the virtual machine 5) Getting the public IP address to connect to the virtual machine 6) Deleting the resource group

Uploaded by

Pavan P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Create Resource Group

# Create variables to store the location and resource group names.


$location = "southindia"
$ResourceGroupName = "sansbound"

New-AzureRmResourceGroup `
-Name $ResourceGroupName `
-Location $location

Create Storage account

# Create variables to store the storage account name and the storage account SKU
information
$StorageAccountName = "sansboundstoragedisks"
$SkuName = "Standard_LRS"

# Create a new storage account


$StorageAccount = New-AzureRMStorageAccount `
-Location $location `
-ResourceGroupName $ResourceGroupName `
-Type $SkuName `
-Name $StorageAccountName

Set-AzureRmCurrentStorageAccount `
-StorageAccountName $storageAccountName `
-ResourceGroupName $resourceGroupName

# Create a storage container to store the virtual machine image


$containerName = 'osdisks'
$container = New-AzureStorageContainer `
-Name $containerName `
-Permission Blob

Create Virtual Network

# Create a subnet configuration


$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
-Name mySubnet `
-AddressPrefix 192.168.1.0/24

# Create a virtual network


$vnet = New-AzureRmVirtualNetwork `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-Name MyVnet `
-AddressPrefix 192.168.0.0/16 `
-Subnet $subnetConfig
# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-AllocationMethod Static `
-IdleTimeoutInMinutes 4 `
-Name "mypublicdns$(Get-Random)"

Create Network Security Group with New Inbound Rule (allow 80, 3389)

# Create an inbound network security group rule for port 3389


$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleRDP `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 `
-Access Allow

# Create an inbound network security group rule for port 80


$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleWWW `
-Protocol Tcp `
-Direction Inbound `
-Priority 1001 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 80 `
-Access Allow

# Create a network security group


$nsg = New-AzureRmNetworkSecurityGroup `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-Name myNetworkSecurityGroup `
-SecurityRules $nsgRuleRDP,$nsgRuleWeb

Create NIC card for a VM and Attach public IP Address

# Create a virtual network card and associate it with public IP address and NSG
$nic = New-AzureRmNetworkInterface `
-Name myNic `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $pip.Id `
-NetworkSecurityGroupId $nsg.Id

Create a New Virtual Machine

# Define a credential object to store the username and password for the virtual
machine
$UserName='demouser'
$Password='Password@123'| ConvertTo-SecureString -Force -AsPlainText
$Credential=New-Object PSCredential($UserName,$Password)

# Create the virtual machine configuration object


$VmName = "VirtualMachinelatest"
$VmSize = "Standard_A1"
$VirtualMachine = New-AzureRmVMConfig `
-VMName $VmName `
-VMSize $VmSize

$VirtualMachine = Set-AzureRmVMOperatingSystem `
-VM $VirtualMachine `
-Windows `
-ComputerName "MainComputer" `
-Credential $Credential

$VirtualMachine = Set-AzureRmVMSourceImage `
-VM $VirtualMachine `
-PublisherName "MicrosoftWindowsServer" `
-Offer "WindowsServer" `
-Skus "2016-Datacenter" `
-Version "latest"

$osDiskName = "OsDisk"
$osDiskUri = '{0}vhds/{1}-{2}.vhd' -f `
$StorageAccount.PrimaryEndpoints.Blob.ToString(),`
$vmName.ToLower(), `
$osDiskName

# Sets the operating system disk properties on a virtual machine.


$VirtualMachine = Set-AzureRmVMOSDisk `
-VM $VirtualMachine `
-Name $osDiskName `
-VhdUri $OsDiskUri `
-CreateOption FromImage | `
Add-AzureRmVMNetworkInterface -Id $nic.Id

# Create the virtual machine.


New-AzureRmVM `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-VM $VirtualMachine

Get public IP address of the VM

Get-AzureRmPublicIpAddress `
-ResourceGroupName $ResourceGroupName | Select IpAddress

Connect VM

mstsc /v <publicIpAddress>

Delete Resource Group

Remove-AzureRmResourceGroup `
-Name $ResourceGroupName

You might also like