从 Powershell 中的 URL 获取域 [英] Get Domain from URL in Powershell

查看:61
本文介绍了从 Powershell 中的 URL 获取域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在这种情况下使用 PowerShell 去除域.从以下变量中获取domain.com"的最有效方法是什么?

I'm looking to strip out the domain in this scenario using PowerShell. What is the most effective method to get 'domain.com' out of the following variable?

$URL = "http://www.domain.com/folder/"

(此处使用某种正则表达式命令将 $URL 转换/剥离为 $DOMAIN 使用 PowerShell)

(some sort of regex command here to convert/strip $URL into $DOMAIN using PowerShell)

$DOMAIN = "domain.com" #<-- taken from $URL

我已经搜索并找到了从域中查找 IP 地址的结果,但我需要首先使用正则表达式(或其他方法)确定域是什么.任何建议都很棒.

I've searched and I've found results for finding the IP address from a domain but I need to establish what the domain is first using regex (or another method). Any suggestions are great.

推荐答案

试试 Uri 类:

PS> [System.Uri]"http://www.domain.com/folder/"


AbsolutePath   : /folder/
AbsoluteUri    : http://www.domain.com/folder/
LocalPath      : /folder/
Authority      : www.domain.com
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /folder/
Segments       : {/, folder/}
IsUnc          : False
Host           : www.domain.com
Port           : 80
Query          :
Fragment       :
Scheme         : http
OriginalString : http://www.domain.com/folder/
DnsSafeHost    : www.domain.com
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

并删除 www 前缀:

And remove the www prefix:

PS> ([System.Uri]"http://www.domain.com/folder/").Host -replace '^www\.'
domain.com

这篇关于从 Powershell 中的 URL 获取域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆