完全披露,我是使用Powershell的新手,我正在开发一个解决方案,测试38个excel报告是否位于某个位置并在其末尾格式化了特定日期(例如Excel-Advanzia- Outages 2015年9月18日),将它们作为备份复制到另一个文件夹,在另一个excel工作簿中运行宏(根据日期将报告分类到子文件夹中),最后从原始文件夹中删除38个报告。
我从其中一个报告中更改/删除了日期,以便它会返回false以测试会发生什么,但它仍然在尝试执行IF语句中的代码。如果所有报告的格式都正确,那么一切似乎都有效。
我希望听到一些关于如何修复错误的反馈,或者是否有更好,更动态的方式对此进行编码。谢谢!
到目前为止,这是我的代码:
# Declares variable $a as today's date and will add 2 days in "dd MMM yyyy" formatting (Adding 2 days because this will be set up to run on Wednesday and checks for files that have Friday's date)
$a = "{0:dd MMM yyyy}" -f (get-date).AddDays(2)
# Verifies each Excel file is correctly formatted with upcoming Friday's date formatted as "dd MMM yyyy" at end of file. If true, code inside if statement is executed.
If ((test-path "C:\Users\mathill\Desktop\Outages\Excel-Advanzia-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-AIB-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-American-Express-Corp-Srvcs-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Amex-Central-Europe-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Arcot-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-B+S-Card-Service $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Bank-of-America-Asia-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Bank-of-America-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Barclays-Commercial-Card-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-BOI-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-C-Card-S-p-A-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-CCUK Finance-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-CMC-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-COBE-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Degussa-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Deutsche-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Elavon-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Home-Retail-Group-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-IE-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-ING-CPS-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-JPMC-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-KBC-Bank-Ireland-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Lloyds-TSB-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Metro-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-NBS-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-PayPal-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Prime-Carrefour-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-PTSB-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Rabo-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-RBSG-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-RSA-Cyota-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Swisscard-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Telrock-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Tesco-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-TSYS-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Virgin-Money-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Wells-Fargo-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-WEX-Outages $a.xls") -eq $true)
{
# Deletes all files in "C:\Users\mathill\Desktop\Outages\Current (Pre-Wrap)"
Remove-Item "C:\Users\mathill\Desktop\Outages\Current (Pre-Wrap)\*.*"
# Copies all Excel files from Outages folder into Current (Pre-Wrap) folder as backups
Copy-Item C:\Users\mathill\Desktop\Outages\*.xls "C:\Users\mathill\Desktop\Outages\Current (Pre-Wrap)"
# Create new Excel Object
$objExcel = new-object -comobject excel.application
# Open Workbook from specific location
$objWorkbook = $objExcel.Workbooks.Open("C:\Users\mathill\Desktop\Outage Protection Macro - Class.xls")
# Open workbook to first worksheet
$worksheet = $Objworkbook.worksheets.item(1)
# Run outageprotect macro
$objExcel.Run("outageprotect")
# Close workbook without saving
$objWorkbook.close($false)
# Exit Excel
$objExcel.quit()
# Remove all Excel files from Outages folder
Remove-Item "C:\Users\mathill\Desktop\Outages\*.xls"
}
答案 0 :(得分:2)
这是因为某些值返回true。您应该考虑的是将所有路径存储在单个数组变量中,然后通过foreach循环一次一个地测试和操作每个路径。所以它看起来更像是这样:
$Paths = "C:\Users\mathill\Desktop\Outages\Excel-Advanzia-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-AIB-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-American-Express-Corp-Srvcs-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Amex-Central-Europe-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Arcot-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-B+S-Card-Service $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Bank-of-America-Asia-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Bank-of-America-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Barclays-Commercial-Card-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-BOI-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-C-Card-S-p-A-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-CCUK Finance-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-CMC-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-COBE-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Degussa-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Deutsche-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Elavon-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Home-Retail-Group-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-IE-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-ING-CPS-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-JPMC-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-KBC-Bank-Ireland-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Lloyds-TSB-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Metro-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-NBS-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-PayPal-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Prime-Carrefour-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-PTSB-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Rabo-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-RBSG-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-RSA-Cyota-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Swisscard-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Telrock-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Tesco-Bank-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-TSYS-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Virgin-Money-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-Wells-Fargo-Outages $a.xls","C:\Users\mathill\Desktop\Outages\Excel-WEX-Outages $a.xls") -eq $true)
Foreach ($Path in $Paths)
{
#skips to next item in array if path not found (continue)
If (! (Test-path $Path))
{Continue}
#rest of code goes here
}
或者,如果此处的要求是在继续之前存在所有38条路径,则可以这样确认:
$Tests = test-path 'c:','c:\windows','c:\dontexist'
If ($Tests -contains $False)
{'path missing';return}