Home > SQL server DBA administration, SQL Server Job Management > Monitor free space from mount points by power shell script.

Monitor free space from mount points by power shell script.


  1. Install windows power shell 1.0 or 2.0 on the centralized server.
  2. By default the Set-ExecutionPolicy option is disabled
  3. Need to change as Set-ExecutionPolicy unrestricted
  • Run -> cmd-> type powershell -> Set-ExecutionPolicy unrestricted

ShellScript1 :

function main () {

GetServersFromFile

}

function GetServersFromFile () {

if ($verbose) {$VerbosePreference = “Continue”}

if ($debug) {$DebugPreference = “Continue”}

foreach ($svr in get-content “C:\1.txt”)

{

write-host $svr

GetDrivesInfoInsertScript $svr

}

}

#Function to get Drives Info Insert Script

function GetDrivesInfoInsertScript ($Server_Name) {

write-host “Processing Server $Server_Name ”

trap [Exception]

{

$Err= $((get-date).toString() + “TRAPPED: ” + $_.Exception.Message + “– > $Server_Name “)

write-error $Err

continue;

}

$Drives =Get-WmiObject win32_volume -ComputerName $Server_Name | where-object {$_.filesystem -match “ntfs”} | select name,Label,Capacity,FreeSpace

foreach ($drive in $Drives) {

$name = $drive.name

$Label = $drive.Label

$FreeMB = ($drive.FreeSpace)/1MB

$TotalSpaceMB = ($drive.Capacity)/1MB

$UsedSpaceMB = ($TotalSpaceMB – $FreeMB)

$PctFreeSpace = ( 100 * $FreeMB ) / $TotalSpaceMB

$PollDate =get-date

#write-host $Server_ID’,’$PollDate’,’$name’,’$Label’,’$TotalSpaceMB’,’$UsedSpaceMB’,’$FreeMB’, ‘$PctFreeSpace’

write-host $Server_ID $PollDate $name $Label $TotalSpaceMB $UsedSpaceMB $FreeMB $PctFreeSpace

}

}

Main

In the above script there is a path mentioned “C:\test\1.txt”) go to path(any path) then edit the text file and put the full server names on separate line save and exit.

 

Example

Server1.twcable.com

Server2.twcable.com

Server2.twcable.com

How to execute shell script.

Set the path

Run -> cmd -> type powershell -> run the below command

$env:path = $env:path + “;c:\foldername”

Run the below command

. c:\scripts\shellscript1.ps1

The output will be in the same window where you are running the script.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: