Powershell脚本不会继续停止

我创build了一个Powershell脚本,在一个(长)Excel列表上自动半自动地工作(用户仍然需要自己做一些小的事情)。 我希望它能够保存已停止的地方,然后从那里继续(用户可以在列出的元素块(他可以决定这个博客的大小)之后退出工作)。 现在我的解决scheme是,我将保存在一个单独的文件中的variables,然后从那里读取它,跳过已经工作的列表元素。 但是,这似乎并不奏效。

# 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; }} 

好吧,如果是我的话,我会这样做。 当他们完成后,我会先保存Excel文件,然后得到一些细节来logging它,以确保在脚本运行的时间之间没有任何修改(哦,是的,有什么好玩的在Excel中打开它并sortinglogging不同,然后尝试并用脚本继续处理它!)。 就个人而言,我在V3上安装了PowerShell社区扩展,所以我有Get-Hash来获得一个MD5 HashString。 如果您有PSv4,则有一个内置命令来获取文件的SHA哈希值。 如果你没有,你可以使用LastWriteTime属性,因为我必须假设你没有上述提到我会用我的例子。

现在,这不会加载Windows窗体,并包含一个函数来创buildpopup窗口来与用户交互。 如果你不喜欢,你可以将代码恢复到你的文本提示,并使用你想要的,我想。

 #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扩展名)。 如果它find一个它validation源文件完整性的日志,并循环所有剩余的logging检查,看看他们是否要停止每一个$ outNulogging。 然后附加日志文件,或者创build它,如果没有。