-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipinfo.ps1
81 lines (69 loc) · 2.5 KB
/
ipinfo.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Description: Send IP information to an email address when the network changes
# Author: Alexandre Teles
# License: WTFPL
# Version: 1.0.0
# Settings
$scriptPath = "C:\scripts\"
$scriptName = "ipinfo.ps1"
$machineName = $env:COMPUTERNAME
$apiKey = "" # Your Mailgun API key
$mailgunUrl = "" # Your Mailgun API URL
$fromAddress = "" # Who is sending this email?
$toAddress = "" # Who should receive this email?
$subject = $machineName + ": " + "IP Information"
$taskName = "IP Information at Startup"
function Install {
if (!(Test-Path -Path $scriptPath)) {
New-Item -ItemType Directory -Path $scriptPath
}
$source = $PSCommandPath
$destination = "$scriptPath$scriptName"
if (!(Test-Path -Path $destination)) {
Copy-Item -Path $source -Destination $destination
}
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$scriptPath", "Machine")
if(!(Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue)){
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-File $scriptPath$scriptName"
$trigger = New-ScheduledTaskTrigger -AtStartup
$settings = New-ScheduledTaskSettingsSet
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings
}
}
function Uninstall {
if(Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
}
if(Test-Path "$scriptPath$scriptName") {
Remove-Item -Path "$scriptPath$scriptName" -Confirm:$false
}
}
function Main {
$ipinfo = Get-NetIPConfiguration -All
$localIP = $ipinfo | Format-List | Out-String
$remoteIP = Invoke-WebRequest -Uri "https://ifconfig.io/ip"
$body = $localIP + "`n" + "External IP: " + $remoteIP.Content
return Invoke-RestMethod -Uri $mailgunUrl -Method Post -Credential (
New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "api", (
ConvertTo-SecureString -String $apiKey -AsPlainText -Force)) -Body @{
from=$fromAddress;
to=$toAddress;
subject=$subject;
text=$body}
}
if ($args.Count -eq 0) {
Main
} else {
switch ($args[0]) {
"--install" {
Install
break
}
"--uninstall" {
Uninstall
break
}
default {
Write-Host "Invalid argument. Available options are: --install, --uninstall"
}
}
}