2014-12-19 80 views
2

我们的应用程序使用,包含以下信息如何删除添加到“添加可信站点?”所需的详细信息?

[DEFAULT] 
BASEURL=http://MachineName:1800/App/LandingPage.aspx 
[InternetShortcut] 
URL=http://MachineName:1800/App/LandingPage.aspx 

我需要这个网址添加到受信任网站的URL文件。

首先,我需要获得单独http://MachineName

如果我运行followind命令,它具有完整产品线,其中BASEURL存在。

$URL = Get-content FileName.url | Select-string -pattern "BASEURL" 

如何使用powershell从http://MachineName获取内容?

回答

4

Select-String cmdlet返回一个布尔值或MatchInfo。按照documentation

输出Microsoft.PowerShell.Commands.MatchInfo或System.Boolean通过 默认情况下,输出是一组MatchInfo对象,一个对于每个找到的匹配 的。如果使用Quiet参数,则输出是一个布尔值 ,指示是否找到该模式。

当您在没有使用-quiet的情况下得到多个匹配项时,您会得到一个MatchInfo对象数组。结果可通过Matches[]阵列的Value属性像这样被访问,

PS C:\> $URL = Get-content \temp\FileName.url | Select-string -pattern "(http://[^:]+)" 
PS C:\> $URL 

BASEURL=http://MachineName:1800/App/LandingPage.aspx 
URL=http://MachineName:1800/App/LandingPage.aspx 

PS C:\> $URL[0].Matches[0].value 
http://MachineName 
PS C:\> $URL[1].Matches[0].value 
http://MachineName 

为了只捕获BASEURL字符串不带前缀,使用非捕获组像这样,

PS C:\> $URL = Get-content \temp\FileName.url | Select-string -pattern "(?:BASEURL=)(http://[^:]+)" 
PS C:\> $url 

BASEURL=http://MachineName:1800/App/LandingPage.aspx 

PS C:\> $url.Matches[0].Groups[1].Value 
http://MachineName 
相关问题