It is always recommended to keep tracking of the disk space utilization for you servers. This will help us to get the idea of our future requirement. There are multiple tools available for track this but you have to pay for most of them. Recently I also had similar kind of requirement and I was asked to provide the free solution for this.
There are some free tools available but all of them having some short of limitation, and also for security reason, it is not recommended to install any free tools on the server. So, I had to look for some alternatives, and I thought why we dont use the PowerShell to get the disk space utilization details daily and kept it in MS Access database using Microsoft Access Database Engine 2010, which is available for free.
Requirements:
- PowerShell 3.0 +
- Microsoft Access Database Engine 2010
- Local Admin privilege to target windows systems.
How to Setup:
First you have to download and install MS Access Database Engine. Once the installation complete, you have to create a blank database with a table name DiskUtilization which will have following field.
If you do not have MS Access installed, then you could also download and use the following blank database for Disk Utilization.
DiskUtilization (560.0 KiB, 1,232 hits)
Place the database file to D: or anywhere you like.
Now you have to create a System DSN using ODBC by selecting Microsoft Access Driver (*.mdb, *.accdb)
Now select the database from D:DiskUtilization.accdb
Once everything is set you have to schedule the following PowerShell script to execute on daily basis. You also have to put all the Target Server Names or IP Addresses to the file called Servers.txt (One in each line) and update the first line of the script with the location of that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | $Servers = Get-Content D:Servers.txt $adOpenStatic = 3 $adLockOptimistic = 3 $objConnection = New-Object -com "ADODB.Connection" $objRecordSet = New-Object -com "ADODB.Recordset" $Date=(Get-Date).ToString("dd/MM/yyyy") foreach ($Server in $Servers) { $LocalDisks = "" $GetDisk = Get-WmiObject -ComputerName $Server Win32_LogicalDisk -AsJob $Complete = Get-date While (Get-Job -State Running){ If ($(New-TimeSpan $Complete $(Get-Date)).TotalSeconds -ge 20) { Get-Job | Remove-Job -Force } Start-Sleep -Seconds 3 } $LDisk = $GetDisk | Receive-Job $LocalDisks = $LDisk | ? {$_.DriveType -eq '3'} foreach ($LocalDisk in $LocalDisks) { $Drive = $LocalDisk.DeviceID $VolumeName = $LocalDisk.VolumeName $TotalSpace = [math]::Round(($LocalDisk.Size)/1048576) $PartitionType = $LocalDisk.FileSystem $FreeSpace = [math]::Round(($LocalDisk.FreeSpace)/1048576) $objConnection.Open("DSN=DiskUtilization") $objRecordset.Open("Select * from DiskUtilization", $objConnection,$adOpenStatic,$adLockOptimistic) $objRecordSet.AddNew() $objRecordSet.Fields.Item("Server").Value = "$Server" $objRecordSet.Fields.Item("DriveLetter").Value = "$Drive" $objRecordSet.Fields.Item("PartitionFormat").Value = "$PartitionType" $objRecordSet.Fields.Item("TotaSpace (MB)").Value = "$TotalSpace" $objRecordSet.Fields.Item("FreeSpace (MB)").Value = "$FreeSpace" $objRecordSet.Fields.Item("ReportingDate").Value = "$Date" $objRecordSet.Update() $objRecordSet.Close() $objConnection.Close() } } |
Once the script execute, it will start collecting the data in following format.
Tips:
Smiler to DiskUtilization you can easily create MemoryUtilization or CPUUtilization by using following WMI Classes
For CPU > Win32_PerfFormattedData_PerfOS_Processor
For Memory > Win32_OperatingSystem
How to Use this data to Represent:
You can easily import this data to excel and create trend analysis using pivot table and graph.
Please let me know whether you like it or not.
Update : There is a limitation in Get-WmiObject, if the WMI is broken in target system, this script will get stuck, to avoid this I have updated the script to use Get-WmiObject with Job loop, which will terminate the Job after 20 seconds.
Disclaimer: All posts and opinions on this site are provided AS IS with no warranties. These are our own personal opinions and do not represent our employer’s view in any way.
This article currently have 6,667 views
This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License.
Can you please share the same script get the avg memory and CPU utilization?
Can you please share the avg memory and CPU utilization script?
I am not sure what are the processes are used by AVG, I don’t have AVG installed on my laptop nor on my other systems. You have to figure it out your own.
Regards,
Saugata D.