Powershell commands to automate basic server tasks
Posted on 13 December 2017 by Beaming SupportPowershell 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)}}, |
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-EventLog –Logname Application then changing the document name to $AppLog$month.txt.
Add-Content Add-Content Add-Content C:ScriptSysLog$month.txt Get-EventLog |
The following are variables that count the amount of Event entries, for adding to our email.
$SysCount = (Get-EventLog –Logname System –EntryType Error, Warning -After $dat).count $AppCount = (Get-EventLog –Logname 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 $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.