首页 > 解决方案 > 从 PowerShell 将布尔值传递给 .net 函数

问题描述

我需要调用该方法ZipFile.ExtractToDirectory以在 PowerShell 中提取 zip 文件。当我尝试使用以下方法调用它时:[System.IO.Compression.ZipFile]::ExtractToDirectory($source, $destinationPath, $True)

我收到一条错误消息Cannot convert argument "entryNameEncoding", with value: "True", for "ExtractToDirectory" to type "System.Text.Encoding": "Cannot convert value "True" to type "System.Text.Encoding". Error: "Invalid cast from 'System.Boolean' to 'System.Text.Encoding'.""

我也尝试使用创建一个新的布尔变量[Boolean]$b = $true;,然后传递它,但它仍然给出相同的错误。如果目标中存在文件,我如何传递true值以覆盖文件?

Note:我无法使用Expand-Archivecmdlet,因为它仅受 PS V5.0 支持。我需要我的脚本在 PS4.1 上运行

如果文件夹存在,我也可以删除它然后提取,但想使用现有的。

标签: powershellpowershell-4.0

解决方案


推荐阅读