使用PowerShell脚本从.zip文件中提取特定文件

使用PowerShell脚本从.zip文件中提取特定文件,powershell,zipfile,cmdlets,Powershell,Zipfile,Cmdlets,我目前有一个脚本,用于从一些文件夹中提取特定的.xml文件。但是,我需要对这些文件夹中的zip文件执行相同的操作,而扩展存档cmdlet无法完全执行我需要执行的操作。有人能提供帮助吗?这是我的原稿: param ( [string]$loannumber, [string]$mids ) $ErrorActionPreference = "Continue" $MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.M

我目前有一个脚本,用于从一些文件夹中提取特定的.xml文件。但是,我需要对这些文件夹中的zip文件执行相同的操作,而
扩展存档
cmdlet无法完全执行我需要执行的操作。有人能提供帮助吗?这是我的原稿:

param (
    [string]$loannumber,
    [string]$mids
)

$ErrorActionPreference = "Continue"

$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$config = [xml][IO.File]::ReadAllText("$MyDir\MessageGatherConfig.xml")

#$loannumber = '1479156692'
#$mids = 'M1,M2,DUREQ'

$selectedmids = $mids.Split(",")

foreach ($mid in $selectedmids) {
    $filestocopy = @()
    Write-Host "Checking for $mid messages..."

    $midfile = ($config.MessageGatherConfig.MessageFilePatterns.FilePattern | Where-Object {$_.messageid -eq $mid})
    $pattern = $midfile.pattern

    $copyfiles = $false

    foreach ($path in $midfile.Path) {

        $searchval = $pattern.Replace("LOANNUMBER", $loannumber)

        Write-Host "Searching $path for $searchval"

        $dircmd = "dir /b $path\$searchval"
        $files = ""

        $files = cmd.exe /c $dircmd

        if ($files -ne $null) {
            $copyfiles = $true
            $files = $files.replace('[', '`[')
            $files = $files.replace(']', '`]')

            $files2 = $files.Split([Environment]::NewLine)

            foreach ($filename in $files2) {
                $filestocopy += "$path\$filename"
            }
        }       

    }

    if ($copyfiles) {
        Write-Host "Copying $mid files to local folder"

        if (Test-Path $MyDir\$loannumber\$mid) {
            Remove-Item $MyDir\$loannumber\$mid -Force -Recurse 
        }

        New-Item $MyDir\$loannumber\$mid -type directory
    }
}

我将使用一种方法将zip解压缩到临时目录,然后使用上面的函数复制您需要的文件。这个简单的函数将把内容提取到临时目录,并返回提取内容的路径

function extractZipToTemp () {
    Param (
        $ZipFilePath
    )

    # Generate the path to extract the ZIP file content to.
    $extractedContentPath = "$([System.IO.Path]::GetTempPath())$(([guid]::NewGuid()).tostring())"
    # Extract the ZIP file content.
    Expand-Archive -Path $ZipFilePath -DestinationPath $extractedContentPath -Force
    # Return the path to the extracted content.
    return $extractedContentPath
}

如果希望内容保持在执行脚本所在目录的本地,只需按如下方式调整上述函数

function extractZipToTemp () {
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [String]$ZipFilePath,

        [Parameter(Mandatory = $true, Position = 1)]
        [String]$ExtractPath
    )

    # Generate the path to extract the ZIP file content to.
    $extractedContentPath = "$extractPath\$($ZipFilePath | Split-Path -Leaf)"
    # Extract the ZIP file content.
    Expand-Archive -Path $ZipFilePath -DestinationPath $extractedContentPath -Force
    # Return the path to the extracted content.
    return $extractedContentPath
}


使用上述任一方法,请记住在复制所需文件后进行清理。

屏幕截图没有帮助。请粘贴一个您正在使用的代码示例,并告诉它如何不工作(错误消息等)。我刚刚发布了我的整个原始脚本。并不是说它不起作用。我只需要它也能用于zip文件,我不确定如何做到这一点。“也能用于zip文件”是什么意思?从文件夹中提取特定文件时,我的脚本已经起作用了。这些文件夹都有zip文件,我还需要从中提取特定的文件。我正在寻求帮助来编辑我的脚本以实现这一点。为什么
Expand Archive
对您不起作用?我将尝试一下。谢谢,先生,没问题。如果你需要更多的帮助,请告诉我。