获取 Microsoft Edge 选项卡标题/URL 使用 Powershell

huangapple go评论50阅读模式
英文:

Grab Microsoft Edge Tab title/URL using Powershell

问题

I'm brand new to Powershell.

我对Powershell全新。

I'm writing a script to validate the basic functionality of Microsoft Edge. All it needs to do is open an msedge window and check to see that the correct default site loads.

我正在编写一个脚本来验证Microsoft Edge的基本功能。它只需要打开一个msedge窗口并检查是否加载了正确的默认网站。

I originally wanted to grab the URL and output it but I only see solutions that are way too lengthy for a script like this, so I opted to output the result of grabbing MainWindowTitle.

我最初想获取URL并输出它,但我只看到了对于这样的脚本来说过于冗长的解决方案,所以我选择输出获取MainWindowTitle的结果。

I am trying to use it like this:

if((Get-Process | Select MainWindowTitle) -like "wikipedia.org"){
  Write-Host "Site successfully opened."
} else {
  Write-Host "Site failed."
}

我尝试像这样使用它:

if((Get-Process | Select MainWindowTitle) -like "wikipedia.org"){
  Write-Host "Site successfully opened."
} else {
  Write-Host "Site failed."
}

Despite landing on the correct site and the output of Get-Process | Select MainWindowTitle being identical to the string I compare it to, the test fails. The command works fine outside of an if statement so I guess it can't be used in the way I'm attempting to use it. Is there any way to do this?

尽管着陆在正确的网站上,并且Get-Process | Select MainWindowTitle的输出与我进行比较的字符串相同,但测试失败。该命令在if语句之外运行良好,所以我猜它不能以我尝试的方式使用。是否有办法做到这一点?

英文:

I'm brand new to Powershell.

I'm writing a script to validate the basic functionality of Microsoft Edge. All it needs to do is open an msedge window and check to see that the correct default site loads.

I originally wanted to grab the URL and output it but I only see solutions that are way too lengthy for a script like this, so I opted to output the result of grabbing MainWindowTitle.

I am trying to use it like this:

if((Get-Process | Select MainWindowTitle) -like "wikipedia.org"){
  Write-Host "Site successfully opened."
} else {
  Write-Host "Site failed."
}

Despite landing on the correct site and the output of Get-Process | Select MainWindowTitle being identical to the string I compare it to, the test fails. The command works fine outside of an if statement so I guess it can't be used in the way I'm attempting to use it. Is there any way to do this?

答案1

得分: 0

Select MainWindowTitle 输出一个具有单个属性的 对象

通常情况下,将对象与字符串进行比较不会按预期工作(PowerShell 尝试使用其 .ToString() 方法将对象转换为字符串,在进程对象的情况下,会产生类似于 "System.Diagnostics.Process (进程名称)" 的结果)。

您可以使用 Where-Object 的简化形式来过滤具有特定属性值的对象:

if (Get-Process | Where-Object MainWindowTitle -like 'wikipedia.org') {
  Write-Host "网站成功打开。"
} else {
  Write-Host "网站打开失败。"
}

如果 Where-Object 找到至少一个匹配的进程对象,则条件将评估为 $true

英文:

Select MainWindowTitle outputs an object with a single property.

Comparing an object with a string usually doesn't work as expected (PowerShell tries to convert the object to string using its .ToString() method, which in case of a process object produces something like "System.Diagnostics.Process (processname)").

You may use Where-Object in its simplified form to filter for objects with given property values:

if( Get-Process | Where-Object MainWindowTitle -like '*wikipedia.org*' ) {
  Write-Host "Site successfully opened."
} else {
  Write-Host "Site failed."
}

The condition evaluates to $true, if Where-Object found at least one matching process object.

huangapple
  • 本文由 发表于 2023年6月15日 21:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定