2017-09-15 22 views
0

在此question,得到的答复是如何从剪贴板中为变量分配一个字符串并使用该字符串搜索Google?

@echo off start "" chrome.exe www.google.com#q=batch

是什么#Q =在这里做?我是批处理脚本的新手,我尝试从剪贴板中传递一个字符串,代替上面的“批处理”一词。

我试图将powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"分配给一个变量,并尝试将该变量传递给第一个脚本代替“批处理”。

+0

'的https:/ /www.google.com/search?q = batch'或上面的'www.goog le.com#q = batch',Google搜索将使用搜索词** batch **。因为这是你上面唯一的问题,并不是一个编程问题,所以这个网站的主题不在话下。 – Compo

+0

我已经将powershell标签添加到您的问题中,因为您提到了它,显示了它的代码,尽管cmd.exe可以发送到剪贴板,但它没有本地方式从它检索。 – Compo

回答

2

Google搜索URL是google.com/search?q=。只需将剪贴板的内容添加到它并将其附加到浏览器调用。至少chrome.exeiexplore.exe允许URL作为未命名的参数。

$SearchTearm = [System.Windows.Forms.Clipboard]::GetText() 
$GoogleSearch = "https://www.google.com/search?q=$SearchTerm" 
$Browser = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' 
Start-Process $Browser -ArgumentList $GoogleSearch 

由于PowerShell 5,您可以使用Get-Clipboard而不是.NET函数。

+0

可能不是必要的,但取决于剪贴板内容,我会插入'[System.Net.WebUtility] :: UrlEncode()' – LotPings

+0

**编辑:抱歉,刚刚意识到,我应该在PowerShell中运行它,而不是cmd。 **我收到一个错误:'$ SearchTearm'不被识别为内部或外部命令,可操作程序或批处理文件。 '$ GoogleSearch','$ Browser','Start-Process'...相同。 –

+0

这对恐怕多个字符串不起作用。 –

0

不需要使用全路径导航器。总是有命令被保存。

冷凝(在PowerShell中5最小值):

对于铬:

Start-Process "chrome" -ArgumentList "https://www.google.com/search?q=$(Get-Clipboard)" 

对于Internet Explorer:

Start-Process "iexplore" -ArgumentList "https://www.google.com/search?q=$(Get-Clipboard)" 

对于边:

Start-Process "microsoft-edge:https://www.google.com/search?q=$(Get-Clipboard)" 
+0

由于'Get-Clipboard'是[Pscx](https://github.com/Pscx/Pscx)cmdlet,因此您不能假定它存在。 – LotPings

+1

获取剪贴板是包括到PowerShell中,就像我解释https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-clipboard?view=powershell-5.1 – Esperento57

+0

感谢您的反馈,它是奇怪我的PSVersion是'5.1.15063.608',但'gcm Get-Clipboard'告诉我源''pscx'版本'3.2.0.0' – LotPings

相关问题