Today I needed a quick Windows batch script to check the percentage free space on drive E: and write an event to the event log if it was below 15%. I had just created a ChatGPT account and thought I would try it.
I said, “write a batch script to put a warning in event log when disk space on drive e falls below 15%”. It instantly typed out a script that looked great! And it added some informative comments about how the script worked. Getting the correct syntax for SET, FOR, and WMIC commands is half the battle in writing this kind of script. But when I tried it, it didn’t work. Oops, maybe I need to specify the operating system. I instructed, “Now write a a script with those parameters that works on Server 2012 R2.” it immediately spat this out:
@echo off setlocal enabledelayedexpansion rem Configure the threshold in percentage set threshold=15 rem Get the free space on Drive E for /f "skip=1 tokens=3" %%i in ('wmic logicaldisk where "DeviceID='E:'" get FreeSpace') do ( set "freeSpace=%%i" ) rem Get the total size of Drive E for /f "skip=1 tokens=3" %%i in ('wmic logicaldisk where "DeviceID='E:'" get Size') do ( set "totalSize=%%i" ) rem Calculate the percentage of free space set /a "percentage=freeSpace*100/totalSize" rem Compare the percentage with the threshold if !percentage! leq %threshold% ( rem Write a warning to the event log echo Warning: Disk space on Drive E is below %threshold%%%. echo eventcreate /T WARNING /ID 100 /L APPLICATION /D "Disk space on Drive E is below %threshold%%%." >> "%temp%\eventlog.bat" call "%temp%\eventlog.bat" ) endlocal
The comments: “In this version, I have made a small adjustment to accommodate the different output format of the wmic
command on Windows Server 2012 R2. The skip=1
option is used to skip the header line when retrieving the free space and total size of Drive E.” Sounds so promising!
Again, didn’t work.
Almost two hours later, I have a working script after fixing ChatGPT’s errors:
- WMIC outputs blank lines. To skip them, use /format:csv, delimit with comma, and get the second token of the only line that has two tokens.
- Batch files can only handle 32-bit math. Truncate the disk space size down to MB before getting the percentage (as suggested in this StackOverflow answer).
- In the percentage calculation, the variables on the right of the equal sign must be delimited by percent signs (or possibly exclamation points).
- The script uses echo with “>>” to append the EVENTCREATE command to a batch file, then calls that file. Because it is appending rather than overwriting, the eventlog.bat soon has multiple EVENTCREATE commands, so multiple events are written each time. Use “>” to overwrite the file.
In fact, there is no reason to create and call a batch file. Just run EVENTCREATE inside this batch file.
I decided to expand the event text to show how much space is free, and to write an Information event if it’s above the threshold. Here’s what I came up with:
@echo off setlocal enabledelayedexpansion rem Configure the threshold in percentage set threshold=15 rem Get the free space on Drive E for /f "delims=, skip=2 tokens=2" %%i in ('wmic logicaldisk where "DeviceID='E:'" get FreeSpace /format:csv') do ( set "freeSpace=%%i" ) set "freeSpaceMB=%freeSpace:~0,-6%" REM echo %freeSpaceMB% rem Get the total size of Drive E for /f "delims=, skip=2 tokens=2" %%i in ('wmic logicaldisk where "DeviceID='E:'" get Size /format:csv') do ( set "totalSize=%%i" ) set "totalSizeMB=%totalSize:~0,-6%" REM echo %totalSizeMB% rem Calculate the percentage of free space set /a "percentage=%freeSpaceMB%*100/%totalSizeMB%" REM echo %percentage% rem Compare the percentage with the threshold if %percentage% leq %threshold% ( eventcreate /T WARNING /ID 100 /L APPLICATION /D "Drive E has %percentage%%% free space, which is below the warning threshold of %threshold%%%." ) else ( eventcreate /T INFORMATION /ID 100 /L APPLICATION /D "Drive E has %percentage%%% free space, which is at or above the warning threshold of %threshold%%%." ) endlocal
Conclusion
ChatGPT is interesting, and its confidence in incorrect results is breathtaking, but for batch scripting, I think I might be better off to just look for human-written examples that are close to what I need.
Update June 17, 2023 – Better at PowerShell
I wondered if ChatGPT would do better at writing a PowerShell script to do the same thing, and in fact it does. (PowerShell is arguably a better option for this as it is not limited to 32-bit integer math.) ChatGPT’s only mistake was to try to write to the event log with Source = “Disk Space Monitor”, which doesn’t exist. When I reported this error to ChatGPT, it provided a workaround that uses the existing “Application” as a Source. This script works:
$threshold = 15 # Get the free space on Drive E $driveInfo = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='E:'" | Select-Object -ExpandProperty FreeSpace # Calculate the percentage of free space $percentage = $driveInfo / ($driveInfo + (Get-PSDrive -Name 'E').Used) * 100 # Compare the percentage with the threshold if ($percentage -le $threshold) { # Write a warning to the event log using the existing "Application" source $message = "Warning: Disk space on Drive E is below $threshold%." Write-EventLog -LogName Application -Source "Application" -EntryType Warning -EventId 100 -Message $message }
Also when asked, ChatGPT provided code for creating the custom Source. I did not test that.