使用PowerShell在Windows上使用.bat文件安装Chrome [英] Install Chrome on Windows with a .bat file using PowerShell
问题描述
我正在四处搜寻,发现了一些提示,但缺少一些细节. 这是我所拥有的:
I was searching around and found a few hints but a few detail pieces are missing. Here is what I have:
install-chrome.bat
install-chrome.bat
PowerShell -NoProfile -Command "&{Start-Process PowerShell -ArgumentList '-NoProfile -File install-chrome.ps1' -Verb RunAs}"
install-chrome.ps1
install-chrome.ps1
$client = New-Object System.Net.WebClient;
$client.DownloadFile("https://dl.google.com/chrome/install/ChromeStandaloneSetup64.exe", ".\ChromeStandaloneSetup64.exe");
.\ChromeStandaloneSetup64.exe /silent /install ;
两件事没有按预期进行:
Two things are not working as expected:
- 即使我发现帖子指出上面的内容应该以Admin模式启动PowerShell,我仍然会看到UAC弹出窗口.
- 我期望
.\
将.exe
下载到.ps1
和.bat
脚本所在的目录.
- I still get a UAC popup even though the posts I found state that the above should start PowerShell in Admin mode.
- I was expecting
.\
would download the.exe
to the directory the.ps1
and.bat
scripts are located.
关于如何解决此问题的任何提示?
Any hints on how to solve this?
感谢@ TheIncorrigible1的回复,我设法解决了第二部分.当我直接在PowerShell中执行它们时,这两个选项或多或少都起作用(下载它,但安装会在本地引发错误):
Thanks to the reply from @TheIncorrigible1 I managed to solve the second part. Both options work more or less (it downloads it, but the installation throws an error locally) when I execute them directly in PowerShell:
< V3
$PSScriptRoot = Split-Path -Parent -Path $script:MyInvocation.MyCommand.Path
$uri = "https://dl.google.com/chrome/install/ChromeStandaloneSetup64.exe"
$path = "$PSScriptRoot\ChromeStandaloneSetup64.exe"
$client = New-Object System.Net.WebClient
$client.DownloadFile($uri, $path)
& $path /install
V3 +
$uri = "https://dl.google.com/chrome/install/ChromeStandaloneSetup64.exe"
$path = "$PSScriptRoot\ChromeStandaloneSetup64.exe"
Invoke-WebRequest -Uri $uri -OutFile $path
& $path /install
但是该批次仍会引发错误:
But the batch still throws errors:
At line:1 char:62
+ ... tart-Process PowerShell -Verb RunAs -ArgumentList -NoProfile, -File, ...
+ ~
Missing argument in parameter list.
At line:1 char:69
+ ... ocess PowerShell -Verb RunAs -ArgumentList -NoProfile, -File, 'C:\Pro ...
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
推荐答案
两件事-
您不需要将批处理命令包装到powershell中的脚本块中,并且-ArgumentList
需要一个字符串参数数组:
You don't need to wrap your batch command to powershell in a scriptblock and -ArgumentList
expects an array of string arguments:
powershell.exe -NoProfile -Command "Start-Process -FilePath powershell.exe -ArgumentList @('-NoProfile', '-File', '%~dp0install-chrome.ps1') -Verb RunAs"
有一个自动变量$PSScriptRoot
来确定您的根目录在哪里:
There's an automatic variable, $PSScriptRoot
, to determine where your root directory is:
$uri = 'https://dl.google.com/chrome/install/ChromeStandaloneSetup64.exe'
if (-not $PSScriptRoot) {
$PSScriptRoot = Split-Path -Parent -Path $script:MyInvocation.MyCommand.Definition
}
$outFile = "$PSScriptRoot\ChromeStandaloneSetup64.exe"
if ($PSVersionTable.PSVersion.Major -lt 3) {
(New-Object -TypeName System.Net.WebClient).DownloadFile($uri, $outFile)
}
else {
Invoke-WebRequest -Uri $uri -OutFile $outFile
}
& $outFile /silent /install
这篇关于使用PowerShell在Windows上使用.bat文件安装Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!