我有一个用于压缩文件的powershell脚本,我遇到了一些问题。我的脚本的简化版本是:
function MyFunction([__ComObject] $zipFile)
{
if(test-path($zipFile.Self.Path))
{
"Zip path found in MyFunction: " + $zipFile.Self.Path
}
else
{
"Zip path not found in MyFunction: " + $zipFile.Self.Path
}
# Check path directly
if(test-path("C:\zips\july2013.zip"))
{
"Direct zip path found in MyFunction: " + $zipFile.Self.Path
}
else
{
"Direct zip path not found in MyFunction: " + $zipFile.Self.Path
}
}
$zipfilepath = "C:\zips\july2013.zip"
if(-not (test-path($zipfilepath)))
{
set-content $zipfilepath ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilepath).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilepath)
if( (test-path($zipPackage.Self.Path)))
{
"Zip exists: " + $zipPackage.Self.Path
}
else
{
"Zip does not exist: " + $zipPackage.Self.Path
}
MyFunction $zipPackage
如果在我运行脚本之前存在C:\ zips \ july2013.zip,或者我通过打开powershell来调试我得到(正如我所期望的那样):
Zip exists: C:\zips\july2013.zip
Zip path found in MyFunction: C:\zips\july2013.zip
Direct zip path found in MyFunction: C:\zips\july2013.zip
但是如果文件不存在C:\ zips \ july2013.zip并且我在没有打开powershell的情况下运行它(即右键单击“使用powershell运行”)我得到:
Zip exists: C:\zips\july2013.zip
Zip path not found in MyFunction: C:\zips\july2013.zip
Direct zip path not found in MyFunction: C:\zips\july2013.zip
我是PowerShell的新手,任何想法是什么?