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
Thomas
  • 6,325
  • 4
  • 30
  • 65
  • you can try to set __compat_layer variable in order to aoic the [UAC](https://blogs.msdn.microsoft.com/cjacks/2009/09/13/how-to-run-applications-manifested-as-highestavailable-with-a-logon-script-without-elevation-for-members-of-the-administrators-group/) - but it could not work. – npocmaka Aug 20 '18 at 15:26

2 Answers2

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
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Any reason to use `$client.DownloadFile($x,$y)` instead of `Invoke-WebRequest -Uri $x -Outffile $y`? – Paxz Aug 20 '18 at 19:34
  • 1
    @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). – Maximilian Burszley Aug 20 '18 at 19:35
  • 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 Aug 21 '18 at 10:56
  • @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. – Maximilian Burszley Aug 21 '18 at 11:27
  • 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. – Thomas Aug 21 '18 at 11:43
  • @Thomas See my update. It should resolve your problems now – Maximilian Burszley Aug 21 '18 at 12:01
  • Thanks, but I still get UAC warning. Seems I have to execute the Batch file as Administrator as well. Very cumbersome process in windows, as I would need to create a shortcut to the batch file where I can finally set a flag to execute as Administrator. – Thomas Aug 21 '18 at 12:14
  • @Thomas I mean, that kind of makes sense since you can't do in-process rights elevation. Have you looked at `runas.exe`? – Maximilian Burszley Aug 21 '18 at 12:15
  • SO, thinks we should start a chat ;). Though I need to be working on something else now. This is meant to be installed on a Windows server to use headless chrome in an app. So I think I just provide the .ps1 file and the installer shall start PowerShell as admin and execute this script. Or at least execute the .bat as admin. I'll accept your answer though as it helped me a lot. – Thomas Aug 21 '18 at 12:19
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
lww
  • 624
  • 1
  • 7
  • 14