Skip to content

Instantly share code, notes, and snippets.

@kral2
Created October 18, 2014 16:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kral2/e06396d14c12792e8394 to your computer and use it in GitHub Desktop.
Save kral2/e06396d14c12792e8394 to your computer and use it in GitHub Desktop.
Get IPv4 and mail it with gmail in powershell
# Quick powershell script to :
# Get the IPv4 and send it using gmail smtp server. Handy when there is no local SMTP server available
# !! Warning !! You need to provide your credentials in plain text ...
$date = (get-date).ToString()
# Get IPv4. Quick filter with -like "1*" to exclude IPv6. There should be a better way to do it ...
$IP = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -filter "ipenabled=true" |
Where { $_.IPAddress -like "1*"} |
Select -ExpandProperty IPAddress | select -First 1
## Customize this section
$user = "<yourGmailAccount>"
$pw = "<yourGmailPwd>"
$from = $user
$to = "<yourRecipients>"
$subject = $date + " | " + "My IP is " + " : " + $IP
$body = $subject
## Customize this section
# PowerShell SMTP section (with SSL support)
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-object System.Net.NetworkCredential($user,$pw);
$SMTPClient.Send($from,$to,$subject,$body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment