Created
August 14, 2020 20:29
-
-
Save dcode/b9e292f9065c601eaf8cb93ebe68f124 to your computer and use it in GitHub Desktop.
This is the Google Compute Engine install-driver file used for windows driver installation. I couldn't find it anywhere else online.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Copyright 2018 Google, Inc. All Rights Reserved. | |
<# | |
.SYNOPSIS | |
Script to install specific driver. | |
.DESCRIPTION | |
Script will add certs from the driver to TrustedPublisher store and | |
install driver with pnputil. | |
NOTE: The script needs to be run with admin priviledge. | |
.PARAMETER Target | |
The target driver to install. Required. | |
.PARAMETER Dev | |
Whether to install a development version of the driver. Default to $false. | |
.EXAMPLE | |
install-driver.ps1 -Target gvnic -Dev $false | |
Install a development version of gvnic driver. | |
#> | |
param ( | |
[parameter(Mandatory=$true)] | |
[ValidateSet('gvnic', 'netkvm', 'vioscsi', 'gga', 'pvpanic', 'balloon')] | |
[string]$Target, | |
[bool]$Dev = $false | |
) | |
$ErrorActionPreference = 'Stop' | |
if (!$PSScriptRoot) { | |
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent | |
} | |
Import-Module $PSScriptRoot\package-common.psm1 -Force | |
# https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/pnputil-return-values | |
$PNPUTIL_REBOOT_REQUIRED = 3010 | |
Assert-AdminUser | |
function Install-DriverCert { | |
param ( | |
[string]$DriverPath | |
) | |
$signature = Get-AuthenticodeSignature $DriverPath; | |
$cert = $signature.SignerCertificate; | |
$store = New-Object -TypeName ` | |
System.Security.Cryptography.X509Certificates.X509Store ` | |
-ArgumentList ` | |
@([System.Security.Cryptography.X509Certificates.StoreName]::TrustedPublisher, ` | |
[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine); | |
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite); | |
$store.Add($cert); | |
$store.Close() | |
} | |
Write-Output "Trying to install driver for $Target" | |
if ($Dev) { | |
Write-Output 'Development driver is selected.' | |
} | |
$package_install_folder = $MyInvocation.MyCommand.Path | Split-Path | Split-Path | |
$driver_folder = Get-PackageContentsFolder $Target -Dev $Dev | |
$driver_folder = Join-Path $package_install_folder $driver_folder | |
if (!(Test-Path $driver_folder)) { | |
throw "$driver_folder not found. Not a supported Windows version." | |
} | |
Write-Output 'Install driver cert to TrustedPublisher.' | |
$sys_file = Get-ChildItem $driver_folder -Filter '*.sys' | |
Install-DriverCert $sys_file.FullName | |
Write-Output "Install driver from $driver_folder" | |
$inf_file = Get-ChildItem $driver_folder -Filter '*.inf' | |
& pnputil -i -a $inf_file.FullName | |
if ($LASTEXITCODE -eq $PNPUTIL_REBOOT_REQUIRED) { | |
Write-Warning 'Driver installation completed successfully but a manual reboot is required.' | |
exit 0 | |
} | |
elseif ($LASTEXITCODE -ne 0) { | |
Write-Output 'Driver installation failed.' | |
exit 1 | |
} | |
if ($Target -eq 'gvnic' -and (Test-Path "${driver_folder}\gvnichelper.dll")) { | |
Import-Module $PSScriptRoot\gvnic_helper.psm1 | |
Write-Output 'Register gvnic_helper.' | |
$HelperPath = "${env:ProgramFiles}\Google\Compute Engine\gvnic_helper" | |
if (!(Test-Path $HelperPath)) { | |
New-Item $HelperPath -Type Directory | |
} | |
& robocopy $driver_folder $HelperPath gvnichelper.dll /r:5 /w:1 /np /fp /njh /ndl /nfl /njs /nc /ns | |
$DllPath = Join-Path $HelperPath 'gvnichelper.dll' | |
Register-Helper $DllPath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment