【发布时间】:2020-04-13 14:20:13
【问题描述】:
我似乎在使用这个看似基本的脚本来使用 Google 的 DDNS 服务更新我的 IP 时遇到很多问题。
#Fetch current IP address
$ipuri = "https://api.ipify.org"
$ip = Invoke-RestMethod -Uri $ipuri
#Send fetched IP address to Google Domains through their API
$uri = "https://username:password.google.com/nic/update?hostname=home.domain.com&myip="$ip""
Invoke-RestMethod -Method 'Get' -Uri $uri
第一个问题是 $ip 变量。如果我将 $ip 保留在上面,它不会产生任何输出,但没有它也可以正常工作(我需要它作为变量,因为我稍后会使用它)。
第二个问题是https://username:password@domains.google.com/nic/update?hostname=home.domain.com&myip="$ip"。
如果我将确切的字符串转储到邮递员中,它就可以正常工作(用实际的 IP 地址代替 $ip) 但即使我在 PowerShell 中使用手动插入的 IP(例如https://username:password@domains.google.com/nic/update?hostname=home.domain.com&myip=1.2.3.4)运行它也无法发送任何内容。
附:在我的实际代码中,我替换为正确的用户名和密码(由 Google 提供)和正确的子域、域、*域,因为它适用于我。
对此有什么想法吗?
编辑: 更新(和工作)的代码如下所示:
#Fetches current IPv4 address
$ipuri = "https://api.ipify.org"
$ip = Invoke-RestMethod -Uri $ipuri
#Stores Google-provided username and password
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$credential = New-Object Management.Automation.PSCredential ('username', $password)
#Send fetched IP address to Google Domains through their API
$uri = "https://domains.google.com/nic/update?hostname=home.domain.com&myip=$($ip)"
Invoke-RestMethod -Method 'POST' -Uri $uri -Credential $credential
【问题讨论】:
标签: powershell rest api dynamic dns