powershell

[PowerShell] Send email from Gmail with attachment on PowerShell

Credits goes to 

I modified the script a little bit to pick up the log from c:\temp\ daily where the filename is made up of yyyymmdd

$a = Get-Date -Format yyyyMMdd

$smtpClient = new-object system.net.mail.smtpClient 
$smtpClient.Host = 'smtp.gmail.com'
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username@gmail.com", "password");

$emailFrom = "sender@domain.com"
$emailTo = "receipt1@domain.com,receipt2@domain.com"
$Subject = "Backup log for " +$a
$Body = "Please refer to attached logfile."

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $EmailFrom
$emailMessage.To.Add($EmailTo)
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\temp\libra_backup"+ $a+".txt")
$SMTPClient.Send($emailMessage)
$emailMessage.Attachments.Dispose()

 

For more information on Get-Date format, please click here

[PowerShell] Retrieving hard disk health through SMART status

To get the SMART status of the hard disk with PowerShell, you could run the following:

Get-WmiObject win32_diskdrive|Select-Object Status

 

Below is a sample script to check multiple PC’s SMART status by getting a list of PC names from a text file.

$allComputers=Get-Content -Path C:\temp\pc.txt

foreach($computer in $allComputers)
{

$isonline=test-connection -Quiet -count 2 -ComputerName $computer
if($isonline)
{

$diskStatus= Get-WmiObject win32_diskdrive -ComputerName $computer|Select-Object -ExpandProperty Status

Write-Host -ForegroundColor Green "$computer is online and disk status is $diskStatus";
$diskStatus="";
}
else
{
Write-Host -ForegroundColor Red "$computer is offline";
}

}

[PowerShell] Script Lets You Check Patches’ Status

I was asked to modify the script posted at http://windowsitpro.com/scripting/powershell-script-lets-you-check-patches-status

such that given a list of computers, check if any PC has missed a KB. So here it is:

$erroractionpreference = “SilentlyContinue”
clear-host

if ($args.count -lt 2)
{
write-host -f blue “Syntax Error : Must have 2 parameters.”
write-host -f blue “Eg powershell.exe \kb.ps1 ”
break
}

$filename = $args[0]
$computer = $args[1]
$kbs = get-content $filename
$kbTotal = 0
$kbNotFound = 0
write-host -f green “Checking on”$computer

foreach ($kb in $kbs)
{
$global:kbTotal++
$strQuery = “select * from win32_pingstatus where address = ‘” + $computer + “‘”
$wmi = get-wmiobject -query $strQuery
if ($wmi.statuscode -eq 0)
{
$checkkb = get-wmiobject Win32_QuickFixEngineering -computer $computer | where-object {$_.hotfixid -eq $kb} | select-object hotfixid, description
# BEGIN CALLOUT A

if ($checkkb.hotfixid -eq $kb)
{write-host -f green $kb “`t” $checkkb.description “`r”}
else
{write-host -f red $kb “`t” “Patch not found.” “`r”
$global:kbNotFound++
}
}
else
{write-host $computer “`t” “Ping failed.” “`r”}
# END CALLOUT A
}

if ($global:kbNotFound -eq 0)
{write-host -ForegroundColor Green $global:kbNotFound “KBs missing out of”$global:kbTotal “required”}
else
{write-host -ForegroundColor Red $global:kbNotFound “KBs missing out of”$global:kbTotal “required”}

Below is the the version where it outputs to a text file, I had modified it to output to c:\temp instead:

# Function : to check for kbxxxx patches on remote pc
# This version redirects the output to a text file.

$erroractionpreference = “SilentlyContinue”

cls
if ($args.count -ne 2)
{
write-host -f blue “Syntax Error : Must have 2 parameters.”
write-host -f blue “Eg powershell.exe \kb.ps1 ”
break
}

$filename = $args[0]
$computer = $args[1]
$kbs = get-content $filename

write-host -f green “Checking on”$computer
“Checking on” +$computer | out-file c:\temp\results.txt -append

foreach ($kb in $kbs)
{

$strQuery = “select * from win32_pingstatus where address = ‘” + $computer + “‘”
$wmi = gwmi -query $strQuery

if ($wmi.statuscode -eq 0)
{
$checkkb = gwmi Win32_QuickFixEngineering -computer $computer | where {$_.hotfixid -eq $kb} | `
select-object hotfixid, description

if ($checkkb.hotfixid -eq $kb)
{
write-host -f green $kb “`t” $checkkb.description “`r”
$kb + ” ” + $checkkb.description | out-file c:\temp\results.txt -append
}

else
{
write-host -f red $kb “`t” “Patch not found.” “`r”
$kb + ” patch not found” | out-file c:\temp\results.txt -append
}

}
else
{
write-host $computer “`t” “Ping fail.” “`r”
$computer + ” ping fail”| out-file c:\temp\results.txt -append
}

}