执行此代码时,我收到以下错误:
$filesExist = Test-Path($file) if ($filesExist) { $shell_app=new-object -com shell.application $zip_file = Get-Item "$mindCrackFolder\files.zip" $destination = Get-Item $mindCrackFolder $destination.Copyhere($zip_file.items(), 0x14) #Remove-Item "$zip_file" #Remove-Item "install.ps1" }
错误:
Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'items'. At C:\Users\User1\Desktop\ps install\install.ps1:33 char:5 + $destination.Copyhere($zip_file.items(), 0x14) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
但我已经转换$目标到一个IO对象被操纵? 我可以得到任何帮助,这是我第一次尝试PS。
这与$目的地没有任何关系。 首先计算$zip_file.items()
,并且错误消息告诉您由Get-Item
返回的.NET System.IO.FileInfo对象没有Items()
方法。 Get-Item
只返回提供关于文件大小,最后写入时间,是否只读等信息的对象。不能使用Get-Item来访问ZIP文件的内容。
如果您需要提取ZIP文件的内容,请考虑使用PowerShell Community Exensions的 Expand-Archive
cmdlet。
错误是关于你在$zip_file
上使用items()
方法的对象。 Powershell没有内置的zip支持,你必须创建它(使用shell.application COM对象,谷歌它)或添加.net库。 $zip_file
只是一个简单的FileInfo对象,就像从dir
(Get-ChildItem)获得的对象一样。 它不包含items()
方法。
解决方案:如前所述,谷歌powershell zip files
以了解如何在PowerShell中使用压缩文件。 我的建议是DotNetZip
我不知道你从哪里学到了这些东西,但是你似乎错误地复制了一些代码,并且特意做了修改。 尝试下面的脚本,你想要什么:
$shell_app=new-object -com shell.application $filename = "test.zip" $zip_file = $shell_app.namespace((Get-Location).Path + "\$filename") $destination = $shell_app.namespace((Get-Location).Path) $destination.Copyhere($zip_file.items(), 0x14)
items
和copyHere
方法在FileInfo
对象上不可用,这是您从Get-Item
。 如上所示使用它们。