Search This Blog

Friday, November 13, 2009

Check disk space in C#

Sample 1: Using System.Diagnostics.PerformanceCounter
//Get disk space info from remote server
System.Diagnostics.PerformanceCounter _pc;
float _freemegabytes;
float _freespacepercentage;
float _capacity = 0;
string result = "";
//Get free space percentage
_pc = new System.Diagnostics.PerformanceCounter("LogicalDisk", "% Free Space", serverdrive, servername);
_freespacepercentage = _pc.NextValue();
//Get free space in megabytes
_pc = new System.Diagnostics.PerformanceCounter("LogicalDisk", "Free Megabytes", serverdrive, servername);
_freemegabytes = _pc.NextValue();
//Calculate the capacity in gigabytes
_capacity = ((_freemegabytes / _freespacepercentage) * 100) / 1024;
//Calculate free space in gigabytes
_freemegabytes = _freemegabytes / 1024;
result = "" + servername + " " + serverdrive + " TotalS:" + _capacity.ToString("##.00") + "gb FreeS:" + _freemegabytes.ToString("##.00") + "gb. ";
 
Sample 2: Using System.IO.DriveInfo
//Get disk space info from local server
System.IO.DriveInfo dinfo = new DriveInfo("x:");
//Get disk size
double dsize = double.Parse(dinfo.TotalSize.ToString()) / 1073741824;
//Get free space
double dspace = double.Parse(dinfo.TotalFreeSpace.ToString()) / 1073741824;
result = "Logical Disk Size = " + dsize.ToString("##.00") + " GB";
result += " Logical Disk FreeSpace = " + dspace.ToString("##.00") + " GB";

Wednesday, November 11, 2009

SysPrep base Virtual Machine to create multiple images

Overview
Creating new virtual machines is bit time consuming so to create a base virtual hard drive and just make copies of it and start-up new virtual machine is ideal, but cloning/coping vhd will lead to having virtual servers with same SID and CID on your network. There are tools like NewSID to fix it but I had problem with clone/ghost virtual machine which didn't want to be joined to a domain.


What is SysPrep?
SysPrep is a tool that allows you to prepare or “prep” a machine with the operating system along with any software you wish was pre-installed and pre-configured. Once a machine is SysPrep’d, you have a new virtual hard drive that has the Windows operating system along with any additional software or features you want, such as IIS, preinstalled and pre-configured. SysPrep allows you to create your perfect system configuration packaged so that you can have a new virtual machine up and running in just minutes. And, it is available for both Windows Server 2003 32bit/64bit and Windows XP.


Where to get SysPrep?
System Preparation tool for Windows Server 2003 Service Pack 2 32bit Deployment
System Preparation tool for Windows Server 2003 Service Pack 2 64bit Deployment


How to SysPrep?
Creating the base virtual machine image/vhd
  1. Create new virtual machine, I did it on Hyper-V but should work for VPC 2007/Virtual Server and more.
  2. Install your OS. Windows Server 2003 R2 (latest service pack) or Windows XP.
  3. Do NOT join the virtual machine to any domain.
  4. Leave the administrator password blank or reset it to blank.
  5. Now get all latest windows updates and all.
  6. Install antivirus software and latest updates.
  7. Install the virtual additions, depending which virtualization you're using.
  8. Install all and latest .Net frameworks.
  9. Activate the OS license. Then you don't need to re-activate the OS for each new virtual machine you create.
  10. When I create a base for virtual servers (MOSS, K2, SQL or WEB servers) I install BGInfo, part of Windows sysinternals package, get it here. It creates cool desktop background with various server information as a desktop background.
Create SysPrep.inf File

Before you can SysPrep you virtual machine, you need to create a SysPrep.inf configuration file. This file contains the information about your machine. It will also prevent you from having to enter you CD Key each time you create a new virtual machine from you SysPrep’d image. Below is a sample of the SysPrep.inf file that you need to create. This file configures the SysPrep process and automates boot up process.
  1. On your virtual machine, create a folder SysPrep at the root of your C: drive (C:\SysPrep).
  2. Copy the following text into a text file named SysPrep.inf.
  3. Enter the correct values for the following keys:
    1. TimeZone – the value of 140 is Harare, Pretoria (UCT +02:00). You may want to change this to your local time zone, but it is not required to do so, Index numbers for [GuiUnattended]/TimeZone.
    2. OEMDuplicatorsting – this should contain the name of the operating system you have installed on your virtual machine.
    3. FullName – your name, the name you would enter if you were installing Windows.
    4. OrgName – the name of your company, or blank.
    5. ProductKey – Your product key (CD key) license.
Sample SysPrep.inf file:

;SetupMgrTag
[GuiUnattended]
TimeZone=140
OEMSkipRegional=1
OemSkipWelcome=1
EncryptedAdminPassword=NO
OEMDuplicatorstring="Windows Server 2003 R2 64Bit"
[Identification]
JoinWorkgroup=WORKGROUP
[Networking]
InstallDefaultComponents=Yes
[LicenseFilePrintData]
AutoMode=PerServer
AutoUsers=50
[Unattended]
OemSkipEula=Yes
InstallFilesPath=C:\sysprep\i386
[UserData]
FullName="YOUR NAME HERE"
OrgName="YOUR COMPANY NAME HERE"
ProductKey=YOUR-PRODUCT-KEY-HERE
[SetupMgr]
DistFolder=C:\sysprep\i386
DistShare=windist

Your SysPrep.inf configuration file is now ready to be used.

SysPrep-ing your Virtual Machine

SysPrep-ing your virtual machine takes just a minute or two. Most of the time is simply shutting down your virtual machine. Important: do not start this virtual machine back up or it will un-SysPrep your machine. If this does happen, you can simply go through these steps below to SysPrep you virtual machine again.
  1. Run the SysPrep install tool, it will install a deploy.cab file in this location, C:\WINDOWS\system32\deploy.cab.
  2. Extract all the files in the deploy.cab to C:\SysPrep.
  3. Then run SysPrep.exe
  4. Check the “Don't reset grace period for activation” option.
  5. Make sure Shutdown mode is Shut down.
  6. Click the Reseal button to shutdown and package.
  7. Click OK to generate new SID's.
  8. Your virtual machine will now shut down and be SysPrep’d.
  9. You now have a virtual image that is SysPrep’d, but not ready to be used.
  10. Before you use this image, you will need to make a backup copy of your virtual machine image. This will allow you to always have a SysPrep’d virtual machine ready and waiting.
  11. Backup your SysPrep’d virtual machine image (.vhd), and rename them to something you can easily understand and that describes what your image contains. For example:
    Base2003R2x64_SysPrep.vhd
  12. You now have your virtual machine SysPrep’d. You can now use this image to quickly create a new virtual machine in minutes, with a new machine name and new unique System ID (SID) each time you use it.