Saturday, December 28, 2019

Create VM using PowerShell Script

Lab Exercise includes as below




In this lab we will be implementing centOs 7.0 with PowerShell script later we will be configuring Apache Server.


Resource Group   - MyRG
Location             - EastUs
Virtual Network  - VNet
IP Address         - 192.168.0.0/16
Subnet              - 192.168.0.0/24
Subnet Name     - MySubnet
NSG Name          - MyNSG
Storage Account - awaneesh (must be unique)
VM Name           - centOS-01
OS Type             - centOS
OS Version         - 7.5
VM Size              - Standard_B1s
Public IP            - PIP (Dynamic)
NIC                    - NIC 
OS Disk              - centOS-01-OsDisk 
==
Output as Below






Run Blow Script in Windows PowerShellISE

================Script========

# Define variables for networking part
$ResourceGroup  = "MyRG"
$Location           = "EastUS"
$vNetName        = "VNet"
$AddressSpace   = "192.168.0.0/16" # Format 10.10.0.0/16
$SubnetIPRange  = "192.168.0.0/24" # Format 10.10.0.0/24
$SubnetName     = "MySubnet"
$nsgName          = "MyNSG"
$StorageAccount = "awaneesh" # Name must be unique. Name availability can be check using PowerShell command Get-AzStorageAccountNameAvailability -Name "awaneesh"

# Create Resource Groups and Storage Account for diagnostic
New-AzResourceGroup -Name $ResourceGroup -Location $Location
New-AzStorageAccount -Name $StorageAccount -ResourceGroupName $ResourceGroup -Location $Location -SkuName Standard_LRS

# Create Virtual Network and Subnet
$vNetwork = New-AzVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName -AddressPrefix $AddressSpace -Location $location
Add-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNetwork -AddressPrefix $SubnetIPRange
Set-AzVirtualNetwork -VirtualNetwork $vNetwork

# Create Network Security Group
$nsgRuleVMAccess = New-AzNetworkSecurityRuleConfig -Name 'allow-vm-access' -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 80,22,3389 -Access Allow
New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Location $location -Name $nsgName -SecurityRules $nsgRuleVMAccess

# Define Variables needed for Virtual Machine
$vNet       = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName
$Subnet     = Get-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNet
$nsg        = Get-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Name $NsgName
$vmName = "centOS-01"
$pubName = "OpenLogic"
$offerName = "centOS"
$skuName = "7.5"
$vmSize = "Standard_B1s"
$pipName    = "$vmName-pip" 
$nicName    = "$vmName-nic"
$osDiskName = "$vmName-OsDisk"
$osDiskSize = "30"
$osDiskType = "Premium_LRS"

# Create Admin Credentials
$adminUsername = Read-Host 'Admin username'
$adminPassword = Read-Host -AsSecureString 'Admin password with least 12 characters'
$adminCreds    = New-Object PSCredential $adminUsername, $adminPassword

# Create a public IP and NIC
$pip = New-AzPublicIpAddress -Name $pipName -ResourceGroupName $ResourceGroup -Location $location -AllocationMethod Static 
$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroup -Location $location -SubnetId $Subnet.Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

# Set VM Configuration
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize
Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id

# Set VM operating system parameters
Set-AzVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential $adminCreds

# Set boot diagnostic storage account
Set-AzVMBootDiagnostics -Enable -ResourceGroupName $ResourceGroup -VM $vmConfig -StorageAccountName $StorageAccount

# Set virtual machine source image
Set-AzVMSourceImage -VM $vmConfig -PublisherName $pubName -Offer $offerName -Skus $skuName -Version 'latest'

# Set OsDisk configuration
Set-AzVMOSDisk -VM $vmConfig -Name $osDiskName -DiskSizeInGB $osDiskSize -StorageAccountType $osDiskType -CreateOption fromImage

# Create the VM
New-AzVM -ResourceGroupName $ResourceGroup -Location $location -VM $vmConfig 

=========
VM Implemented as below

 

 Lets Login to VM - command line-

ssh your_username@host_ip_address

ssh awaneesh@40.76.50.224

Command line for install Apache-

sudo yum update httpd


Once the packages are updated, install the Apache package:

sudo yum install httpd



Apache does not automatically start on CentOS once the installation completes. You will need to start the Apache process manually:
 

sudo systemctl start httpd

Verify that the service is running with the following command: 


sudo systemctl status httpd

You will see an active status when the service is running:
 

 





When you have your server’s IP address, enter it into your browser’s address bar:

http://your_server_ip

http://40.76.50.224

 









No comments:

Post a Comment