好吧,這就是我要做的,如果是我的話。當他們完成後,我會先保存Excel文件,然後獲取一些詳細信息以記錄它,以確保腳本運行時間之間不存在任何修改(哦,是的,在Excel中打開它並對它進行排序很有趣記錄不同,然後嘗試並用腳本繼續處理它!)。就個人而言,我在V3上安裝了PowerShell社區擴展,所以我有Get-Hash來獲得MD5 HashString。如果你有PSv4,那麼有一個內置命令來獲得文件的SHA哈希。如果你沒有,你可以使用LastWriteTime屬性,因爲我必須假設你沒有前面提到的,我會在我的例子中使用它。
現在,這不會加載窗體窗體,並且包含一個函數來創建彈出窗口來與用戶進行交互。如果你不喜歡這樣,你可以將代碼恢復到你的文本提示,並只使用你想要的東西。
#Load up the Windows Forms so we can display a message box as needed
[void] [System.Reflection.Assembly]::LoadWithPartialName(「System.Windows.Forms」)
#Function to display a message box with ease as needed
Function Show-MsgBox ($Title,$Text,$Button = "OK"){
[Windows.Forms.MessageBox]::Show("$Text", "$Title", [Windows.Forms.MessageBoxButtons]::$Button, [Windows.Forms.MessageBoxIcon]::Information)
}
#Import the log file, or set one up if there isn't one. Make sure source file hasn't changed since we last updated it with the script.
$LogFile=$args[0].replace(".csv", ".log")
If((Test-Path $LogFile)){
$Log = Import-Csv $LogFile
If(!((GCI $Args[0]).LastWriteTime -eq ($Log|Select -Last 1).LastWriteTime)){If((Show-MsgBox -Title "File Change Confirmation" -Text "The Last Write Time of $($args[0]) is different than what is recorded in the log.`n`nWould you like to proceed with record number $CurrentRecord anyway?" -Button YesNo) -eq "No"){$null=Show-MsgBox -Title "Abort" -Text "Ending Script due to Last Write Time mismatch.`nPlease verify that the records in $(gci $args[0]|select Name) have not been modified before running this script again, or rename (or delete) the following before running this script again to start over:`n`n$LogFile";Break}
$CurrentRecord = ($Log|Select -Last 1).EndRecord
Write-Output "$($Args[0]) was last updated $($Log.LastWriteTime). Proceeding from record $CurrentRecord`."
} Else {
$CurrentRecord=0
Write-Output "No log file detected for $($Args[0]). Proceeding from record $CurrentRecord`."
}
}
$Loop = 1
#Loop for all remaining records, checking to see if the user wants to stop every $outNu records
For($i=$CurrentRecord;$i -le $Element.count;$i++){
#Do Stuff to $Element[$i] with feedback from user
$CurrentRecord++
If($Loop -eq $outNu){
If((Show-MsgBox -Title "Time for a break?" -Text "You have finished one block, would you like to take a break? (If you say no you must do the next $outNu list elements.))" -Button YesNo) -eq "Yes"){
Break
} else {
$Loop = 1
}
} else {
$Loop++
}
}
#Save Excel file and exit Excel if desired
$LogFile = [pscustomobject]@{TimeStamp=$(Get-date -UFormat "%x %r");EndRecord=$CurrentRecord;LastWriteTime=$(GCI $args[0]|Select LastWriteTime)}
$LogFile|Export-Csv .\Desktop\RecordLog.log -NoTypeInformation -Append
就是這樣。查找指定Excel文件的日誌文件(如CSV,但使用.log擴展名)。如果它找到一個它驗證源文件對日誌的完整性,並循環遍歷所有剩餘的記錄,檢查它們是否要停止每個$ outNu記錄。然後它追加日誌文件,或者創建它,如果沒有。
我不知道如何在列表本身工作。我需要能夠發佈一個coment foreach list元素並給出一個特殊的背景顏色。我將結果保存爲html文件。這很容易。但我用for循環接受了你的想法。 – user3284214