Skip to content

Instantly share code, notes, and snippets.

@a4099181
Last active November 27, 2022 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a4099181/b1f9d9e4f6b4d752e58b7f2345332094 to your computer and use it in GitHub Desktop.
Save a4099181/b1f9d9e4f6b4d752e58b7f2345332094 to your computer and use it in GitHub Desktop.
Retrieves the current IP address. Stores the current IP address in the Windows registry to track changes. Notifies you of changes via email. Generates a link to wake up the computer via the Internet (wake on Lan, wake on WAN). Requires the creation of an registry key: 'HKCU\Software\seb!\Send-ChangedDynamicIP' with a property named 'Known'. Crea…
@{
RegistryKey = `
Get-ItemProperty -Path HKCU:\Software\seb!\Send-ChangedDynamicIP;
Current= `
( Invoke-WebRequest -Uri https://api.ipify.org?format=json -UseBasicParsing ).Content `
| ConvertFrom-Json `
| Select-Object -ExpandProperty ip;
Mac = `
Get-WmiObject win32_networkadapterconfiguration `
| Where-Object DefaultIPGateway `
| Select-Object -ExpandProperty MACAddress;
MailTo = ( `
"01000000d08c9ddf0115d1118c7a00c04fc297eb0100000064be572a413d464eb08e5e3022ce2284000" + `
"00000020000000000106600000001000020000000a0fc62209f390ce2b32f333ce71d8433beedf98fe8" + `
"427f393b51dff7e092177c000000000e80000000020000200000009329b9c2b13fbfae3ed3a55324cd5" + `
"ffb3e9244161010fe7d5e37a299a6c9aa6d30000000c113ba3c7460e5ef257b57f8b4450a59507799b1" + `
"31dc1bde10dcb4f7f36b23ed5ed6da1901a4881d0053912e0ab0f67a4000000028ce248057b6a2c1ad4" + `
"c48840f09a27a517b2f1181792302ddea65c339a2e294c362cef0a4f7f8e649e19ddb9574ffbf67c04c" + `
"3536a2485aa289ae6f7d7284b9" );
SmtpServer = ( `
"01000000d08c9ddf0115d1118c7a00c04fc297eb01000000a745e39559afea41991e3c6f937ea913000" + `
"00000020000000000106600000001000020000000ec6ea1f3401954f15ea15811b00c637467dbe03701" + `
"4394b5a9a295897f2b15c9000000000e80000000020000200000003e984c1dda1f4c75ac1c98eaab243" + `
"b5a9d7e41a3300875beeb817afebd08ce0f300000000154c7540dbc3466222cab378561285fd9f6129d" + `
"8b96b5102fb4fa915b30f4a247e729df7f8b4b7a3a57cb1518238732400000006e6a1c5e0c6ac8b921e" + `
"2312efaf67ee3a97caa9d5e9551e5438595938423be180dfa16e4875c0ec6cb140df572c5cf55105e21" + `
"1955062dcdab79bd08870a9631" );
WakeOnLANPort = 9
} |
Where-Object { ($_.Current -ne $_.RegistryKey.Known) -or `
((Get-Date).ToUniversalTime() - [System.DateTime]::new($_.RegistryKey.Stamp)).TotalDays -gt 6 } |
ForEach-Object {
Send-MailMessage `
-From ( New-Object System.Net.NetworkCredential( `
"n/a", `
( ConvertTo-SecureString $_.MailTo ) ) | Select-Object -ExpandProperty Password ) `
-To ( New-Object System.Net.NetworkCredential( `
"n/a", `
( ConvertTo-SecureString $_.MailTo ) ) | Select-Object -ExpandProperty Password ) `
-Subject $_.Current `
-SmtpServer ( New-Object System.Net.NetworkCredential( `
"n/a", `
( ConvertTo-SecureString $_.SmtpServer ) ) | Select-Object -ExpandProperty Password ) `
-Body @"
IP : $($_.Current)
MAC: $($_.Mac)
Wake on WAN link: https://www.depicus.com/wake-on-lan/woli?m=$($_.Mac.Replace(':','') )&i=$($_.Current)&p=$($_.WakeOnLANPort)
Ping link: https://ping.eu/ping/$($_.Current)
"@
Set-ItemProperty -Path $_.RegistryKey.PSPath -Name Known -Value $_.Current
Set-ItemProperty -Path $_.RegistryKey.PSPath -Name Stamp -Value (Get-Date).ToUniversalTime().Ticks
}
@a4099181
Copy link
Author

a4099181 commented May 14, 2019

Requirements

  • Windows registry key HKCU:\Software\seb!\Send-ChangedDynamicIP with a property named Known.
  • Some confidential data updates such as:
    • MailTo mail recipient (and sender also) address,
    • SmtpServer SMTP server FQDN.

How to...

  • create required Windows registry key (from Powershell console)

    New-Item HKCU:\Software\seb!
    New-Item HKCU:\Software\seb!\Send-ChangedDynamicIP
    New-ItemProperty -Path HKCU:\Software\seb!\Send-ChangedDynamicIP -Name Known
    
  • create your own confidential data (from Powershell console)

    ConvertTo-SecureString -String your-secret-here -AsPlainText -Force | Convertfrom-SecureString
    

    Copy the output and replace the current confidential data.

  • use

    Save the script to your profile. It is designed to be executed from Windows Task Scheduler. Create a task, let him execute as often as you wish. Define powershell.exe as executable with arguments -NoLogo -Sta -ExecutionPolicy ByPass -NoProfile -NonInteractive -WindowStyle Hidden -File <path-to-your-copy-of-the-script>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment