Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add script #8

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ jobs:
run: |
cd build
cmake --install .
set PACKAGE_VERSION=${{ github.ref_name }}
set PACKAGE_VERSION=%PACKAGE_VERSION:~1%
rename "package_contents" "WinUSB-installer-generator-%PACKAGE_VERSION%"
tar -caf "WinUSB-installer-generator-%PACKAGE_VERSION%".zip "WinUSB-installer-generator-%PACKAGE_VERSION%"
rename package_contents WinUSB-installer-generator-${{ github.ref_name }}
tar -caf WinUSB-installer-generator-${{ github.ref_name }}.zip WinUSB-installer-generator-${{ github.ref_name }}
- name: Release
uses: softprops/action-gh-release@v1
with:
files: |
build/WinUSB-installer-generator-*.zip
generate_and_apply_WinUSB.ps1
128 changes: 128 additions & 0 deletions generate_and_apply_WinUSB.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<#
.SYNOPSIS
This script Apply WinUSB to a USB device

.DESCRIPTION
This script 1. Download WinUSB Installer Generator, 2. Generate WinUSB Installer, and 3. Apply WinUSB to a USB device with user provided vendor id

.PARAMETER VenderId
The vender ID provided by the user.

.EXAMPLE
.\generate_and_apply_WinUSB.ps1 <vendor id>
Prompts the user for a vender ID and displays the entered vender ID.

.NOTES
Author: Fixstars-momoko
Date: February 7, 2024
#>

param (
[Parameter(Mandatory=$true)]
[string]$VenderId
)

# Vender ID of the target USB device
Write-Host "You entered vender ID: $VenderId"

# Working Directory
$installPath = "$env:TEMP"
Write-Host "installPath = $installPath"


# Download
$installerName = "WinUSB-installer-generator"
$repoUrl = "https://api.github.com/repos/Sensing-Dev/$installerName/releases/latest"

$response = Invoke-RestMethod -Uri $repoUrl
$version = $response.tag_name

Write-Host "Latest version: $version"

$Url = "https://github.com/Sensing-Dev/$installerName/releases/download/${version}/${installerName}-${version}.zip"
Write-Verbose "Download from: $Url"

# Extract Item
if ($Url.EndsWith("zip")) {
# Download ZIP to a temp location

$tempZipPath = "${env:TEMP}\${installerName}.zip"
Invoke-WebRequest -Uri $Url -OutFile $tempZipPath -Verbose

Add-Type -AssemblyName System.IO.Compression.FileSystem

$tempExtractionPath = "$installPath\_tempWinUSBExtraction"
# Create the temporary extraction directory if it doesn't exist
if (Test-Path $tempExtractionPath) {
Remove-Item -Path $tempExtractionPath -Force -Recurse -Confirm:$false
}
New-Item -Path $tempExtractionPath -ItemType Directory

# Attempt to extract to the temporary extraction directory
try {
Expand-Archive -Path $tempZipPath -DestinationPath $tempExtractionPath
Get-ChildItem $tempExtractionPath
}
catch {
Write-Error "Extraction failed...."
}
Remove-Item -Path $tempZipPath -Force
}else{
throw "Target archive does not exit."
}

# Generate WinUSB Installer
if (Test-Path $tempExtractionPath) {

# Run Winusb installer
Write-Host "This may take a few minutes. Starting the installation..."

Write-Verbose "Start winUsb installer"
$TempDir = "$tempExtractionPath/temp"

New-item -Path "$TempDir" -ItemType Directory
$winUSBOptions = @{
FilePath = "${tempExtractionPath}/${installerName}-${versionNum}/winusb_installer.exe"
ArgumentList = "$VenderId"
WorkingDirectory = "$TempDir"
Wait = $true
Verb = "RunAs" # This attempts to run the process as an administrator
}
# Start winusb_installer.exe process
Start-Process @winUSBOptions

Write-Verbose "End winUsb installer"
}else{
throw "Failed to execute WinUSB Installer Generator."
}

# Run Driver installer
Write-Verbose "Start Driver installer"

$infPath = "$TempDir/target_device.inf"
if (-not (Test-Path -Path $infPath -PathType Leaf) ){
Write-Error "$infPath does not exist."
}
else{
$pnputilOptions = @{
FilePath = "PNPUtil"
ArgumentList = "-i -a ./target_device.inf"
WorkingDirectory = "$TempDir"
Wait = $true
Verb = "RunAs" # This attempts to run the process as an administrator
}
try {
# Start Pnputil process
Start-Process @pnputilOptions
Write-Host "Sucessfully installed winUSB driver"
}
catch {
Write-Error "An error occurred while running pnputil: $_"
}
}

# delete temp files

Start-Process powershell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"Remove-Item -Path '$tempExtractionPath' -Recurse -Force -Confirm:`$false`""

Write-Verbose "End Driver installer"
Loading