65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Jul 29, 2015

CPOL

1 min read

viewsIcon

15875

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.

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

#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

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

Points of Interest

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

History

Initial version 7-29-2015