Powershell脚本gci访问奇怪的文件夹

时间:2013-04-18 01:32:10

标签: powershell

我有以下脚本通常运行正常,它只是查找任何解压缩的文件,压缩它,然后删除它。

[string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe";
$items= gci -exclude *.zip E:\backup\MYFOLDER | where {!$_.PsIsContainer} 

# loop through the files
foreach ($file in $items) { 
 # run only if there is any file in the directory
 if ($items.count) {
     [Array]$arguments = "a", "$file.zip", $file; 
     # zip the file
     &$pathToZipExe $arguments;
     # delete the file ONLY if the compression succeeds
     if($?){        
        Remove-Item -Force $file;
     } else {
        # later this should be changed to send an email            
        write-host "Error: The file could NOT be deleted."
     };
 };
 };

通常它运行正常并返回如下输出:

..
..
7-Zip [64] 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Scanning
Creating archive E:\backup\MYFOLDER\ARC_T813021320_S536_P1_C1.zip
Compressing  ARC_T813021320_S536_P1_C1
Everything is Ok

7-Zip [64] 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Scanning

Creating archive E:\backup\MYFOLDER\ARC_T813021387_S537_P1_C1.zip
Compressing  ARC_T813021387_S537_P1_C1
Everything is Ok

..
..

但最近它开始以奇怪的方式表现并报告错误输出,如下所示:

..
..
7-Zip [64] 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03

Scanning

LogFiles\WMI\RtBackup\:  WARNING: Access is denied.


Updating archive .zip.zip

Compressing  .zip.zip
Compressing  0401\rusb3co.dll.mui
Compressing  0404\rusb3co.dll.mui
Compressing  0405\rusb3co.dll.mui
Compressing  0406\rusb3co.dll.mui
Compressing  0407\rusb3co.dll.mui
Compressing  0408\rusb3co.dll.mui
Compressing  0409\iisext.ini
Compressing  0409\IIsScHlp.ini
Compressing  0409\rusb3co.dll.mui
Compressing  040A\rusb3co.dll.mui
Compressing  040B\rusb3co.dll.mui

.. <massive files>

----------------
WARNING: Cannot open 157 files
Error: The file could NOT be deleted.

..
..

我的代码出错了。 干杯!

0 个答案:

没有答案