2014-02-07 51 views
1

我創建了一個Powershell腳本,在(長)Excel列表上半自動工作(用戶仍然需要對他或她做一些小的事情擁有)。我希望它能夠保存已停止的位置,然後從那裏繼續(用戶有能力在列出的元素塊之後退出工作(他可以決定此博客的大小))。現在我的解決方案是,我將保存在一個單獨文件中的變量,然後從那裏讀取它,跳過已經處理的列表元素。但是,這似乎並不奏效。一個Powershell腳本不會繼續從它應該停止的地方

# Checking if the list was entered properly 
# Checking where it has stopped 
$counter=$args[0].replace(".csv", "c.ps1"); 
if(-not (Test-path $counter)){ 
     $num=1; 
}else{ 
     $countn=$args[0].replace(".csv", "c.ps1"); 
     . ".\$countn"; 
} 
... 
$zzz=1; 
$pause=$outNu; #outNu was entered by the user 
$liste| ForEach-Object { 
if($zzz -lt $num){ 
     $zzz++ 
     $zzz 
     return; 
} 
# work on the listelement 
# with a litttle feedback from the use 
$num++; 
$pause--; 
if(-not ($pause -eq 0)){ 
    "Elements in this block: "+$pause+"." 
}else{ 
    $weg=Read-Host "Yo have finished one block, would you like to take a break? (If you say no you must do the next $outNu list elements.) Y/N"; 
    if($weg -eq "Y" -OR $weg -eq "y"){ 
      echo "`$num=$num;" > $counter; 
      break; 
    } 
    $pause=$outNu; 
}} 

回答

0

好吧,這就是我要做的,如果是我的話。當他們完成後,我會先保存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記錄。然後它追加日誌文件,或者創建它,如果沒有。

+0

我不知道如何在列表本身工作。我需要能夠發佈一個coment foreach list元素並給出一個特殊的背景顏色。我將結果保存爲html文件。這很容易。但我用for循環接受了你的想法。 – user3284214

相關問題