【问题标题】:Invoke-RestMethod PowerShell Google DDNSInvoke-RestMethod PowerShell Google DDNS
【发布时间】: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


    【解决方案1】:

    首先,这是一个简洁的服务,api.ipify.org,我以后必须使用它。

    其次,我认为这里唯一的问题是您对$url 的定义。

    如果您尝试自行运行该行,您之前的语法实际上会引发错误,此处显示错误。

    "https://username:password.google.com/nic/update?hostname=home.domain.com&myip="$ip""
    At line:1 char:81
    + ... e:password.google.com/nic/update?hostname=home.domain.com&myip="$ip""
    +                                                                     ~~~~~
    Unexpected token '$ip""' in expression or statement.
    

    在 PowerShell 中,您应该使用这样的字符串扩展语法,而不是嵌套引号。

    $uri = "https://username:password.google.com/nic/update?hostname=home.domain.com&myip=$($ip)"
    

    更新

    https://support.google.com/domains/answer/6147083?hl=en 找到 API 文档。他们说使用 Basic Auth 提供您的用户名和密码,这会生成您的凭据的 base64 编码字符串。我们可以很容易地在 PowerShell 中做到这一点!

    您可以使用Get-Credential cmdlet 提供您的凭据以保存它们,然后将它们传递到Invoke-RestMethod 并添加-Credential-Authentication 作为参数。这是一个完整的解决方案的样子。

    $ipuri = "https://api.ipify.org"
    $ip = Invoke-RestMethod -Uri $ipuri
    #Send fetched IP address to Google Domains through their API
    $myCredential = Get-Credential #you'll be prompted to provide your google username and pwd.  
    $uri = "https://domains.google.com/nic/update?hostname=home.domain.com&myip=$($ip)"
    Invoke-RestMethod -Method 'POST' -Uri $uri -Credential $myCredential -Authentication Basic
    
    

    【讨论】:

    • 尝试更改语法。我收到以下错误:Invoke-RestMethod:远程服务器返回错误:(401)未经授权。在 line:4 char:1 + Invoke-RestMethod -Method 'Get' -Uri $uri.奇怪的是,如果我尝试 Out-File 它,输出是完全空白的。出现这种情况是因为有错误还是因为实际上没有输出?
    • 你能指出 API 文档吗?您非常、非常、非常不太可能通过 HTTP GET 来更新您的域信息,这几乎总是PATCHPUTPOST。 401 意味着至少你已经接近了。
    • 在页面底部的“使用 API 更新您的动态 DNS 记录”下link。我还发现它允许 GET 非常奇怪,但文档声称(我通过 Postman 验证了这一点)POST 或 GET 似乎都可以工作。
    • 好的,我找到了文档并更新了我的示例。我已经对其进行了测试,它似乎可以按预期工作。另请注意,您的我修复了 URl,这似乎不正确。
    • 试用了您的新代码,收到以下错误:Invoke-RestMethod:找不到与参数名称“AuthenticationBasic”匹配的参数。在没有 -AuthenticationBasic 的情况下尝试过,但它有效!最后一个问题,如果用户名和密码不会改变,是否有可能以某种方式对其进行硬编码?