Invoke-RestMethod返回整数作为字符串

时间:2018-11-06 15:31:03

标签: powershell

我有一个整数变量,例如$int = (1,2,3)

我需要使用Invoke-RestMethod获得该值,例如$var = Invoke-RestMethod -Headers @{"Metadata-Flavor" = "Google"} -Uri http://169.254.169.254/computeMetadata/v1/instance/attributes/numbers的问题是,Invoke-RestMethod返回的值是字符串而不是整数。

也无法将其转换为整数[int]$var = $int 它返回一个错误: Cannot convert value "(1,2,3)" to type "System.Int32". Error: "Input string was not in a correct format."

如果我不使用Invoke-RestMethod来检索值,则它会按预期工作,因为$int = (1,2,3)创建了System.Int32。

1 个答案:

答案 0 :(得分:1)

您需要指定caching可以是数组。

int

帮助这有帮助!

编辑**

从字符串中删除括号,并在逗号处将其分割。

[int[]]$var = $int