Dark Developments Where Knowledge Meets Power

28Sep/110

PS – PowerShell Service Check

Posted by Dark#Basics

Because of some recent issues regarding automatic services not starting up after a reboot, I wrote a small PowerShell script that checks a service if it's running.

Select All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function CheckService{ param($ServiceName)
    $srvServiceDetails = Get-Service -Name $ServiceName
 
    if ($srvServiceDetails.Status -ne "Running"){
        SendMail -ServiceDetails $srvServiceDetails
    }
}
 
function SendMail{ param($ServiceDetails)
    $smtpServer = "out.darkdevelopments.com"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $strComputer = gc env:computername
    $smtpSubject = "[SYSMON] SERVICE ALERT @ " + $strComputer
    $msg.From = "sysmon@darkdevelopments.com"
    $msg.To.Add("darkbasics@darkdevelopments.com")
    $msg.Subject = $smtpSubject
    $msg.Body = "Hostname: " + $strComputer + "`nDisplayName: " + $ServiceDetails.displayname + "`nName: " + $ServiceDetails.name + "`nStatus: " + $ServiceDetails.status
    $smtp.Send($msg)
}
 
CheckService -ServiceName "TheServiceNameHere"
9Aug/110

PS – Overview Diskspace / Diskusage and Mail

Posted by Dark#Basics

PowerShell script to get the available diskspace of the computers specified. With markup for less the 20% and mail-functionality.

Select All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#Servernames
$colComputers = "SERVER01","SERVER02"
 
$erroractionpreference = "SilentlyContinue"
 
#Define Excel
$a = New-Object -comobject Excel.Application
$a.visible = $False
 
#Add Workbook & Sheets
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.name = "DiskSpace"
 
#Header Text
$c.Cells.Item(1,1) = "Server Name"
$c.Cells.Item(1,2) = "Size"
$c.Cells.Item(1,3) = "Total Size (MB)"
$c.Cells.Item(1,4) = "Available Space (MB)"
$c.Cells.Item(1,5) = "Available Space (%)"
 
#Font Style
$d = $c.UsedRange
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$d.EntireColumn.AutoFit($True)
 
$intRow = 2
 
#Emulate all the specified computers
foreach ($strComputer in $colComputers)
{
$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3"
 
$c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
 
foreach ($objdisk in $colDisks)
{
$c.Cells.Item($intRow, 2) = $objDisk.DeviceID
$c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1MB)
$c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1MB)
$c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
 
#-10%
if(([double]$objDisk.FreeSpace/[double]$objDisk.Size) -lt 0.10){
$c.Cells.Item($intRow, 2).Font.ColorIndex = 30
$c.Cells.Item($intRow, 2).Font.Bold = $True
$c.Cells.Item($intRow, 3).Font.ColorIndex = 30
$c.Cells.Item($intRow, 3).Font.Bold = $True
$c.Cells.Item($intRow, 4).Font.ColorIndex = 30
$c.Cells.Item($intRow, 4).Font.Bold = $True
$c.Cells.Item($intRow, 5).Font.ColorIndex = 30
$c.Cells.Item($intRow, 5).Font.Bold = $True
#-20%
}elseif(([double]$objDisk.FreeSpace/[double]$objDisk.Size) -lt 0.20){
$c.Cells.Item($intRow, 2).Font.ColorIndex = 46
$c.Cells.Item($intRow, 2).Font.Bold = $True
$c.Cells.Item($intRow, 3).Font.ColorIndex = 46
$c.Cells.Item($intRow, 3).Font.Bold = $True
$c.Cells.Item($intRow, 4).Font.ColorIndex = 46
$c.Cells.Item($intRow, 4).Font.Bold = $True
$c.Cells.Item($intRow, 5).Font.ColorIndex = 46
$c.Cells.Item($intRow, 5).Font.Bold = $True
}
 
$intRow = $intRow + 1
}
}
 
#Save with a nice datetime-stamp in filename
$datetimestamp = (Get-Date).Year.ToString() + (Get-Date).Month.ToString() + (Get-Date).Day.ToString() + (Get-Date).Hour.ToString() + (Get-Date).Minute.ToString() + (Get-Date).Second.ToString()
$filename = "DiskSpace_" + $datetimestamp + ".xlsx"
$b.SaveAs("C:\TEMP\" + $filename)
$a.Quit()
 
#SMTP
$smtpServer = "mail.internal.loc"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment("C:\TEMP\"+$filename)
$msg.From = "schedule@mail.internal.loc"
$msg.To.Add("darkbasics@mail.internal.loc")
#Subject
$msg.Subject = "[SCHEDULE] " + (Get-Date).Day.ToString() + "/" + (Get-Date).Month.ToString() + "/" + (Get-Date).Year.ToString() + " - Available Disk Space"
#Body
$msg.Body = "This is an auto generated message."
$msg.Attachments.Add($att)
$smtp.Send($msg)
 
#Delete TEMP-file
Remove-Item "C:\TEMP\" + $filename