测试路径 returns True 但 ExtractToDirectory 找不到路径

Test-Path returns True but ExtractToDirectory Could not find path

我有一个简单的 powershell 脚本,可以简化一些部署任务。

在脚本的前面部分,我创建了一个映射到 Z: 的虚拟驱动器,该驱动器位于远程服务器上。绊倒的部分是当它试图解压缩映射到 Z 的远程服务器上的文件时:

function UnzipBuild($destinationFolder)
{    

    Add-Type -assembly "System.IO.Compression.Filesystem"    
    $zipFiles = Get-ChildItem -Path $destinationFolder -Filter *.zip
    foreach($zip in $zipFiles)
    {
        $folderName = $zip.ToString().TrimEnd(".zip")
        $extractPath = Join-Path $destinationFolder $folderName              

        New-Item -ItemType Directory $extractPath  
        Write-Host "Extracting $zip to $extractPath `r`n"                    
        [io.compression.zipfile]::ExtractToDirectory([string]$zip.FullName, "$extractPath")        
    }           
}

当它到达 ::ExtractToDirectory 行时抛出异常

Hit Line breakpoint on 'D:\MyDeploymentScript.ps1:85'
[DBG]: PS C:\WINDOWS\system32>> 
Exception calling "ExtractToDirectory" with "2" argument(s): "Could not find a part of the path 
'Z:\Build_11_17_13_28\Web'."
At D:\MyDeploymentScript.ps1:85 char:9
+         [io.compression.zipfile]::ExtractToDirectory([string]$zip.FullName, "$ex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DirectoryNotFoundException

但疯狂的是,如果我在同一个点断点并使用 Test-Path 检查路径,它 returns 正确。我不知道现在可能出了什么问题。

[DBG]: PS C:\WINDOWS\system32>> Test-Path Z:\Build_11_17_13_28\Web
True

您似乎在脚本的前面部分使用 New-PSDrive 映射了您的驱动器。使用该 cmdlet 创建的驱动器仅在 PowerShell 中可见,除非您使用 -Persist 开关。该开关将其创建为实际的映射驱动器(就像您使用了 net use 或组策略或通过资源管理器将其映射一样)。

您正在调用的 .ExtractToDirectory 方法看不到任何 powershell 提供程序命名空间,因此它需要一个对整个操作系统可见的真实映射驱动器或 UNC 路径。

请记住,如果您正在使用 -Persist,您现在可能还想手动取消映射驱动器。