Archive for the 'PC stuff' Category

Scheduled automatic hard drive defragmentation for Windows Server 2003

Friday, March 16th, 2007

The problem

Suppose you are the administrator of one or more servers running Windows Server 2003. You probably have a schedule of maintenance tasks you perform on a periodic basis, such as checking system logs, installing software updates, maintaining security policies, cleaning up users’ files and defragmenting the hard drives.

For the last activity, you may have opted for a third party solution such as O&O Software’s Defrag for servers. I love and trust this solution — it’s a powerful and flexible tool specialized on defragmenting drives, making this recurring activity a breeze to manage.

But what if you’re in a corporate environment that, for several reasons, will stick to the Windows built-in defragmenter? Would you spend several hours a week attending to manually defragmenting drives and be happy with it? That’s silly.

Fortunately, unlike the Windows XP counterpart, the defragmenter application in Windows Server 2003 can be started from a batch script, so you can automatize the defragmentation activity. But things get a little more complicated if you have several partitions you want to defragment or if you want to perform more advanced tasks.

The needs

Here’s what I wanted:

  • Defragment all drives and partitions in the system sequentially (one after another).
  • Prevent multiple instances of the defragmenter from running in parallel.
  • Defragment only if there is sufficient free disk space for the defrag to work optimally (minimum 15% of disk space free).
  • Defragment only if the defragmenter recommends it, so it doesn’t run on drives with a 2% fragmentation.
  • Log the activity somewhere — I chose the system log for it, since I’m already checking it at least once a week, but this can be done easily in a text file.
  • Prepare for possible errors and log them.

I studied several scripts for automatic defragmentation on the web and built my own, to match my needs. Credit and thanks goes to everyone else who has attempted this in the past.

The script

' Unattended defragmentation script for Windows Server 2003
' Checks all hard drives and defragments them as necessary
' Create a BAT file with the following command:
'     CSCRIPT AutoDefrag.vbs //B
' Call the BAT file from Scheduled Task, run as Administrator or another user with rights for defragmenting disk
Const EVENT_SUCCESS = 0
Const EVENT_ERROR = 1
Const EVENT_WARNING = 2
Const EVENT_INFORMATION = 4
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")
' Check if another instance of Windows Defrag is already running
Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = ' Dfrgntfs.exe'")
If colProcesses.Count = 0 Then
	' No other instance found
	' Write information message in the system log
	objShell.LogEvent EVENT_INFORMATION, "Starting unattended disk defragmentation."
	' Find all hard drives
	Set colVolumes = objWMIService.ExecQuery("Select * from Win32_Volume where DriveType = 3")
	For Each objVolume in colVolumes
		' Analyze drive, check if it is recommended to defragment it
		errResult = objVolume.DefragAnalysis(blnRecommended, objReport)
		' Check if the operation completed without errors
		If errResult = 0 Then
			' Fragmentation check completed without errors
			' Check if there is sufficient free space on disk for defragmentation (minimum 15%)
			If (objReport.FreeSpacePercent < 15 ) Then
				' Drive doesn't have enough free space for an efficient defragmentation
				' Write error message in the system log
				objShell.LogEvent EVENT_WARNING, "Drive " & objVolume.Name & " is almost full and cannot be defragmented! Drive is " & objReport.FreeSpacePercent & "% free."
			Else
				' There is enough free space
				' Is it is recommended to defragment current drive?
				If (blnRecommended) Then
					' Yes, current drive should be defragmented
					' Write information message in the system log
					objShell.LogEvent EVENT_INFORMATION, "Drive " & objVolume.Name & " should be defragmented: " & objReport.TotalPercentFragmentation & "% total fragmentation, " & objReport.FilePercentFragmentation & "% file fragmentation. Drive is " & objReport.FreeSpacePercent & "% free."
					' Begin defragmentation
					errResult = objVolume.Defrag()
					' Check if defragmentation completed without errors
					If errResult = 0 Then
						' No errors, defragmentation is complete
						' Write information message in the system log
						objShell.LogEvent EVENT_SUCCESS, "Drive " & objVolume.Name & " successfully defragmented."
					Else
						' Error encountered
						' Write error message in the system log
						objShell.LogEvent EVENT_SUCCESS, "Drive " & objVolume.Name & " could not be defragmented. Error code: " & errResult & "."
						Err.Clear
					End If
				Else
					' No, current drive doesn't need to be defragmented
					' Write information message in the system log
					objShell.LogEvent EVENT_SUCCESS, "Drive " & objVolume.Name & " does not need to be defragmented: " & objReport.TotalPercentFragmentation & "% total fragmentation, " & objReport.FilePercentFragmentation & "% file fragmentation. Drive is " & objReport.FreeSpacePercent & "% free."
				End If
			End If
		Else
			' Fragmentation check generated an error
			' Write error message in the system log
			objShell.LogEvent EVENT_WARNING, "Drive " & objVolume.Name & " could not be analyzed. Error code: " & errResult & "."
		End If
	Next
	' Finished checking and defragmenting all hard drives in the system
	' Write information message in the system log
	objShell.LogEvent EVENT_INFORMATION, "Unattended disk defragmentation is complete."
Else
	' Another instance of Windows Defrag already running
	' Write error message in the system log
	objShell.LogEvent EVENT_ERROR, "Unattended disk defragmentation is unable to start: another instance of NTFS Defrag is already running."
End If

How to use this?

  1. Copy and paste this text in Notepad, save the text file as autodefrag.vbs in a folder on your Windows Server 2003 machine.
  2. In another Notepad window, enter the command: cscript autodefrag.vbs //B and save it as autodefrag.bat in the same folder with autodefrag.vbs .
  3. Create a new scheduled task that runs autodefrag.bat whenever you want. Make sure you run this under an account with administrative priviledges.

Tips

  • Schedule automatic defragmentation at a time when it doesn’t conflict with other disk-intensive activities, such as file transfers, database backups, file archival, tape backups, other maintenance.
  • Check logs to make sure the defragmentation ended without errors.
  • If a partition is almost full, free up some space and defragment it manually.
  • Check if partitions that were skipped by the automatic defragmenter really do not need to be defragmented.
  • For optimum performance, prepare your drives before automatic defragmentation commences — delete temporary files, archive logs and other files.
  • Augment this script to write details of its actions in a separate log file you can back up or send by e-mail, as needed. On request, I could prepare a version which also uses text files for logs.

If you find this script useful, I’d really like to hear your experiences with it.

Tinkering with capacitors

Sunday, October 15th, 2006

Yesterday I finally had the time to shop for replacement capacitors for the Dell desktop I bought for a file server, whose 5 capacitors were swollen.

Somehow electronic parts shops are all grouped together on the same street in this city, stretching for a couple hundred meters on both sides. I needed 5 capacitors of 2200 microfarads at 6.3 volts. Armed with patience and enjoying the warmth of the late autumn sun, I went through all the shops in a few hours. It’s pretty difficult to find capacitors for such a low voltage; some shops only carried from 10 or 16 volts up, others only from 50 up.

I started with largest electronic parts distributors. One didn’t have what I needed, another had the 10 volt kind - so I bought 10 of them for about $4. Cheap for a reason: their brand is “NA.” Not Applicable? Yikes. I’d better find alternate options.

Across the street, a smaller shop carried the kind I wanted - 6.3 volts. A bit more expensive, I bought only 5 of them for $4, but I wasn’t confident they were any better. They are dark blue with golden markings, made by “Kingcon.” Never heard of this one either, but they look a lot like those I’ve seen swelling up, and the brand sounds a lot like the 5 faulty capacitors in the Dell, “Nickicon.”

On a side note, what were Foxconn thinking when they picked “Nickicon” as a component supplier for manufacturing motherboards for Dell?!

Anyways, I went through the other stores, many being specialized in audio parts - speakers, amplifiers, specialty audio cables and connectors, and ultra-high performance electronic components for hi-fi audio devices. In such a small shop I found the kind of capacitors I needed, but this time a well-known, trusted brand: Jamicon. Clearly, these capacitors were nearly $1 a piece, but it’s money well spent.

Later in the day, I replaced the faulty capacitors on the motherboard with these Jamicon gems, powered up the desktop and… it started beeping at me. For a few seconds, I froze. Then I looked at the 4 diagnostic LEDs on the back of the Dell - two orange, two green. I looked up the pattern in the troubleshooting section of the manual and sighed with relief: “Bad memory.” I knew one of the memory modules has been acting weird, and this time I got proof: self-check identified two faulty locations. After taking the module out, everything worked perfectly.

Now I’ve got a new story to tell, a working file server, 15 capacitors I don’t need, and a deffective SDRAM memory module as a fancy keychain.

Bad capacitors, take three

Sunday, October 1st, 2006

It happened again.

The first time it was an accidental discovery. I was happy to think that I have fixed the problem.

The second time it was a surprising discovery, but I was convinced that the second blow will finally kill the monster.

But it didn’t. And a few days ago I discovered that another two of the original capacitors left on the motherboard look like a finger stung by a bee.

That got me mad. Really mad. I definitely ain’t gonna patch this motherboard for a third time. It’s time to stop trying to resurrect the dead and let the priest light a candle.

Darned capacitors.

I was planning to replace the core components of my PC with modern stuff — a new motherboard, an Intel Core 2 Duo processor, some DDR2 memory and probably a reasonable video card on PCI Express. That’d give me a lot of juice, less heat, less noise, and a bundle of features such as 10 USB2 ports on-board and Serial ATA 300. It looked like a big investment and I was waiting for prices to drop, using the old PC in the mean time. After all, I only get to spend maybe 6 to 8 hours per week at my home computer these days, so $1000 sitting in an unused computer would be a stupid idea.

But the old PC went belly-up, thanks to the effin’ faulty capacitors. And I needed an immediate solution. So I bought a cheap motherboard for Socket A (pretty much the only model I could still find in stock anyway), which will keep me running for some time. In need of space, I also bought a new 320 GB hard drive on PATA, so the pressure of SATA is gone for now.

Effin’ capacitors.

On a side note, yesterday I bought a second hand Dell OptiPlex G150, a nice Pentium III in a slim case, to become a flexible file server (and maybe a print server, and media center, and whatever else I want to throw at it — like, running a couple more virtual machines on it with Microsoft Virtual PC). Its inner workings are wonderful, the case is so well thought and the airflow keeps the system cool with one fan for video card, hard drive and power supply. Everything inside is made by Foxconn and labeled as such. The motherboard is a little smaller than mATX form factor, and has only 5 capacitors on it.

They’re swollen.

*sigh*

Now, does anyone want to buy an old Epox 8K3A motherboard (Socket A, VIA KT333 & 8233 chipset, 266 MHz maximum processor FSB and 333 MHz maximum memory frequency) which has gone through surgery twice and still needs (at least) a third intervention?

Love and hate for Dell

Tuesday, August 15th, 2006

I’ve got a mixed love/hate relationship with Dell.

I love them for having great ideas. Their computers are not designed for everyone; they’re specialized tools that perform very well in specific tasks and areas. The hardware is well thought and assembled — exactly what you need to do something. Custom designed motherboards embrace the BTX form factor, to improve air cooling, minimize noise and size. If you open some Dell desktops or servers, you will simply be amazed on how every little piece of plastic has its well-thought role, how detailed the design is and how much more you are getting in the box, compared to a generic brand PC. The servers particularily stand out, just go through the service manual of a Dell PowerEdge 2950 and be amazed. And, like most brand name systems, these are tested for incompatibilities and should give you fewer problems than self-built systems.

But I also hate Dell for screwing up more often than you’d like. The reason anyone goes with a brand PC is the set of extra services included in the price. Dell’s services have been slipping for years. Tech support is in India, they simply cannot do anything to help you out other than recording your complaint; it’s even more aggravating when you have to ask the dude to repeat what he’s saying 5 times, because you can’t understand his English. Order processing has its issues as well; as a company that sells directly to the consumer, they need to make sure the orders are processed in due time. Delays in shipment, incorrect addresses, lost packages and wrong configurations are a few things they need to control, as well as sending refunds for cancelled orders.

And finally, there’s the hardware. When it runs, it’s awesome. When it fails, it’s a nightmare. You don’t know for sure what’s wrong with it. They don’t know either. There’s no easy fix, and you end up spending weeks asking yourself “Why?” Just like my three Dell Latitude laptops: why on Earth have they decided to go with SDRAM instead of DDRAM? What a horrible bottleneck in performance, also seen in many of their Pentium 4 desktops! Why have they decided to go with Hitachi hard drives, which nearly self-destruct on a periodic basis? Why are there hardware issues with power management when I install Western Digital replacement hard drives? And why are they still listing ancient driver versions on their support site, when component manufacturers (Intel chipsets or 3com network controllers, for instance) have released newer, improved drivers?

I’ll probably buy a new computer in a few months. What will it be — a Dell, or a bag of components I’ll assemble myself? I’m oscillating between these. If I get the Dell, I’ll probably be very happy with what it delivers without needing to open the box at all. However, it won’t be too flexible in how I can modify and upgrade it. And if something will go wrong, I’ll be cursing my decision. On the other hand, with a custom built computer, I’ll probably spend a decent amount of time with my hands in it, tweaking and fiddling with stuff — lots of fun, but also annoying at one point, and definitely not as impressive as the brand name hardware and construction.

Stay tuned for the next episode of this amazing soap opera.

Anything can go wrong

Saturday, August 12th, 2006

If something can go wrong, it will go wrong. Even if there’s nothing that can go wrong, it still will go wrong.

Situation: rack-mounted server with all the goodies on it, Windows 2000 Server running SQL Server, IIS and a bunch of other stuff, including Symantec Antivirus. External HP tape drive to be attached. No-brainer to-do list: set correct SCSI ID, connect the cable and restart the system so the SCSI controller finds the new device. Windows will detect the drive, then the drivers will be updated with most recent ones.

No biggie, you’d think. Pretty much the same complexity/risk level with connecting a USB printer, 30 minutes of downtime. Tops. I have not seen, heard of, or even imagined something this trivial can go so horribly wrong.

On reboot, server is as slow as an old, feeble turtle which suffered some rare illness as a baby-turtle. The tape drive is recognized, but when trying to access the device properties to update the driver — everything freezes. Nearly anything I try to do in Management console results in MMC freezing. Disk management doesn’t work at all. Worst of all, the SQL server won’t start — and time is ticking, people need to use applications relying on this data, business isn’t running, you can almost see money flying out of the pocket. To top it off, I don’t have physical access to the server by myself, only others do and they all went home to enjoy the Friday afternoon. No “Safe mode” for me, no unplugging the tape drive and hope things will turn back to normal. What to do?

First thought, the universal Windows fix: restart it. Servers aren’t exactly snappy at getting back online, in fact their POST diagnostics can take more time to complete than the operating system to load. So 20 minutes later, I’m staring at the same situation.

Second thought, let’s see what is not running properly. Checking services, I notice that a few of them are trying to start but get stuck at “Starting” — stop action is disabled at this point. OK, let’s disable these services and restart. Nope, can’t disable them, management console freezes when applying changes. Hmmmm.

Third thought: this situation occured as a consequence of attaching the tape drive. Being unable to physically detach the cable, I can only disable the device. No workie. Uninstall the device, to remove the driver from memory. Nada.

At this point, phone calls are building up and one of the people with access to the server has to return to the office. I know he’s pissed off — I would if I were him. Goodbye weekend plans, when worst case scenario is spending the next 24 hours reinstalling the operating system and restoring all settings to the way they were before. We detach the tape drive, but the server doesn’t seem to care and continues to be as slow as a dead fish thrown down the toilet. We test the SCSI controller, diagnose drives, try anything we can think of; nothing wrong. Must be software, then: we go through the system and manually delete all references to the tape drive, even in the registry. Same outcome. Finally, the server is started in Safe mode from the console, all supplemental services are disabled, server restarts and… whew, we have a stable system.

Sort of.

We enable critical services, SQL server starts in less than 10 seconds, things are back on the floating line, except for a few management services including the Symantec Antivirus. Its event log contains a reference to some 40+ settings being changed, and it just won’t run properly for some reason. Maybe it has detected the tape drive as a Removable Storage and attempted to scan it (with no tape in drive)? Maybe it was just Windows automatically creating scheduled backups. I don’t know. And frankly, at 1 in the morning on a Friday night, I don’t effin’ care. It’s been too long since the last time I tried to find logical explanations for Windows’ oddities.

Lesson learned? Never underestimate the possibility that things can — and will — go wrong when you least expect them to, and in a way that you couldn’t predict. What started as a 10-minute connect–reboot–verify-that-everything-works-before-leaving-for-home kind of after-hours job, turned into a 5 hour ordeal propagating into tens of hours lost by other people waiting for this system and probably thousands of dollars of losses from delays. And a pissed off boss.

Effin’ tape drive and older Windows systems. Effin’ physical access restrictions. And effin’ single point of failure in case this server goes down. Which is why I added the tape drive, to have a full system and data backup ready for emergencies. Which is exactly what caused the emergency.

In a few minutes I’m going to fall asleep thinking “what have I done wrong?” That ain’t good.