Now I know that anyone lured into this blog post by the title will be asking, "Colin, why not use a DDNS provider?", and the answer would be, "Because I don't want to!". It's a lot easier to justify spent time on technology to your wife than it is to justify spent money. ;)
So, the code:
#Send-IP2Gmail.ps1
#$F = File
#$U = Username
#$P = Password
#$B = Browser
#$C = Credentials
#$E = Email Address
#$I = IP Address
#$S = SMTP Server
function Store-myPass
{
$global:U = $args[0]
$global:F = $args[1]
$Credential = Get-Credential $U
$credential.Password | ConvertFrom-SecureString | Set-Content $F
}
function Get-myCred
{
$U = $args[0]
$F = $args[1]
$P = Get-Content $F | ConvertTo-SecureString 
$C = New-Object System.Management.Automation.PsCredential($U,$P)
$C
}
function Send-IP
{
$U = $args[0]
$F = $args[1]
$C = get-myCred $U $F
$url =  "http://www.whatismyip.com/automation/n09230945.asp"
$browser = New-Object Net.Webclient
$S = "smtp.gmail.com"
$E = $U + "@gmail.com"
$I = $browser.DownloadString($url)
Send-MailMessage -to $E -from $E -Subject $I -Credential $C -SmtpServer $S -UseSsl
}
$Uv1 = $args[0]
$Setup = $args[1]
if ($args[0] -eq $null){
    Write-Host ""
    Write-Host "" 
    Write-Host -ForegroundColor Yellow "Welcome to Send-IP2Gmail Usage Instructions!"
    Write-Host ""
    Write-Host "" 
    Write-Host -ForegroundColor Yellow "Running this cmdlet without specifying a username will bring you to this message."
    Write-Host -ForegroundColor Yellow 'For initial configuration, or to change your password, use the "setup" command:'
    Write-Host ""
    Write-Host -ForegroundColor Red ".\Send-IP2Gmail davidcolinsmith setup"
    Write-Host ""
    Write-Host ""
    Write-Host -ForegroundColor Yellow "After this command is run, your password is securely hashed in a file in the "
    Write-Host -ForegroundColor Yellow 'C:\creds\ folder.  Subsequent use of this command without the "setup"'
    Write-Host -ForegroundColor Yellow "feature can be schedule or scripted using this syntax:"
    Write-Host ""
    Write-Host -ForegroundColor Red ".\Send-IP2Gmail davidcolinsmith"
    Write-Host ""
    }
else{
    if ($Setup -eq "setup")
         {
         $U = $Uv1
         $Result = test-path -path "c:\creds"
    if (!(test-path -path "c:\creds"))
        {
        new-item c:\creds -type directory
        $credspath = "c:\creds\" + $U + ".txt"
        if (!(test-path -path $credspath))
        {
        new-item $credspath -type file
        }
     else{}
 Write-Host "Send your Public DHCP-assigned IP address to your gMail account"
    [environment]::SetEnvironmentVariable("Send-IP-Username", $U, "User")
 $global:F = "c:\creds\" + $U + ".txt"
 [environment]::SetEnvironmentVariable("Send-IP-CredPath", $F, "User")
 Store-myPass $U $F
 }
 else{ 
  Write-Host "Send your Public DHCP-assigned IP address to your gMail account"
  [environment]::SetEnvironmentVariable("Send-IP-Username", $U, "User")
  $global:F = "c:\creds\" + $U + ".txt"
  [environment]::SetEnvironmentVariable("Send-IP-CredPath", $F, "User")
  Store-myPass $U $F
  }
 }
    else {
    $U = get-childitem env:send-ip-username
    $F = get-childitem env:Send-IP-CredPath
    $U = $U.value
    $F = $F.value
    }
Send-IP $U $F
}
Comments, questions, concerns, or suggestions are welcome!
 
 
No comments:
Post a Comment