0

I was searching around and found a few hints but a few detail pieces are missing. Here is what I have:

install-chrome.bat

PowerShell -NoProfile -Command "&{Start-Process PowerShell -ArgumentList '-NoProfile -File install-chrome.ps1' -Verb RunAs}"

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:

  1. I still get a UAC popup even though the posts I found state that the above should start PowerShell in Admin mode.
  2. I was expecting .\ would download the .exe to the directory the .ps1 and .bat scripts are located.

Any hints on how to solve this?

EDIT: 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
2
  • you can try to set __compat_layer variable in order to aoic the UAC - but it could not work. Commented Aug 20, 2018 at 15:26
  • install-package GoogleChromeStandaloneEnterprise64.msi Commented Mar 27, 2024 at 15:26

2 Answers 2

3

Two things-

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"

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
Sign up to request clarification or add additional context in comments.

9 Comments

Any reason to use $client.DownloadFile($x,$y) instead of Invoke-WebRequest -Uri $x -Outffile $y?
@Paxz Nope, I was just copying the OP's code. They may be stuck on v2 (in which case my $PSScriptRoot suggestion needs more explanation).
Thannks for the reply, I am a bit confused regarding the -FilePath argument. And shouldn't there be another comma before -Verb. Lastly if I use the V3 version, how would I then execute the file?
@Thomas filepath is just explicitly calling out the argument you were passing implicitly. ArgumentList takes an array of strings so each of those is parsed as a string without the quotes. If you add another comma, you break the command.
I tried to execute this as you wrote it, but I always get an error (not sure how to post this here, so it is formatted right: At line:1 char:60 + Start-Process -FilePath PowerShell -ArgumentList -NoProfile, -File, ' ... + ~ Missing argument in parameter list. At line:1 char:67 + ... Process -FilePath PowerShell -ArgumentList -NoProfile, -File, 'C:\Pro ... + ~ Missing argument in parameter list.
|
0

Here you go:

$Path = $env:TEMP; $Installer = "chrome_installer.exe"; Invoke-WebRequest "http://dl.google.com/chrome/install/375.126/chrome_installer.exe" -OutFile $Path\$Installer; Start-Process -FilePath $Path\$Installer -Args "/silent /install" -Verb RunAs -Wait; Remove-Item $Path\$Installer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.