Powershell脚本不会从它应该停止的位置继续

时间:2014-02-07 15:01:38

标签: excel powershell

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

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

1 个答案:

答案 0 :(得分:0)

好的,所以这就是我做的事情,如果是我的话。当它们完成后,我首先保存Excel文件,然后获取一些详细信息来记录它,以确保脚本运行的时间之间没有任何修改(哦,是的,有趣的是在Excel中打开它并以不同方式对记录进行排序,然后尝试使用脚本继续处理它!)。我个人在v3上安装了PowerShell社区扩展,所以我有Get-Hash来获取MD5 HashString。如果您有PSv4,那么内置命令可以获取文件的SHA哈希值。如果你没有,你可以使用LastWriteTime属性,因为我必须假设你没有前面提到的我会在我的例子中使用它。

现在,这会加载Windows窗体,并包含一个创建弹出窗口以与用户交互的功能。如果您不喜欢,可以将代码还原为您提供的文本提示,并使用我想要的内容。

#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记录。然后它附加日志文件,或者如果没有日志文件则创建它。