长时间运行的Powershell脚本冻结

问题描述 投票:3回答:4

亲爱的PowerShell用户,

我们使用长时间运行的PowerShell脚本来执行许多可能需要很长时间的小操作。大约30分钟后,脚本冻结了。我们能够通过按Ctrl-C让脚本重新开始运行,这导致脚本恢复执行而不是终止进程。

是否有某种脚本超时或机制阻止PowerShell中长时间运行的脚本?

谢谢您的帮助。

powershell-v2.0 long-running-processes
4个回答
2
投票

我有这个问题,因为我有一个坏习惯。如果在控制台PowerShell中选择一些文本,脚本日志将冻结。启动一个大脚本后确保没有选择任何内容:)


0
投票

经过很长一段时间后,我对这个问题没有任何回应。我将假设我的脚本或子线程冻结有问题。如果不是这样,请回答。


0
投票

尝试在您的脚本中添加百分比计算..这样您就可以确定完成所需的时间...


0
投票

试试我的kill timer脚本。只需将$ScriptLocation变量更改为您要运行的脚本即可。然后该脚本将作为后台作业运行,而当前窗口将跟踪计时器。时间到期后,当前窗口将终止后台作业并将其全部写入日志。

    Start-Transcript C:\Transcriptlog-Cleanup.txt #write log to this location
$p = Get-Process  -Id $pid | select -Expand id  # -Expand selects the string from the object id out of the current process.
Write-Host $p
$BJName = "Clean-up-script"   #Define Background job name

$startTime = (Get-Date) # set start time
$startTime
$expiration = (Get-Date).AddMinutes(2)#program expires at this time
# you could change the expiration time by changing (Get-Date).AddSeconds(20) to (Get-Date).AddMinutes(10)or to hours or whatever you like


#-----------------
#Timer update function setup
function UpdateTime
   {
    $LeftMinutes =   ($expiration) - (Get-Date) | Select -Expand minutes  # sets minutes left to left time
    $LeftSeconds =   ($expiration) - (Get-Date) | Select -Expand seconds  # sets seconds left to left time


    #Write time to console
    Write-Host "------------------------------------------------------------------" 
    Write-Host "Timer started at     :  "  $startTime
    Write-Host "Current time         :  "  (Get-Date)
    Write-Host "Timer ends at        :  "  $expiration
    Write-Host "Time on expire timer :  "  $LeftMinutes "Minutes" $LeftSeconds "Seconds"
    Write-Host "------------------------------------------------------------------" 
    }

    #get background job info and remove the it afterwards + print info
function BJManager   
    {
       Receive-Job -Name $BJName  #recive background job results
       Remove-Job -Name $BJName -Force #remove job
       Write-Host "Retrieving Background-Job info and Removing Job..."
    }
#-----------------
$ScriptLocation = "C:\\Local-scripts\Windows-Server-CleanUp-Script-V2.4(Beta).ps1"  #change this Var for different kind of script locations

Start-Job -Name $BJName -FilePath $ScriptLocation #start this script as background job
# dont start job in the loop.
do{   #start loop

   Write-Host "Working"#start doing other script stuff
   Start-Sleep -Milliseconds 5000  #add delay to reduce spam and processing power
   UpdateTime #call upadate function to print time

   Get-Job -Name $BJName  | select Id, State ,Location , Name
        if((Get-Job).State -eq "Failed")
            {
                BJManager
            }
        elseif((Get-Job).State -eq "Completed")
            {
                BJManager
            }

 }
until ($p.HasExited -or (Get-Date) -gt $expiration) #check exit time

Write-Host "Timer Script Finished"
Get-Job -Name $BJName  | select Id, State ,Location , Name
UpdateTime

BJManager

Start-Sleep -Milliseconds 5000 #give it some time to write to log
Stop-Transcript
Start-Sleep -Milliseconds 5000 #give it some time to stop the logging before killing process
if (-not $p.HasExited) { Stop-Process -ID $p -PassThru } # kill process after time expires
© www.soinside.com 2019 - 2024. All rights reserved.