WD / Powershell

WD
Spotfire SQL
--SELECT * FROM TMP_MVX_REPORT_SQL_LOG order by REG_TIMESTAMP desc 
--Press ctrl + c to copy sql




#IMPORTANT AND CONCERNS
# -Set the $percentCritcal and $percentWarning basing on requirements
# -$Users: Set the user names for email notification
# -$reportPath : Set the report path basing on the location where you want to store the reports
# -$computers: refer the location Servers.txt in which all the machine names added to monitor
# -$smtpServer: set the SMTP server which you want to use for email notitifcation
#*********************************************************#

# Continue even if there are errors
$ErrorActionPreference = "Continue";

# Set your warning and critical thresholds
$percentWarning = 25;
$percentCritcal = 10;

# EMAIL PROPERTIES
 # Set the recipients of the report.
 $users = "Tan.ChengKean@wdc.com,NobleKing.Constantin@wdc.com,MOHAMMAD.NAZMUL.HUDA.RUBOL.CHOWDHURY@wdc.com,Abhishek.KumarAshu@wdc.com"; 
  

# REPORT PROPERTIES
 # Path to the report
  $reportPath = "D:\Diskspace\Logs\";  # set the location on wehere you want to store the reports

 # Report name
  $reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";

# Path and Report name together
$diskReport = $reportPath + $reportName

#Set colors for table cell backgrounds
$redColor = "#FF0000"
$orangeColor = "#FBB917"
$whiteColor  = "#FFFFFF"
$greenColor  =  "#00FF00"

# Count if any computers have low disk space.  Do not send report if less than 1.
$i = 0;

# Get computer list to check disk space
$computers = Get-Content "D:\Diskspace\servers.txt";  #Set the location of server.txt file which contain all the server and desktop names which you want to monitor the disk space.
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";

# Remove the report if it has already been run today so it does not append to the existing report
If (Test-Path $diskReport)
    {
        Remove-Item $diskReport
    }

# Cleanup old files..
$Daysback = "-7"  #Set to clean the reports which are older than 7 days
$CurrentDate = Get-Date;
$DateToDelete = $CurrentDate.AddDays($Daysback);
Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item;

# Create and write HTML Header of report
$titleDate = get-date -uformat "%m-%d-%Y - %A %H:%M:%S"
$header = " 
  'Content-Type' content='text/html; charset=iso-8859-1'>
-->
  '100%'>
  '#548DD4'>
 
'7' height='30' align='center'>
  'calibri' color='#003399' size='4'>WD Server Disk Space  Report for $titledate  
"
 Add-Content $diskReport $header

# Create and write Table header for report
 $tableHeader = "
 '100%'> 

'10%' align='center'>Server

'5%'  align='center'>Drive 

'10%' align='center'>Total Capacity(GB)

'10%' align='center'>Used Capacity(GB)

'10%' align='center'>Free Space(GB)

'5%'  align='center'>Freespace 


"
Add-Content $diskReport $tableHeader
 
# Start processing disk space
  foreach($computer in $computers)
 { 
 $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3 AND DeviceID != 'P:'"
 $computer = $computer.toupper()
 $isEmailSend = 0
  foreach($disk in $disks)
 {        
  $deviceID = $disk.DeviceID;
        $volName = $disk.VolumeName;
  [float]$size = $disk.Size;
  [float]$freespace = $disk.FreeSpace; 
  $percentFree = [Math]::Round(($freespace / $size) * 100);
  $sizeGB = [Math]::Round($size / 1073741824, 2);
  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
        $usedSpaceGB = $sizeGB - $freeSpaceGB;
        $color = $whiteColor;
                       
# Set background color to Orange if just a warning

  if($percentFree -lt $percentWarning)      
    {
    $color = $orangeColor 
            }
  else{
   $color =$greenColor 
   }
# Set background color to Orange if space is Critical
      if($percentFree -lt $percentCritcal)
        {
        $color = $redColor
        $isEmailSend =1
       }        
 
 # Create table data rows 
    $dataRow =
'10%'>$computer 
'5%' align='center'>$deviceID 
'10%' align='center'>$sizeGB 
'10%' align='center'>$usedSpaceGB 
'10%' align='center'>$freeSpaceGB 
'5%' bgcolor=`'$color`' align='center'>$percentFree % 

"
Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";
    $i++  
 
 }
}

# Create table at end of report showing legend of colors for the critical and warning
 $tableDescription = "


'100%'>
  'White'> 
           
'15%' align='center' bgcolor='#00FF00'>Info - greater than 25% free space will be in green color

   
'10%' align='center' bgcolor='#FBB917'>Warning - less than 25% free space

'10%' align='center' bgcolor='#FF0000'>Critical - less than 10% free space


"
 Add-Content $diskReport $tableDescription
 Add-Content $diskReport "
"

# Send Notification if alert $i is greater then 0
if ($isEmailSend  -gt 0)
{
    foreach ($user in $users)
{
        Write-Host "Sending Email notification to $user"
  
  $smtpServer = "smtp.wdc.com" #set the SMTP server
  $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
  $msg = New-Object Net.Mail.MailMessage
  $msg.To.Add($user)
        $msg.From = "Tan.ChengKean@wdc.com" # set the user name from which email should send
                         
  $msg.Subject = "WD Server Disk Space Report for $titledate"
        $msg.IsBodyHTML = $true
        $msg.Body = get-content $diskReport
  $smtp.Send($msg)
        $body = ""
    }
  }





#IMPORTANT AND CONCERNS
# -Set the $percentCritcal and $percentWarning basing on requirements
# -$Users: Set the user names for email notification
# -$reportPath : Set the report path basing on the location where you want to store the reports
# -$computers: refer the location Servers.txt in which all the machine names added to monitor
# -$smtpServer: set the SMTP server which you want to use for email notitifcation
#*********************************************************#

# Continue even if there are errors
$ErrorActionPreference = "Continue";

# Set your warning and critical thresholds
$percentWarning = 25;
$percentCritcal = 10;

# EMAIL PROPERTIES
 # Set the recipients of the report.
 $users = "Tan.ChengKean@wdc.com,NobleKing.Constantin@wdc.com,MOHAMMAD.NAZMUL.HUDA.RUBOL.CHOWDHURY@wdc.com,Abhishek.KumarAshu@wdc.com"; 
 

# REPORT PROPERTIES
 # Path to the report
  $reportPath = "D:\Diskspace\Logs\";  # set the location on wehere you want to store the reports

 # Report name
  $reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";

# Path and Report name together
$diskReport = $reportPath + $reportName

#Set colors for table cell backgrounds
$redColor = "#FF0000"
$orangeColor = "#FBB917"
$whiteColor  = "#FFFFFF"
$greenColor  =  "#00FF00"

# Count if any computers have low disk space.  Do not send report if less than 1.
$i = 0;

# Get computer list to check disk space
$computers = Get-Content "D:\Diskspace\servers.txt";  #Set the location of server.txt file which contain all the server and desktop names which you want to monitor the disk space.
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";

# Remove the report if it has already been run today so it does not append to the existing report
If (Test-Path $diskReport)
    {
        Remove-Item $diskReport
    }

# Cleanup old files..
$Daysback = "-7"  #Set to clean the reports which are older than 7 days
$CurrentDate = Get-Date;
$DateToDelete = $CurrentDate.AddDays($Daysback);
Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item;

# Create and write HTML Header of report
$titleDate = get-date -uformat "%m-%d-%Y - %A %H:%M:%S"
$header = "
   
   
 
  DiskSpace Report
 
 
    
   

   
   
  WD Server Disk Space  Report for $titledate
 
  
  
  
"
 Add-Content $diskReport $header

# Create and write Table header for report
 $tableHeader = "
  

   
 Server
 Drive 
 Total Capacity(GB)
 Used Capacity(GB)
 Free Space(GB)
 Freespace 

 "
Add-Content $diskReport $tableHeader
 
# Start processing disk space
  foreach($computer in $computers)
 { 
 $disks = Get-WmiObject -ComputerName 172.21.41.251 -Class Win32_LogicalDisk -Filter "DriveType = 3 AND DeviceID != 'P:'"
 $computer = $computer.toupper()
 $isEmailSend = 0
  foreach($disk in $disks)
 {       
  $deviceID = $disk.DeviceID;
        $volName = $disk.VolumeName;
  [float]$size = $disk.Size;
  [float]$freespace = $disk.FreeSpace; 
  $percentFree = [Math]::Round(($freespace / $size) * 100);
  $sizeGB = [Math]::Round($size / 1073741824, 2);
  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
        $usedSpaceGB = $sizeGB - $freeSpaceGB;
        $color = $whiteColor;

# Set background color to Orange if just a warning

  if($percentFree -lt $percentWarning)     
    {
    $color = $orangeColor 
}
  else{
   $color =$greenColor 
   }
# Set background color to Orange if space is Critical
      if($percentFree -lt $percentCritcal)
        {
        $color = $redColor
$isEmailSend =1
       }       
 
 # Create table data rows 
    $dataRow = "
   
        $computer
  $deviceID 
  $sizeGB
  $usedSpaceGB
  $freeSpaceGB
  $percentFree % 
 
 "
Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";
    $i++ 
 
 }
}

# Create table at end of report showing legend of colors for the critical and warning
 $tableDescription = "


 
    
Info - greater than 25% free space will be in green color
    Warning - less than 25% free space
 Critical - less than 10% free space

 "
 Add-Content $diskReport $tableDescription
 Add-Content $diskReport "
"
# Send Notification if alert $i is greater then 0
if ($isEmailSend  -gt 0)
{
    foreach ($user in $users)
{
        Write-Host "Sending Email notification to $user"
 
  $smtpServer = "smtp.wdc.com" #set the SMTP server
  $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
  $msg = New-Object Net.Mail.MailMessage
  $msg.To.Add($user)
        $msg.From = "Tan.ChengKean@wdc.com" # set the user name from which email should send

  $msg.Subject = "WD Server Disk Space Report for $titledate"
        $msg.IsBodyHTML = $true
        $msg.Body = get-content $diskReport
  $smtp.Send($msg)
        $body = ""
    }
  }

Comments

Popular posts from this blog

Travel RESUME CV

PTE