Checking hard drive space gets old, and if you want historical data it was a manual processes, so i created this script i run once a day to update a SQL database, and connect to it from excel to give me a historical data usage on our shared drives.
Enjoy.
$DasMDrive = Get-WmiObject Win32_logicaldisk -computerName "servername" -filter "DeviceID = 'M:'" | ForEach-Object {$_.freespace / 1GB}
$DasUDrive = Get-WmiObject Win32_logicaldisk -computerName "servername" -filter "DeviceID = 'U:'" | ForEach-Object {$_.freespace / 1GB}
$DasVDrive = Get-WmiObject Win32_logicaldisk -computerName "servername" -filter "DeviceID = 'V:'" | ForEach-Object {$_.freespace / 1GB}
$DasWDrive = Get-WmiObject Win32_logicaldisk -computerName "servername" -filter "DeviceID = 'W:'" | ForEach-Object {$_.freespace / 1GB}
$DasXDrive = Get-WmiObject Win32_logicaldisk -computerName "servername" -filter "DeviceID = 'X:'" | ForEach-Object {$_.freespace / 1GB}
$DasYDrive = Get-WmiObject Win32_logicaldisk -computerName "servername" -filter "DeviceID = 'Y:'" | ForEach-Object {$_.freespace / 1GB}
$DasZDrive = Get-WmiObject Win32_logicaldisk -computerName "servername" -filter "DeviceID = 'Z:'" | ForEach-Object {$_.freespace / 1GB}
$DasToday = get-date -format MM/dd/yyyy
#change "ServerName" to your server
#Change "DISKUsage" to your database
#Change DiskStorage to your table name
$SQLServer = "SERVERNAME" #use Server\Instance for named SQL instances
$SQLDBName = "DiskUsage"
$SqlQuery = "insert into DiskStorage ([Date],[M_Drive],[U_Drive],[V_Drive],[W_Drive],[X_Drive],[Y_Drive],[Z_Drive]) Values ('$DasToday','$DasMDrive','$DasUDrive','$DasVDrive','$DasWDrive','$DasXDrive','$DasYDrive','$DaszDrive')"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
Comments
Post a Comment