Powershell 从 URL 列表中提取域

标签 powershell

我在文本文件中有一个网址列表

我只想提取域名。

运行以下命令:

(Get-Content "c:\temp\test.txt") | ForEach-Object {$url = New-Object System.uri $_;$url.Host} > d:\test.txt

它会生成此错误:

New-Object : Exception calling ".ctor" with "1" argument(s): "Invalid URI: The format of the URI could not be determined."

文本文件只是该域(并且只是域,而不是域后面的路径)一遍又一遍。

对我在哪里胡闹有什么想法吗?

示例文件:

lrp.mxptint.net
lrpush.apxlv.com
lsa.measure.ly
lsa.measure.ly\lsa.js?1449665894174&meta_keywords=Life%2C%20lifestyle%2C%20fashi‌​on%2C%20beauty%2C%20Vancouver%2C%20British%20Columbia%2C%20family%2C%20relationsh‌​ips%2C%20home%2C%20blogs%2C%20news%2C%20jobs%2C%20content%2C%20pets%2C%20food%2C%‌​20beauty&autofire=true&windowlocation=www.vancouversun.com\life\diversions lsa.measure.ly\lsa.js?1449665960913&meta_keywords=horoscopes&autofire=true&windo‌​wlocation=news.nationalpost.com\category\life

最佳答案

By definition ,URI 以方案开头(通常是协议(protocol),例如 http)。因此,.Net URI constructor期望字符串以协议(protocol)开头。例如,http://www.example.com。如果您传入 www.example.com 等,您将收到 Invalid URI 异常。

关于Powershell 从 URL 列表中提取域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210648/

相关文章:

powershell - 修剪命令输出

powershell - 如何在 Powershell 中使用带有 Where-Object 的参数化脚本 block ?

powershell - 为什么 Windows 远程管理服务坚持为 "delayed start"?

powershell - 为什么这个新的JObject添加到我的字符串时会奇怪地更改它?

powershell - 逐行读取文件并分割值

powershell - 在PowerShell中带括号的环境变量名称,例如%ProgramFiles(x86)%?

javascript - 格式化 Json 文件中的 Json 对象值

python - 测量命令 : measure python script execution time

powershell - 使用 UFormat 获取 unix 时间

C# 不会运行 PowerShell 脚本