Friday, November 22, 2013

Delete VMs in Windows Server 2012 and Windows Server 2012 R2 Hyper-V by a PowerShell script

In Windows Server 2012 and Windows Server 2012 R2, we can remove a VM by Hyper-V Manager or PowerShell, "Remove-VM". However, it just deleted the VM and a configuration file of that VM. It hasn't deleted the related virtual hard disk files so we need to delete the virtual hard disk files by ourself. I intended to create a PowerShell script to remove VMs and related virtual hard disk files.

Disclaimer: Make sure you do understand the script and test it in lab environment. You use this script at your own risk. If you don't understand what this script does, please don't use it in any environment. 
 
Function Delete-VM {

<#
.Synopsis
Delete virtual machines with related virtual hard disk files in a Hyper-V server.

.DESCRIPTION
This function is intended to delete virtual machines with all related virtual hard disk files in a Hyper-V Server which is installed Windows Server 2012 and Windows Server 2012 R2.

If a virtual machine is not in "Off" state , this script shows a pop-up window for Administrator deciding shut down the VM or not. After that, the script continue deleting the VM.

There is a "Force" option to shut down and delete virtual machines without confirmation for this script.

Before you using this function, it requires the Hyper-V Module for Windows PowerShell which is installed in your computer.

.Notes
Name        : Delete-VM.ps1   
Last Updated: 22-Nov-2013
Version     : 1.0
Requires    : PowerShell version 3.0 or above with Hyper-V Module for Windows PowerShell

.Link
http://terrytlslau.tls1.cc/2013/11/delete-vms-in-windows-server-2012-and.html

.Parameter Name
The virtual machine name in a Hyper-V Server.

.Parameter ComputerName
The computer name of a Hyper-V Server.

.Parameter Force
Shut down and delete virtual machines with related virtual hard disk files without confirmation window. 

.EXAMPLE
Delete-VM -Name server01

Delete a VM named server01 in current Hyper-V host.

.EXAMPLE
Delete-VM -Name server02 -ComputerName HV02

Delete a VM named server02 in the Hyper-V host, HV02.

.EXAMPLE
Delete-VM -Name server03 -Force

Shut down and delete a VM named server03 with related virtual hard disk files without any confirmation.

.EXAMPLE
Get-VM -Name Server04 | Delete-VM

Get-VM and then pass the value from pipeline to Delete-VM.
#>

[CmdletBinding(ConfirmImpact='High')]

Param (
[Parameter(Position=0,Mandatory=$True,ValueFromPipelineByPropertyName=$True,HelpMessage="Enter the VM name")]
[String[]]$Name,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[String[]]$ComputerName = $env:COMPUTERNAME,
[Switch]$Force
)

Process {

ForEach ($Comp in $ComputerName) {

ForEach ($VMName in $Name) {

Try {
$VMlist_OK = $True
$VMlist = Get-VM -Name $VMName -ComputerName $Comp -ErrorAction Stop
}

Catch {
$VMlist_OK = $False
Write-Warning "The VM $VMName in the server $Comp cannot be found" 
}

If ($VMlist_OK) {
$VMHostPath = $((Get-VMHost -ComputerName $Comp).VirtualMachinePath)
$VMState = $((Get-VM -Name $VMName -ComputerName $Comp).state)
$VMPath = $((Get-VM -Name $VMName -ComputerName $Comp).path)
$VHD = $((Get-VMHardDiskDrive -VMName $VMName -ComputerName $Comp).path)

if ($Force) {

Try {

if (($VMState -eq 'Running') -or ($VMState -eq 'Paused')) {
Stop-VM -Name $VMName -ComputerName $Comp -Force                                    
Remove-VM $VMName -ComputerName $Comp -Force                                    
Invoke-command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VHD -Force}
Remove-Variable VHD

if ($VMHostPath -ne $VMPath) {                                                                    
Invoke-command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VMPath -Recurse -Force}
Remove-Variable VMPath
}

Write-Output "The VM $VMName in the server $Comp was deleted completely!!!"
} 

else {
Remove-VM $VMName -ComputerName $Comp -Force                                    
Invoke-command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VHD -Force}
Remove-Variable VHD                                    

if ($VMHostPath -ne $VMPath) {                                                                    
Invoke-command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VMPath -Recurse -Force}
Remove-Variable VMPath
}                                

Write-Output "The VM $VMName in the server $Comp was deleted completely!!!"
} 
} 

Catch {
Write-Warning "Force deleted $VMName error"
}

} 

else {

if (($VMState -eq 'Running') -or ($VMState -eq 'Paused')) {
Stop-VM -Name $VMName -ComputerName $Comp

if ((Get-VM -Name $VMName -ComputerName $Comp).uptime -gt "00:00:00") {
Write-Warning "The VM $VMName in $Comp hasn't been stopped!!! Cancel to delete VM $VMName"
Continue
}

else {
Write-Verbose "The VM $VMName in $Comp has been stopped!"
Remove-VM -Name $VMName -ComputerName $Comp

Try {
Get-VM -Name $VMName -ComputerName $Comp -ErrorAction Stop | Select -First 0
Write-Warning "The VM $VMName in the server $Comp hasn't been deleted!!! Cancel to delete VM $VMName" 
Continue
}

Catch {
Invoke-Command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VHD}
Remove-Variable VHD

if ($VMHostPath -ne $VMPath) {                                            
Invoke-Command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VMPath -Recurse}
Remove-Variable VMPath
}

Write-Output "The VM $VMName in $Comp was deleted completely!!!"
}
                                       
} 

}

else {
Write-Verbose "The VM $VMName in $Comp has been stopped!"
Remove-VM -Name $VMName -ComputerName $Comp

Try {                                    
Get-VM -Name $VMName -ComputerName $Comp -ErrorAction Stop | Select -First 0
Write-Warning "The VM $VMName in $Comp hasn't been deleted!!! Cancel to delete $VMName"
Continue
}

Catch {                                
Invoke-Command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VHD}
Remove-Variable VHD

if ($VMHostPath -ne $VMPath) {                                            
Invoke-Command -ComputerName $Comp -ScriptBlock {Remove-Item -Path $Using:VMPath -Recurse}
Remove-Variable VMPath
}

Write-Output "The VM $VMName in $Comp was deleted completely!!!"
}
                                
} 

}                  

} 

} 

} 

} # End Process

} # End function

This posting is provided “ AS IS” with no warranties, and confers no rights!

Tuesday, November 19, 2013

Disable Windows Firewall in Windows Server 2012 or Windows Server 2012 R2 by PowerShell

In Windows Server 2012 or Windows Server 2012 R2, we can disable Windows Firewall by PowerShell. By default, all profiles of Windows Firewall are "Enabled".


To disable Windows Firewall in one of the profiles, we can perform "Set-NetFirewallProfile -Enabled false - Name Public -PassThru" to disable firewall in "Public" profile.


To disable Windows Firewall in all profiles, we can perform "Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled false -PassThru".


For more information:


This posting is provided “AS IS” with no warranties, and confers no rights!

Friday, November 15, 2013

Monday, November 11, 2013

Server Virtualization w/ Windows Server Hyper-V & System Center Jump Start on the 19 & 20 of November 2013

If you are interested in what's new in Microsoft virtualization, join the live event, Server Virtualization w/ Windows Server Hyper-V & System Center Jump start on the 19 and 20 of November 2013. This event is presented by "Symon Perriman" and "Corey Hynes" and it's free. You will learn about the new virtualization features of Windows Server 2012 R2 and System Center 2012 R2. Plus event attendees will get a free voucher for the exam 74-409.

Go to the following web site to register it.

This posting is provided “AS IS” with no warranties, and confers no rights!

Sunday, November 10, 2013

Installing Hyper-V Server 2012 R2

Microsoft Hyper-V Server is a standalone product released by Microsoft. You don't need to install any Windows Server version before installing it. It's no need any license and free to use it. The latest version of this is Microsoft Hyper-V Server 2012 R2. It supports the following features like BitLocker, Failover Cluster, Windows PowerShell and so on.


The Hyper-V server can be joined to the domain and can be managed by Server Manager.

It can be downloaded from here.

The hardware requirements of Hyper-V servers are:

Hyper-V requires a 64-bit processor that includes the following:

  • Hardware-assisted virtualization. This is available in processors that include a virtualization option - specifically processors with Intel Virtualization Technology (Intel VT) or AMD Virtualization (AMD-V) technology.
  • Hardware-enforced Data Execution Prevention (DEP) must be available and enabled. Specifically, you must enable Intel XD bit (execute disable bit) or AMD NX bit (no execute bit)

Reference:

To install Hyper-V Server 2012 R2, the steps are quite straightforward.

1. Boot-up and Insert the DVD of Hyper-V Server 2012 R2 into a physical server.
2. On "Install Microsoft Hyper-V Server" window, click "Next".


3. Click "Install now".


4. On "License terms" window, check "I accept the license terms".


5. Click "Next".
6. On "Which type of installation do you want" window, click "Custom: Install the newer version of Hyper-V Server only (advanced)".


7. On "where do you want to install Hyper-V Server" window, select the hard disk which you want to install the OS of Hyper-V Server 2012 R2 and then click "Next".


Now, the Hyper-V server are installing in the physical server.


 Then, the server will restart automatically.


8. When installation finished, you can enter the local administrator password of the Hyper-V server.




9. Now, we can change the computer name, IP addresses, join domain and so on in "sconfig".


As a result, the Hyper-V server was installed successfully.

Remark: If all the command prompts are closed, you can do the following tasks.

1. You can press "Ctrl" + "Alt" + "Delete" and then select "Task Manager". 


Click "More details".


3. On the menu of "Task Manager", click "File > Run new task".


4. On "Create new task" window, enter "cmd" and then click "OK".


Now, you can use the command prompt and then you can also enter "sconfig" to call the menu to configure your Hyper-V Server.



For more information:


This posting is provided “AS IS” with no warranties, and confers no rights!

Thursday, November 7, 2013

How to learn PowerShell from beginning

To learn PowerShell from beginning, I suggest watching "Getting Started with PowerShell 3.0 Jump Start". This course is offered by Microsoft Virtual Academy (MVA) and it's free. There are 9 modules in "Getting Started with PowerShell 3.0 Jump Start" and it is  instructed by Jason Helmick and Jeffrey Snover. Jason Helmick and Jeffrey Snover are the experts and have a lot of experience in PowerShell. They will show you a lot of sample to understand PowerShell easily. If you are the system administrator or system engineer of Microsoft, don't miss it and watch it.

This posting is provided “AS IS” with no warranties, and confers no rights!