如果您运行的是PS 3.0或更高版本,则可以利用Invoke-WebRequest获取网络上存在的网页。如果您对本地文件it can be a bit finicky进行操作。
Invoke-WebRequest返回一个HtmlWebResponseObject,其中包含一个名为ParsedHtml的属性。这个对象有一个名为getElementById的方法,我们可以使用它,因为我们知道select标签上的id“id_country”。从那里,迭代选项标签并过滤以返回我们想要的属性......“文本”和“值”是一件简单的事情。
以下示例输出包含国家/地区名称和国家/地区代码的自定义对象:
代码:
# I'm using your raw pastebin endpoint for this example
$result = Invoke-WebRequest "http://pastebin.com/raw.php?i=b8cShFLA"
# Only return specific properties from the elements you're looking for
$countries = $result.ParsedHtml.getElementById("id_country") |
Where tagName -eq "option" |
Select -Property Text, Value
# Country name and code are stored to this variable
$countries
输出:
text value
---- -----
Afghanistan af
Albania al
Algeria dz
American Samoa as
Andorra ad
... ...
然后,您可以像使用powershell对象上的任何其他属性一样使用国家/地区名称和代码。
对于Web端点,听起来您可以修改此脚本以指向您从中提取此HTML的原始Mozilla页面?