다음을 통해 공유


스크립트: .msu 및 .cab 파일 추출

이 문서에서는 PowerShell 스크립트를 사용하여 지정된 디렉터리에 파일을 추출하고 .cab 파일을 추출 .msu 하는 방법에 대한 가이드를 제공합니다. 스크립트는 다양한 시나리오를 처리하고, 원활한 추출을 보장하며, 필요한 경우 디렉터리를 만들도록 설계되었습니다.

스크립트 개요

스크립트에는 두 가지 필수 매개 변수가 필요합니다.

  • 또는 .cab 파일의 .msu 파일 경로
  • 추출된 파일이 저장될 대상 경로입니다.

스크립트는 지정된 파일 및 대상 디렉터리가 있는지 확인하고, 없는 경우 대상 디렉터리를 만듭니다. 그런 다음 스크립트가 진행하여 또는 .cab 파일의 .msu 내용을 추출하고 프로세스에서 중첩된 .cab 파일을 처리합니다.

스크립트를 사용하기 위한 단계별 지침

  1. 스크립트를 준비합니다.

    제공된 PowerShell 스크립트를 .ps1 파일(예 : Extract-MSUAndCAB.ps1)에 복사합니다.

  2. 스크립트를 실행합니다.

    관리자 권한으로 PowerShell을 엽니다. 스크립트가 저장된 디렉터리로 이동합니다. 이 예제에서는 을 실행하여 스크립트를 실행 .\Extract-MSUAndCAB.ps1합니다.

  3. 파일 경로를 제공합니다.

    메시지가 표시되면 추출할 파일의 .msu .cab 경로를 제공합니다. 추출된 파일을 저장할 대상 경로를 지정합니다.

사용 예는 다음과 같습니다.

powershell -ExecutionPolicy Bypass -File .\Extract-MSUAndCAB.ps1 -filePath "C:\<path>\<yourfile>.msu" -destinationPath "C:\<path>\<destination>"

Important

이 샘플 스크립트는 Microsoft 표준 지원 프로그램 또는 서비스에서 지원되지 않습니다.

샘플 스크립트는 어떤 종류의 보증도 없이 AS IS로 제공됩니다. Microsoft는 특정 목적에 대한 상품성 또는 적합성에 대한 묵시적 보증을 포함하여 모든 묵시적 보증을 추가로 부인합니다.

샘플 스크립트 및 설명서의 사용 또는 성능에서 발생하는 전체 위험은 여전히 유지됩니다. 어떠한 경우에도 Microsoft, 해당 작성자 또는 스크립트의 생성, 제작 또는 전달에 관련된 모든 사람은 샘플 스크립트 또는 설명서를 사용하거나 사용할 수 없는 경우 발생하는 모든 손해(비즈니스 이익 손실, 비즈니스 중단, 비즈니스 정보 손실 또는 기타 금전적 손실 포함)에 대해 책임을 지지 않습니다. Microsoft가 이러한 손해의 가능성을 통보 받은 경우에도

param (
    [Parameter(Mandatory = $true)]
    [string]$filePath,
    [Parameter(Mandatory = $true)]
    [string]$destinationPath
)

# Display the note to the user
Write-Host "==========================="
Write-Host
Write-Host -ForegroundColor Yellow "Note: Do not close any Windows opened by this script until it is completed."
Write-Host
Write-Host "==========================="
Write-Host


# Remove quotes if present
$filePath = $filePath -replace '"', ''
$destinationPath = $destinationPath -replace '"', ''

# Trim trailing backslash if present
$destinationPath = $destinationPath.TrimEnd('\')

if (-not (Test-Path $filePath -PathType Leaf)) {
    Write-Host "The specified file does not exist: $filePath"
    return
}

if (-not (Test-Path $destinationPath -PathType Container)) {
    Write-Host "Creating destination directory: $destinationPath"
    New-Item -Path $destinationPath -ItemType Directory | Out-Null
}

$processedFiles = @{}

function Extract-File ($file, $destination) {
    Write-Host "Extracting $file to $destination"
    Start-Process -FilePath "cmd.exe" -ArgumentList "/c expand.exe `"$file`" -f:* `"$destination`" > nul 2>&1" -Wait -WindowStyle Hidden | Out-Null
    $processedFiles[$file] = $true
    Write-Host "Extraction completed for $file"
}

function Rename-File ($file) {
    if (Test-Path -Path $file) {
        $newName = [System.IO.Path]::GetFileNameWithoutExtension($file) + "_" + [System.Guid]::NewGuid().ToString("N") + [System.IO.Path]::GetExtension($file)
        $newPath = Join-Path -Path ([System.IO.Path]::GetDirectoryName($file)) -ChildPath $newName
        Write-Host "Renaming $file to $newPath"
        Rename-Item -Path $file -NewName $newPath
        Write-Host "Renamed $file to $newPath"
        return $newPath
    }
    Write-Host "File $file does not exist for renaming"
    return $null
}

function Process-CabFiles ($directory) {
    while ($true) {
        $cabFiles = Get-ChildItem -Path $directory -Filter "*.cab" -File | Where-Object { -not $processedFiles[$_.FullName] -and $_.Name -ne "wsusscan.cab" }

        if ($cabFiles.Count -eq 0) {
            Write-Host "No more CAB files found in $directory"
            break
        }

        foreach ($cabFile in $cabFiles) {
            Write-Host "Processing CAB file $($cabFile.FullName)"
            $cabFilePath = Rename-File -file $cabFile.FullName

            if ($cabFilePath -ne $null) {
                Extract-File -file $cabFilePath -destination $directory
                Process-CabFiles -directory $directory
            }
        }
    }
}

try {
    # Initial extraction
    if ($filePath.EndsWith(".msu")) {
        Write-Host "Extracting .msu file to: $destinationPath"
        Extract-File -file $filePath -destination $destinationPath
    } elseif ($filePath.EndsWith(".cab")) {
        Write-Host "Extracting .cab file to: $destinationPath"
        Extract-File -file $filePath -destination $destinationPath
    } else {
        Write-Host "The specified file is not a .msu or .cab file: $filePath"
        return
    }

    # Process all .cab files recursively
    Write-Host "Starting to process CAB files in $destinationPath"
    Process-CabFiles -directory $destinationPath
}
catch {
    Write-Host "An error occurred while extracting the file. Error: $_"
    return
}

Write-Host "Extraction completed. Files are located in $destinationPath"
return $destinationPath