Windows PowerShell 4.0

 ITMug_PowerShell4_Cloud   Windows PowerShell 4.0

Windows PowerShell 4.0 is installed by default Windows Server 2012 R2.
Most IT professionals who have not used PowerShell are scared of this administration and scripting language, because it is different from how they normally work.

PowerShell is a powerful language; it enables simple tasks to be completed more quickly and enables complex tasks to be done with a mouse click.

Before starting your journey in cloud computing, you have to be familiar with basic power shell commands. I am sure you will like PowerShell, as it will allow you to complete tasks in a much easier manner.

Let’s take a look at PowerShell 4.0 and complete basic Hyper-V task

How to start / stop “ITMug01” VM from power shell command

Start-vm ITMug01
Stop-vm ITMug01

In my lab and production servers, I am using a name convention to make my job easier (name convention such as ITMugxx, SmartITxx…etc.). Of course in your infrastructure, you can add additional 2 characters for departments. Examples to this could be, but not limited to:  ITmugAC01, ITMugFN0, ITmugIT01 .

 

Now let’s start/stop all ITMug’s servers

Stop-vm ITMug*
Start-vm ITMug*

How to Disconnect / Reconnect VMs from Network

# Disconnect /Reconnect single VM ITMug01
Get-VMNetworkAdapter ITMug01 | Disconnect-VMNetworkAdapter
Get-VMNetworkAdapter ITMug01 | Connect-VMNetworkAdapter -SwitchName “External virtual Network”

# Disconnect all ITMug VMs from the Virtual Switch
Get-VMNetworkAdapter ITMug* | Disconnect-VMNetworkAdapter

# Reconnect all ITMug VMs NIC’s back to the Virtual Switch
Get-VMNetworkAdapter ITMug* | Connect-VMNetworkAdapter -SwitchName “External virtual Network”

How to report on the key information this includes the VM name, interface name, MAC, VSID, and current IP addresses

Get-VMNetworkAdapter -VMName ITMug*, SmartIT* | Select VMName, Name, MacAddress, VirtualSubnetID, IPAddresses | ft -AutoSize

How to set the VirtualSubnetID by VM name

Get-VMNetworkAdapter ITMug* | Set-VMNetworkAdapter -VirtualSubnetId 4501
Get-VMNetworkAdapter SmartIT* | Set-VMNetworkAdapter -VirtualSubnetId 4601

How to set the VirtualSubnetID by MAC Address

In some scenarios, your virtual machine may have more than one virtual interface and are connected to different Subnets.

# ITMug VMs have a MAC address starting with 00-45-01
Get-VMNetworkAdapter | where {$_.MACAddress -like “004501*”} | Set-VMNetworkAdapter -VirtualSubnetId 4501

# SmartIT VMs have a MAC address starting with 00-46-01
Get-VMNetworkAdapter | where {$_.MACAddress -like “004601*”} | Set-VMNetworkAdapter -VirtualSubnetId 4601

How to Add / Remove VM from Hyper-V

#Add vm to Hyper-V
New-vm -name AD2

#Remove vm from Hyper-V
Remove-vm -name AD2

How to create a new VM which will use a VHD disk from template; disk based on a Windows Server 2012 R2 vhd that has been fully patched and syspreped

New-VM -Name ‘ITMug04’ -VHDPath (New-VHD -Differencing -ParentPath ‘D:\Lib\template\VHD\W2K12.vhd’ -path ‘D:\Hyper-V\ITMug04\ITMug04_c.vhd’).path

# Let’s improve the script; the new script will connect the vm to the virtual switch ‘External virtual Network’

New-VM -Name ‘ITMug04’ -VHDPath (New-VHD -Differencing -ParentPath ‘D:\Lib\template\VHD\W2K12.vhd’ -path ‘D:\Hyper-V\ITMug04\ITMug04_c.vhd’).path -SwitchName ‘External virtual Network’

# Let’s improve the script; by adding 4 core processor

New-VM -Name ‘ITMug04’ -VHDPath (New-VHD -Differencing -ParentPath ‘D:\Lib\template\VHD\W2K12.vhd’ -path ‘D:\Hyper-V\ITMug04\ITMug04_c.vhd’).path -SwitchName ‘External virtual Network’ | Set-VM -ProcessorCount 4

# Let’s improve the script; by Enabling Dynamic Memory

New-VM -Name ‘ITMug04’ -VHDPath (New-VHD -Differencing -ParentPath ‘D:\Lib\template\VHD\W2K12.vhd’ -path ‘D:\Hyper-V\ITMug04\ITMug04_c.vhd’).path -SwitchName ‘External virtual Network’ | Set-VM -ProcessorCount 4 -DynamicMemory -MemoryMinimumBytes 512MB -MemoryMaximumBytes 1GB

# Now let’s power on the VM after creating it

New-VM -Name ‘ITMug04’ -VHDPath (New-VHD -Differencing -ParentPath ‘D:\Lib\template\VHD\W2K12.vhd’ -path ‘D:\Hyper-V\ITMug04\ITMug04_c.vhd’).path -SwitchName ‘External virtual Network’ | Set-VM -ProcessorCount 4 -DynamicMemory -MemoryMinimumBytes 512MB -MemoryMaximumBytes 1GB -Passthru | Start-VM -Passthru

One comment

Leave a comment