Skip to content

Commit

Permalink
Add Open With File Association
Browse files Browse the repository at this point in the history
  • Loading branch information
emtuls committed Apr 26, 2024
1 parent 83d9c0c commit 6f799eb
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/7zip-15-05.vm/7zip-15-05.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>7zip-15-05.vm</id>
<version>15.05.0.20240308</version>
<version>15.05.0.20240425</version>
<authors>Igor Pavlov</authors>
<description>7-Zip file archiver. This version is able to extract NSIS scripts.</description>
<dependencies>
<dependency id="common.vm" version="0.0.0.20230926" />
<dependency id="common.vm" version="0.0.0.20240425" />
</dependencies>
</metadata>
</package>
2 changes: 2 additions & 0 deletions packages/7zip-15-05.vm/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Import-Module vm.common -Force -DisableNameChecking
try {
$toolName = '7z'
$category = 'Productivity Tools'

$url = 'https://sourceforge.net/projects/sevenzip/files/7-Zip/15.05/7z1505.exe/download'
$checksum = 'fa99d29283d9a6c501b70d2755cd06cf5bc3dd8e48acc73926b6e0f389885120'
$url64 = 'https://sourceforge.net/projects/sevenzip/files/7-Zip/15.05/7z1505-x64.exe/download'
Expand Down Expand Up @@ -34,6 +35,7 @@ try {
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Add-To-Right-Click-Menu $toolName 'unzip "infected"' "`"$7zExecutablePath`" e -pinfected `"%1`"" "$executablePath" -extension $extension
VM-Set-Open-With-Association $executablePath $extension
}
} catch {
VM-Write-Log-Exception $_
Expand Down
1 change: 1 addition & 0 deletions packages/7zip-15-05.vm/tools/chocolateyuninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ VM-Uninstall-With-Uninstaller "7-Zip 15.05*" "EXE" "/S"
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Remove-From-Right-Click-Menu $toolName -extension $extension
VM-Remove-Open-With-Association "${toolName}FM" -extension $extension
}
4 changes: 2 additions & 2 deletions packages/7zip-nsis.vm/7zip-nsis.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>7zip-nsis.vm</id>
<version>23.01</version>
<version>23.01.0.20240425</version>
<authors>myfreeer</authors>
<description>7-zip build with nsis script decompiling</description>
<dependencies>
<dependency id="common.vm" />
<dependency id="common.vm" version="0.0.0.20240425" />
</dependencies>
</metadata>
</package>
2 changes: 1 addition & 1 deletion packages/7zip-nsis.vm/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ try {

# Make shortcut point to 7z File Manager so that it's more useful of a shortcut.
$executablePath = Join-Path $toolDir "${toolName}FM.exe" -Resolve

VM-Install-Shortcut $toolName $category $executablePath

# Add 7z unzip with password "infected" to the right menu for the most common extensions.
# 7z can unzip other file extensions like .docx but these don't likely use the infected password.
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Add-To-Right-Click-Menu $toolName 'unzip "infected"' "`"$7zExecutablePath`" e -pinfected `"%1`"" "$executablePath" -extension $extension
VM-Set-Open-With-Association $executablePath $extension
}
} catch {
VM-Write-Log-Exception $_
Expand Down
1 change: 1 addition & 0 deletions packages/7zip-nsis.vm/tools/chocolateyuninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Uninstall-BinFile -Name $toolName
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Remove-From-Right-Click-Menu $toolName -extension $extension
VM-Remove-Open-With-Association "${toolName}FM" -extension $extension
}
4 changes: 2 additions & 2 deletions packages/7zip.vm/7zip.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>7zip.vm</id>
<version>0.0.0.20240410</version>
<version>0.0.0.20240425</version>
<description>Metapackage for 7zip to ensure all packages use the same 7zip version.</description>
<authors>Mandiant</authors>
<dependencies>
<dependency id="common.vm" />
<dependency id="common.vm" version="0.0.0.20240425"/>
<dependency id="7zip-nsis.vm" />
</dependencies>
</metadata>
Expand Down
2 changes: 1 addition & 1 deletion packages/common.vm/common.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>common.vm</id>
<version>0.0.0.20240424</version>
<version>0.0.0.20240425</version>
<description>Common libraries for VM-packages</description>
<authors>Mandiant</authors>
</metadata>
Expand Down
76 changes: 76 additions & 0 deletions packages/common.vm/tools/vm.common/vm.common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,82 @@ function VM-Remove-From-Right-Click-Menu {
}
}

# Add associations to the file extension key
function VM-Set-Open-With-Association {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $executablePath,
[Parameter(Mandatory = $true)]
[string] $extension
)
try {
# Extract the executable name without path or extension
$exeName = [System.IO.Path]::GetFileNameWithoutExtension($executablePath)

ForEach ($hive in @("HKCU:", "HKLM:")) {
# Create the 'command' key and its default value
$commandKey = "${hive}\Software\Classes\${exeName}_auto_file\shell\open\command"
New-Item -Path $commandKey -Force
New-ItemProperty -Path $commandKey -Name '(Default)' -Value "`"$executablePath`" `"%1`""

# Create/update the file extension key
$extKey = "${hive}\Software\Classes\$extension"
New-Item -Path $extKey -Force
New-ItemProperty -Path $extKey -Name '(Default)' -Value "${exeName}_auto_file"

# Add to OpenWithProgids for visibility in "Open with..." menu
if ((Get-ItemProperty -Path $extKey -Name 'OpenWithProgids' -ErrorAction Ignore)) {
# If OpenWithProgids exists, update it
$existingProgIds = (Get-ItemProperty -Path $extKey -Name 'OpenWithProgids').OpenWithProgids
$newProgIds = "$existingProgIds ${exeName}_auto_file" | Select-Object -Unique # Ensure unique values
Set-ItemProperty -Path $extKey -Name 'OpenWithProgids' -Value $newProgIds -PropertyType ExpandString
} else {
# If OpenWithProgids doesn't exist, create it
New-ItemProperty -Path $extKey -Name 'OpenWithProgids' -Value "${exeName}_auto_file" -PropertyType ExpandString
}
}
} catch {
VM-Write-Log "ERROR" "Failed to add $exeName as file association. Error: $_"
}
}

# Remove associations from the file extension key
function VM-Remove-Open-With-Association {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $exeName,
[Parameter(Mandatory = $true)]
[string] $extension
)
ForEach ($hive in @("HKCU:", "HKLM:")) {
$extKey = "${hive}\Software\Classes\$extension"

# Check if the key exists before attempting removal
if (Test-Path $extKey) {
$expectedDefault = "${exeName}_auto_file"

# Remove the default value if it matches
$currentDefault = Get-ItemPropertyValue -Path $extKey -Name '(Default)' -ErrorAction SilentlyContinue
if ($currentDefault -and $currentDefault -eq $expectedDefault) {
New-ItemProperty -Path $extKey -Name '(Default)' -Value "" | Out-Null
}

# Remove from OpenWithProgids if present
if ((Get-ItemProperty -Path $extKey -Name 'OpenWithProgids' -ErrorAction Ignore).OpenWithProgids -contains $expectedDefault) {
$newProgIds = (Get-ItemProperty -Path $extKey -Name 'OpenWithProgids').OpenWithProgids -split ' ' | Where-Object { $_ -ne $expectedDefault }
Set-ItemProperty -Path $extKey -Name 'OpenWithProgids' -Value ($newProgIds -join ' ')
}
}

# Remove the 'command' key and the auto_file key
$commandKey = "${hive}\Software\Classes\${exeName}_auto_file\shell\open\command"
$autoFileKey = "${hive}\Software\Classes\${exeName}_auto_file"
Remove-Item -Path $commandKey,$autoFileKey -Recurse -ErrorAction SilentlyContinue
}
}

function VM-Get-Host-Info {
$survey = @"
Host Information
Expand Down

0 comments on commit 6f799eb

Please sign in to comment.