diff --git a/packages/common.vm/common.vm.nuspec b/packages/common.vm/common.vm.nuspec index 9325d71e7..62fa0fbd8 100755 --- a/packages/common.vm/common.vm.nuspec +++ b/packages/common.vm/common.vm.nuspec @@ -2,7 +2,7 @@ common.vm - 0.0.0.20231020 + 0.0.0.20231027 Common libraries for VM-packages Mandiant diff --git a/packages/common.vm/tools/vm.common/vm.common.psm1 b/packages/common.vm/tools/vm.common/vm.common.psm1 index 31b04f05b..c865b722e 100755 --- a/packages/common.vm/tools/vm.common/vm.common.psm1 +++ b/packages/common.vm/tools/vm.common/vm.common.psm1 @@ -207,47 +207,23 @@ function VM-Install-Raw-GitHub-Repo { # Remove files from previous zips for upgrade VM-Remove-PreviousZipPackage ${Env:chocolateyPackageFolder} - # Create a temp directory to download zip - $tempDownloadDir = Join-Path ${Env:chocolateyPackageFolder} "temp_$([guid]::NewGuid())" - # Download and unzip $packageArgs = @{ packageName = ${Env:ChocolateyPackageName} - unzipLocation = $tempDownloadDir + unzipLocation = $toolDir url = $zipUrl checksum = $zipSha256 checksumType = 'sha256' } Install-ChocolateyZipPackage @packageArgs | Out-Null - VM-Assert-Path $tempDownloadDir + VM-Assert-Path $toolDir - # Make sure our tool directory exists - if (-Not (Test-Path $toolDir)) { - New-Item -Path $toolDir -ItemType Directory -Force | Out-Null + # GitHub ZIP files typically unzip to a single folder that contains the tools. + $dirList = Get-ChildItem $toolDir -Directory + if ($dirList.Count -eq 1) { + $toolDir = Join-Path $toolDir $dirList[0].Name -Resolve } - # Get the unzipped directory - $unzippedDir = (Get-ChildItem -Directory $tempDownloadDir | Where-Object {$_.PSIsContainer} | Select-Object -f 1).FullName - - # Copy all the items in the unzipped directory to their correct directory - Get-ChildItem -Path $unzippedDir | Move-Item -Destination $toolDir -Force -ea 0 - - # Sleep to help prevent file system race conditions - Start-Sleep -Milliseconds 500 - - # Rename all entries in to where the files were actually copied - $zip_name = Join-Path ${Env:chocolateyPackageFolder} ((Split-Path $unzippedDir -Leaf) + ".zip.txt") - (Get-Content $zip_name) | Foreach-Object {$_.Replace($unzippedDir, $toolDir)} | Set-Content $zip_name - - # Remove first line of *.zip.txt file so we don't delete the entire directory on upgrade - (Get-Content $zip_name | Select-Object -Skip 1) | Set-Content $zip_name - - # Sleep to help prevent file system race conditions - Start-Sleep -Milliseconds 500 - - # Attempt to remove temporary directory - Remove-Item $tempDownloadDir -Recurse -Force -ea 0 - if ($powershellCommand) { $executableArgs = "-ExecutionPolicy Bypass -NoExit -Command $powershellCommand" $powershellPath = Join-Path "${Env:WinDir}\system32\WindowsPowerShell\v1.0" "powershell.exe" -Resolve @@ -329,31 +305,18 @@ function VM-Install-From-Zip { [Parameter(Mandatory=$false)] [string] $executableName, # Executable name, needed if different from "$toolName.exe" [Parameter(Mandatory=$false)] - [switch] $withoutBinFile, # Tool should not be installed as a bin file - [Parameter(Mandatory=$false)] - [string] $unzipLocation + [switch] $withoutBinFile # Tool should not be installed as a bin file ) try { - if ($unzipLocation) { - $toolDir = Join-Path $unzipLocation $toolName - } else { - $toolDir = Join-Path ${Env:RAW_TOOLS_DIR} $toolName - $unzipLocation = $toolDir - } + $toolDir = Join-Path ${Env:RAW_TOOLS_DIR} $toolName # Remove files from previous zips for upgrade VM-Remove-PreviousZipPackage ${Env:chocolateyPackageFolder} - # Snapshot all folders in $toolDir - $oldDirList = @() - if (Test-Path $toolDir) { - $oldDirList = @(Get-ChildItem $toolDir | Where-Object {$_.PSIsContainer}) - } - # Download and unzip $packageArgs = @{ packageName = ${Env:ChocolateyPackageName} - unzipLocation = $unzipLocation + unzipLocation = $toolDir url = $zipUrl checksum = $zipSha256 checksumType = 'sha256' @@ -363,23 +326,12 @@ function VM-Install-From-Zip { Install-ChocolateyZipPackage @packageArgs VM-Assert-Path $toolDir - # Diff and find new folders in $toolDir - $newDirList = @(Get-ChildItem $toolDir | Where-Object {$_.PSIsContainer}) - $diffDirs = Compare-Object -ReferenceObject $oldDirList -DifferenceObject $newDirList -PassThru - # If $innerFolder is set to $true, after unzipping only a single folder should be new. + # If $innerFolder is set to $true, after unzipping there should be only one folder # GitHub ZIP files typically unzip to a single folder that contains the tools. if ($innerFolder) { - # First time install, use the single resulting folder name from Install-ChocolateyZipPackage. - if ($diffDirs.Count -eq 1) { - # Save the "new tool directory" to assist with upgrading. - $newToolDir = Join-Path $toolDir $diffDirs[0].Name -Resolve - Set-Content (Join-Path ${Env:chocolateyPackageFolder} "innerFolder.txt") $newToolDir - $toolDir = $newToolDir - } else { - # On upgrade there may be no new directory, in this case retrieve previous "new tool directory" from saved file. - $toolDir = Get-Content (Join-Path ${Env:chocolateyPackageFolder} "innerFolder.txt") - } + $dirList = Get-ChildItem $toolDir -Directory + $toolDir = Join-Path $toolDir $dirList[0].Name -Resolve } if (-Not $executableName) { $executableName = "$toolName.exe" }