Knowledge base

Asset 32

Powershell commands to automate basic server tasks

Posted on 13 December 2017 by Beaming Support

Powershell is a very powerful tool within the Windows operating system. It can be used to accomplish many tasks, some of which are not able to be completed via a UI menu. Server administrators are usually required to run monthly, weekly or daily tasks on servers to keep them working in a healthy manner. The below can help with some of these tasks, by automating the process and emailing you the results.In this example we show you how to create a Powershell script to email you an event log and RAID status.

This will set the variable $dat to the first of the current month (01/01/2000).

$mo = (Get-Date).Month

$yr = (Get-Date).Year

$dat= “01/” + $mo + “/” + $yr

 Then we set the variable that will contain our C drive free and used
space.
 

$drives = Get-PSDrive C | Select-Object Name, @{Name=”UsedGB“;Expression={[math]::round($_.used/1gb,2)}},
@{Name=”FreeGB“;Expression={[math]::round($_.free/1gb,2)}}| Out-String -width 30

 Next we can get the event and application logs to add into a text file.

 This creates a file called Syslog1.txt. 

Out-File C:ScriptSysLog$month.txt

 These lines will add a title and formatting, as well as add all System events since the start of the month to the text file named SysLog1.txt.

You can use the same script for application logs, just changing the variable as below.

Get-EventLogLogname Application then changing the document name to $AppLog$month.txt. 

Add-Content
C:ScriptSysLog$month.txt
“==============================================”

Add-Content
C:Script$yrSysLog$month.txt “Event Log : System”

Add-Content C:ScriptSysLog$month.txt
“==============================================”

Get-EventLog
Logname System –EntryType Error, Warning -After $dat | select Timegenerated, EntryType, Source, Message
| Format-List | Out-File -Append C:ScriptSysLog$month.txt

 The following are variables that count the amount of Event entries, for adding to our email.

$SysCount = (Get-EventLogLogname System –EntryType Error, Warning -After $dat).count

$AppCount = (Get-EventLogLogname Application –EntryType Error, Warning -After $dat).count

 If you have a Dell server with Server
admin installed, the following will give you RAID status

$SvrAdm = omreport storage pdisk controller=0 | Select-String “Failure Predicted”, “Name” | Out-String

 The last step is to build the body of your email and send it to yourself. Each `n is a new line. 

$body = “Server Checks Attached `n`n——————————————`n`nDrives : `n” + $drives + “—————————————-`nEvent Log Counters: `n `nSystem Events this month : ” + $SysCount + “`nApplication
Events this month : ” + $AppCount + “`n——————————————`n `nHard Drive Status: `n” + $SvrAdm

 $syslog = “C:ScriptSysLog$month.txt”

$applog = “C:ScriptAppLog$month.txt”

Send-MailMessage -to “Name@Domain.com” -from “Server@Domain.com” -Subject “Monthly Check – Server” -body $body –SmtpServer TheSMTP.com -Attachments $syslog, $applog

 

Now that you know how to create a Powershell script to email you an event log and RAID status, browse our support archive for more useful articles on connectivity, IT, servers, email and more.

 

Asset 16

We answer real questions asked by businesses

From simple cyber security solutions to O365 migrations and everything in between. Take a look at our knowledge base and benefit from our expertise.

Get the best of Beaming straight to your inbox

Sign up for easy cyber security advice anyone can follow, research into what businesses are doing with their tech and how-to guides to boost productivity and efficiency.

  • This field is for validation purposes and should be left unchanged.