profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Wednesday, February 18, 2015

Customizing Windows VMs as per your standards

Here is a powershell script which will help us to customize a windows virtual machine (or physical). Let us say you have many VMs which are to be customized locally or remotely using the powershell psremote options.
This will be particularly useful in scenarios where you will be deploying multiple VMs on a regular basis which will have same or similar settings.

#VM Internal Settings########################
#Rescan Disks                               #
#Format Disks if they are raw               #
#Extend Disks if there is unallocated space #
#Rename Disks                               #
#Enable Remote Desktop                      #
#Disable Firewall                           #
#Change Computer Description                #
#Script Type : PowerShell                   #
#BroughtToUby : MrAmbiG                  #
#############################################

#Computer's Description
$x.Description = "MyComputer"

#Rescan Disks
Write-Host "Rescanning Disks"
"rescan" | diskpart

#VM Disk Names
$vm_c = "System"
$vm_d = "Data"
$vm_e = "Applications"
$vm_f = "Documents"


#Initialize Disks if they are raw are not initialized
Write-Host "Finding Raw disks if any and then initialize, assign drive letter and format" -foregroundcolor Yellow
Get-Disk |
Where partitionstyle -eq 'raw' |
Initialize-Disk -PassThru |
New-Partition -AssignDriveLetter -UseMaximumSize |
Format-Volume -FileSystem NTFS -Confirm:$false

#Rename VM Disks
#Rename VM C Drive
$drivec = [wmi]"Win32_logicaldisk='C:'"
$drivec.VolumeName = $vm_c
$drivec.Put()
Write-Host "Renamed C Drive to $vm_c on VM" -foregroundcolor Green

#Rename VM D Drive
$drived = [wmi]"Win32_logicaldisk='D:'"
$drived.VolumeName = $vm_d
$drived.Put()
Write-Host "Renamed D Drive to $vm_d on VM" -foregroundcolor Green

#Rename VM E Drive
$drivee = [wmi]"Win32_logicaldisk='E:'"
$drivee.VolumeName = $vm_e
$drivee.Put()
Write-Host "Renamed E Drive to $vm_e on VM" -foregroundcolor Green

#Rename VM F Drive
$drivef = [wmi]"Win32_logicaldisk='F:'"
$drivef.VolumeName = $vm_f
$drivef.Put()
Write-Host "Renamed F Drive to $vm_f on VM" -foregroundcolor Green

#extend disks if they aren't
$MaxSize = (Get-PartitionSupportedSize -DriveLetter c).sizeMax
Resize-Partition -DriveLetter c -Size $MaxSize
$MaxSize = (Get-PartitionSupportedSize -DriveLetter D).sizeMax
Resize-Partition -DriveLetter D -Size $MaxSize
$MaxSize = (Get-PartitionSupportedSize -DriveLetter E).sizeMax
Resize-Partition -DriveLetter E -Size $MaxSize
$MaxSize = (Get-PartitionSupportedSize -DriveLetter F).sizeMax
Resize-Partition -DriveLetter F -Size $MaxSize

#Enable Remote Desktop and Adjust firewall for the same
Set-RemoteDesktopConfig -Enable -ConfigureFirewall
Write-Host "Enabled the Remote Desktop on VM and adjusted the firewall accordingly" -foregroundcolor

#Disable Firewall
Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False
Write-Host "Disabled the Firewall on VM" -foregroundcolor Green

#Change VM's Description
$x = Get-CimInstance Win32_OperatingSystem -Property Description
Set-CimInstance -CimInstance $x -PassThru
Write-Host "Change the Description of the VM" -foregroundcolor Green

#End of Script
#Need to add rescan & extend disk feature

No comments:

Post a Comment