Click here to Skip to main content
15,867,453 members
Articles / Database Development / SQL Server / SQL Server 2008R2
Tip/Trick

Powershell script to monitor windows servers and populate it into a sharepoint list

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Jul 2015CPOL1 min read 15.4K   2  
This powershell script is you to monitor windows servers’ free hard disk and populate it to sharepoint custom list.

Introduction

I am sure monitoring windows servers in a regular interval is a painstaking and boring task for administrators. To minimize this tedious task, this script will be helping you to ease your life. In this script I used Microsoft.SharePoint.PowerShell and Windows Management Instrumentation (WMI) classes.

Background

As a SharePoint administrator I had to monitor 5 servers only within the sharepoint farm. In order to check free hard disk space either it is needed to logging to the each server or it is needed to have a 3rd party tool for monitor all the servers remotely. So that with the use of this script it is lot easier to monitor all the servers with single SharePoint list.

Using the code

Before using this code you must check following factors:

further, you have to create a sharepoint list as follows.

Image 1

Image 2

and in order to run this script after perticuler time period you can schedule it

Image 3

PowerShell
#Powershell script to server monitoring remotly
#written by Lalith Gallage 07/21/2015
$ServerName = Get-Content "C:\PS\Computers.txt"
$ConvertToGB = (1024 * 1024 * 1024)
#$BeginDate=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((get-date).AddDays(-1))
#$filter = "LogFile='application' OR LogFile='system' AND EventType=1 AND TimeWritten>'$BeginDate'"
$spWeb = Get-SPWeb http://sharepointsite/xxx/xxx/
$spList = $spWeb.Lists["listname"] 
foreach ($Server in $ServerName) {
    $newItem = $splist.Items.Add()
    $option = $Server -split " "
    $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $option[1] -Filter $option[2] | Select-Object Size,FreeSpace
    $option[1] + "," + [math]::Round($disk.Size / $ConvertToGB) + "," + [math]::Round($disk.FreeSpace / $ConvertToGB)
    if($option[3] -eq $null) {} else {
    $disk2 = Get-WmiObject Win32_LogicalDisk -ComputerName $option[1] -Filter $option[3] | Select-Object Size,FreeSpace
    $option[1] + "," +  [math]::Round($disk2.Size / $ConvertToGB) + "," + [math]::Round($disk2.FreeSpace / $ConvertToGB)
    $newItem["FreeSpace-Other"] = [math]::Round($disk2.FreeSpace / $ConvertToGB)
    }
$PSSnapin = Remove-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue |    Out-Null
$newItem["Title"] = $option[1]
$newItem["ServerName"] = $option[0]
$newItem["FreeSpace-OS"] = [math]::Round($disk.FreeSpace / $ConvertToGB)
$newItem.Update()
}

and text file should like following

PowerShell
machinename ip DeviceID='C:'
machinename ipaddress DeviceID='C:' DeviceID='E:'

Points of Interest

PowerShell
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $option[1] -Filter $option[2] | Select-Object Size,FreeSpace

History

Initial version 7-29-2015

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --