1
完全公開,我是使用Powershell的新手,我正在研究測試38個excel報告是否在(如Excel-Advanzia-Outages 2015年9月18日),將它們複製到另一個文件夾作爲備份,在另一個Excel工作簿中運行宏(將報表按照日期),最後從原始文件夾中刪除38個報告。PowerShell腳本,如果/否則使用測試路徑,如果/否則不正常工作時False
我從其中一個報告中更改/刪除了日期,以便它將返回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"
}
這完美地工作。謝謝諾亞。 – coreyhill