0

我有以下 .ps1 用于解压缩 zip 文件...

param([string]$path)
$shell=new-object -com shell.application
$Location=$shell.namespace($path)
$ZipFiles = get-childitem *.zip

get-childitem $path -include *.xml -recurse | foreach ($_) {remove-item $_.fullname}

foreach ($ZipFile in $ZipFiles)
{
    $ZipFile.fullname | out-default
    $ZipFolder = $shell.namespace($ZipFile.fullname)
    $Location.Copyhere($ZipFolder.items())
}

以及以下用于设置参数和调用 .ps1 的 run.bat 文件

 powershell -command "C:\Users\eric\unzipFile\unzip3.ps1 -path \"C:\Users\eric\unzipFile\""

如果两者都在同一个目录中,则没有错误,但是如果我将 run.bat 移动到另一个目录,我会得到以下...

You cannot call a method on a null-valued expression.
At C:\Users\eric\unzipFile\unzip3.ps1:12 char:38
+ $Location.Copyhere($ZipFolder.items <<<< ())
+ CategoryInfo: InvalidOperation: (items:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
4

1 回答 1

2

您没有指定在哪个路径中查找 zip 文件,因此它在当前文件夹中查找。更改如下:

$ZipFiles = get-childitem -Path $path -Filter *.zip

其他提示:使用powershell中的-File参数并添加其他参数,以便更容易调用

powershell -file "C:\Users\eric\unzipFile\unzip3.ps1" -path "C:\Users\eric\unzipFile\"
于 2013-05-21T20:49:22.433 回答