Hard Drive Space Monitor From Jenkins - JoelRochambeau/Powershell GitHub Wiki
[CmdletBinding()] Param( #Specifies the path to the configuration file. If there is no path listed, you will be prompted to enter one. [Parameter(Position=0, Mandatory=$true)] [String]$ConfigurationFilePath
) [int]$ExitCode = 0
#Check for config file $confFileExists = Test-Path -Path $configFilePath;
[string[]]$checkConf = ""; if ($confFileExists) {
$ServerNames = Get-Content -Path $(Resolve-Path -Relative $configFilePath);
} else { Write-Error "Cannot find configuration file: $configFilePath`nExiting..."; Exit 1; }
#Argument from Jenkins to pass the SeverNames. #Credential configuration required for Jenkins to pass the Server Account# $pass=$ENV:Password | ConvertTo-SecureString -asPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($ENV:UserName,$pass)
#Resets Array to null and Initializes $ServerStatus = @()
Foreach ($Computer in $ServerNames) {
write-host "$Computer Has been Checked"
#Initiate Invoke to remote computer using the Jenkins $creds
$CheckSpace = Invoke-Command -ComputerName $Computer -Credential $credential -ScriptBlock {
write-host "$Computer Invoke has been completed"
#Perform WMI call to gather Disk space size and information for C and D drives, if other drives are used on systems they need to be added
$CDrive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object size, freespace
$CDriveFreeSpace = $CDrive.freespace / 1GB
Write-host "$Computer C:\ Space is REMAINING $CDriveFreeSpace"
$DDrive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='D:'" | Select-Object size, freespace
$DDriveFreeSpace = $DDrive.freespace / 1GB
Write-host "$Computer D:\ Space is REMAINING $DDriveFreeSpace"
$JDrive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='J:'" | Select-Object size, freespace
$JDriveFreeSpace = $JDrive.freespace / 1GB
Write-host "$Computer J:\ Space is REMAINING $JDriveFreeSpace"
$KDrive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='K:'" | Select-Object size, freespace
$KDriveFreeSpace = $KDrive.freespace / 1GB
Write-host "$Computer K:\ Space is REMAINING $KDriveFreeSpace"
#$ServerStatus += "Hostname: " + $Computer + " Drive Issue: " + $CheckSpace + " </b> "+ "$Computer C:\ Space is REMAINING $CDriveFreeSpace </b>" + "D:\ Space is REMAINING $DDriveFreeSpace </b>"
#Check to see if size is less than 10GB remaining space
if ($CDriveFreeSpace -LE "10" -OR $DDriveFreeSpace -LE "10" -OR $JDriveFreeSpace -LE "10" -OR $KDriveFreeSpace -LE "10" ) {return $True}
#If first invoke returns $true, then we initiate second/third invoke to obtain drive details
}#End Invoke
if ($checkspace)
{
$GetCDriveInfo = Invoke-Command -ComputerName $Computer -Credential $credential -ScriptBlock {
$CDrive1 = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object size, freespace
$CDriveInfo = $CDrive1.freespace / 1GB
$CDriveCompleted = "$Computer C:\ Space REMAINING in GB: $CDriveInfo"
return $CDriveCompleted
}#end invoke
$GetDDriveInfo = Invoke-Command -ComputerName $Computer -Credential $credential -ScriptBlock {
$DDrive1 = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='D:'" | Select-Object size, freespace
$DDriveInfo = $DDrive1.freespace / 1GB
$DDriveCompleted = "$Computer D:\ Space REMAINING in GB: $DDriveInfo"
return $DDriveCompleted
}#end invoke
$GetJDriveInfo = Invoke-Command -ComputerName $Computer -Credential $credential -ScriptBlock {
$JDrive1 = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='J:'" | Select-Object size, freespace
$JDriveInfo = $JDrive1.freespace / 1GB
$JDriveCompleted = "$Computer J:\ Space REMAINING in GB: $JDriveInfo"
return $JDriveCompleted
}#end invoke
$GetKDriveInfo = Invoke-Command -ComputerName $Computer -Credential $credential -ScriptBlock {
$KDrive1 = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='K:'" | Select-Object size, freespace
$KDriveInfo = $KDrive1.freespace / 1GB
$KDriveCompleted = "$Computer K:\ Space REMAINING in GB: $KDriveInfo"
return $KDriveCompleted
}#end invoke
$ServerStatus += "Hostname: <b>" + $Computer + " </b> Drive Issue: <b>" + $CheckSpace + " </b></br> "+ "$GetCDriveInfo </br>" + "$GetDDriveInfo </br>" + "$GetJDriveInfo </br>" + "$GetKDriveInfo </br>"
}#end if
} #End Foreach
write-host $ServerStatus $NeedToEmail = $false foreach ($item in $ServerStatus) { if ($item -match "True") {$NeedToEmail = $true} } #End Foreach
if ($NeedToEmail)
{
write-host "Now sending email to Identified users because there minimum space requirement has been met"
$Body = "<b>Hard Drive Space Issue Detected:</b>" + " " + "</br> $ServerStatus </b> " +"</br></br> Correction of Drive Space is required, without Drive Space the services on the system will fail." +"</br>
</br></br>"
$EmailSignature = "Thank You, Enterprise Platform Services"
#Also, Email the contents above to the following Individuals
# $recipients = @("[email protected]", "[email protected]")
write-host "Emailing : $ENV:Recipients"
$recipients = $ENV:Recipients
$recipients = $recipients.split(",")
Send-MailMessage -SMTPServer smtp.dhsoha.state.or.us -To $recipients -From [email protected] -Subject "Hard Drive Space Issue Detected: True" -Body "$Body $EmailSignature" -BodyAsHtml
}