Skip to content

Instantly share code, notes, and snippets.

@shawly
Last active July 1, 2024 18:22
Show Gist options
  • Save shawly/f301446b5e75639f642d76c114cd9446 to your computer and use it in GitHub Desktop.
Save shawly/f301446b5e75639f642d76c114cd9446 to your computer and use it in GitHub Desktop.
Steam ROM Manager Powershell Script for generating manifest.json files
<#
.SYNOPSIS
SRMManifestGenerator - A script to generate manifest.json files for Steam ROM Manager.
.DESCRIPTION
This PowerShell script retrieves all .lnk files in a specified folder, extracts their
properties (target, working directory, and filename without the .lnk extension),
conditionally removes the 'Run as administrator' flag for executable shortcuts,
and generates a manifest.json file. This script has been created because the
Steam overlay does not work with shortcuts.
.NOTES
File Name : SRMManifestGenerator.ps1
Author : ChatGPT and shawly
Date : 01 July 2024
Version : 1.4
.PARAMETER Path
Optional. Specifies the folder path to scan for .lnk files. If not provided,
the script defaults to the directory where the script is located.
.PARAMETER ManifestSuffix
Optional. Specifies the suffix to append to the manifest.json file name.
For example, specifying '-ManifestSuffix games' will produce 'manifest-games.json'.
.PARAMETER OutputPath
Optional. Specifies the output directory for the manifest.json file.
If not provided, the script defaults to the directory where the files are scanned.
.EXAMPLE
.\SRMManifestGenerator.ps1 -Path "C:\Path\To\Shortcuts" -ManifestSuffix games -OutputPath "C:\Path\To\Output"
Generates a manifest-games.json file for all .lnk files in the specified folder and saves it to the specified output path.
.EXAMPLE
.\SRMManifestGenerator.ps1
Generates a manifest.json file for all .lnk files in the script's directory and saves it to the script's directory.
#>
param (
[string]$Path = $PSScriptRoot,
[string]$ManifestSuffix = "",
[string]$OutputPath = $Path
)
# Enable strict mode and error handling
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
# Function to remove "Run as Administrator" compatibility flag from registry
function Remove-RunAsAdminCompatibilityFlag {
param (
[string]$executablePath
)
# Define registry path
$regPath = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
$regEntry = Get-ItemProperty -Path $regPath -Name $executablePath -ErrorAction SilentlyContinue
# Check if the registry entry exists
if ($regEntry) {
$flagValue = $regEntry.$executablePath
# Remove "RUNASADMIN" flags from the entry
if ($flagValue -match "RUNASADMIN") {
$newFlagValue = $flagValue -replace "RUNASADMIN", ""
$newFlagValue = $newFlagValue -replace "\s\s+", " " # Clean up any double spaces
$newFlagValue = $newFlagValue.Trim()
# Update or remove the registry entry
if ($newFlagValue -eq "" -or $newFlagValue -eq "~") {
Remove-ItemProperty -Path $regPath -Name $executablePath
Write-Host "Removed registry entry for $executablePath" -ForegroundColor Green
} else {
Set-ItemProperty -Path $regPath -Name $executablePath -Value $newFlagValue
Write-Host "Updated registry entry for $executablePath to remove RUNASADMIN" -ForegroundColor Green
}
}
}
}
# Function to get shortcut properties
function Get-ShortcutDetails {
param (
[string]$shortcutPath
)
Write-Debug "Processing shortcut: $shortcutPath"
# Create a COM object to interact with the shortcut
$wsh = New-Object -ComObject WScript.Shell
$shortcut = $wsh.CreateShortcut($shortcutPath)
# Retrieve properties
$details = [PSCustomObject]@{
title = [System.IO.Path]::GetFileNameWithoutExtension($shortcutPath)
target = $shortcut.TargetPath
startIn = $shortcut.WorkingDirectory
}
Write-Debug "Retrieved details: title=$($details.title), target=$($details.target), startIn=$($details.startIn)"
if ($shortcut.Arguments -ne "") {
$details | Add-Member -MemberType NoteProperty -Name launchOptions -Value $shortcut.Arguments
Write-Debug "Found launch options: $($details.launchOptions)"
}
# Check if the target is an executable
if ((Test-Path $details.target -PathType Leaf) -and ([System.IO.Path]::GetExtension($details.target) -eq ".exe")) {
Write-Debug "Target is executable: $($details.target)"
# Remove "Run as Administrator" compatibility flag
Remove-RunAsAdminCompatibilityFlag -executablePath $details.target
}
Write-Host "Adding `"$($details.title)`" to manifest..." -ForegroundColor Yellow
return $details
}
Write-Host "Starting SRMManifestGenerator with Path=$Path, ManifestSuffix=$ManifestSuffix, OutputPath=$OutputPath" -ForegroundColor Gray
# Use `Get-ChildItem` to retrieve all .lnk files in the folder
$shortcutFiles = Get-ChildItem -Path $Path -Filter *.lnk
Write-Host "Found $($shortcutFiles.Count) shortcut(s) in the specified path." -ForegroundColor Yellow
# Prepare an array to hold the shortcut details
$shortcutDetails = @()
# Iterate over each shortcut file and get its details
foreach ($file in $shortcutFiles) {
$details = Get-ShortcutDetails -shortcutPath $file.FullName
$shortcutDetails += $details
}
# Convert the details array to JSON format
$jsonOutput = $shortcutDetails | ConvertTo-Json -Depth 3
# Construct the output manifest file name
$manifestFileName = if ($ManifestSuffix -ne "") { "manifest-$ManifestSuffix.json" } else { "manifest.json" }
# Define the output JSON file path
$outputFilePath = Join-Path -Path $OutputPath -ChildPath $manifestFileName
# Write the JSON output to the file
$jsonOutput | Out-File -FilePath $outputFilePath -Encoding utf8
Write-Host "Manifest file created successfully at $outputFilePath" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment