Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel powershell脚本超时_Excel_Powershell_Timeout - Fatal编程技术网

Excel powershell脚本超时

Excel powershell脚本超时,excel,powershell,timeout,Excel,Powershell,Timeout,我在Powershell中有一个脚本,可以通过与SQL数据库的连接更新Excel文件。脚本工作正常,但问题是如果一个连接不工作,脚本将无法继续。有没有一种方法可以像超时或者其他什么东西那样在一个诡计之后继续? 以下是我的脚本: $libraryPath = "C:\temp\Excel\" $excel = new-object -comobject Excel.Application # Give delay to open Start-Sleep -s 5 $allEx

我在Powershell中有一个脚本,可以通过与SQL数据库的连接更新Excel文件。脚本工作正常,但问题是如果一个连接不工作,脚本将无法继续。有没有一种方法可以像超时或者其他什么东西那样在一个诡计之后继续? 以下是我的脚本:

$libraryPath = "C:\temp\Excel\"  
$excel = new-object -comobject Excel.Application  
 # Give delay to open  
 Start-Sleep -s 5  
 $allExcelfiles = Get-ChildItem $libraryPath -recurse -include “*.xlsx”  

 foreach ($file in $allExcelfiles)  
 {  
      $workbookpath = $file.fullname  
           Write-Host "Updating " $workbookpath  
           # Open the Excel file  
           $excelworkbook = $excel.workbooks.Open($workbookpath)  
           $connections = $excelworkbook.Connections  
           foreach ($c in $connections)  
           {  
                if ($c.DataFeedConnection -ne $null)  
                {  
                     $conn = $c.DataFeedConnection.Connection  
                     # Use regex to search and replace part of connection string  
                     $new = $conn -replace 'ProjectName eq ''(.*)''', "ProjectName eq '$title'"  
                     $c.DataFeedConnection.Connection = $new  
                     Write-Host "Connection replaced."  
                }  
           } 
           Start-Sleep -s 5 
           # This will Refresh All the pivot tables data.  
           $excelworkbook.RefreshAll()  
           # The following script lines will Save the file. 
           Start-Sleep -s 50 
           $excelworkbook.Save()  
           $excelworkbook.Close()  
      }  

 $excel.quit()

谢谢

您可以将脚本作为作业运行,如果作业超过给定时间段,请终止它

$timeout_in_sec = 10
$excel_update = {
    $libraryPath = "C:\temp\Excel\"  
    $excel = new-object -comobject Excel.Application  
    # Give delay to open  
    Start-Sleep -s 5  
    $allExcelfiles = Get-ChildItem $libraryPath -recurse -include “*.xlsx”  

    foreach ($file in $allExcelfiles)  
    {  
        $workbookpath = $file.fullname  
            Write-Host "Updating " $workbookpath  
            # Open the Excel file  
            $excelworkbook = $excel.workbooks.Open($workbookpath)  
            $connections = $excelworkbook.Connections  
            foreach ($c in $connections)  
            {  
                    if ($c.DataFeedConnection -ne $null)  
                    {  
                        $conn = $c.DataFeedConnection.Connection  
                        # Use regex to search and replace part of connection string  
                        $new = $conn -replace 'ProjectName eq ''(.*)''', "ProjectName eq '$title'"  
                        $c.DataFeedConnection.Connection = $new  
                        Write-Host "Connection replaced."  
                    }  
            } 
            Start-Sleep -s 5 
            # This will Refresh All the pivot tables data.  
            $excelworkbook.RefreshAll()  
            # The following script lines will Save the file. 
            Start-Sleep -s 50 
            $excelworkbook.Save()  
            $excelworkbook.Close()  
        }  

    $excel.quit()
}

$job = Start-Job -Name 'thing' -ScriptBlock $excel_update
Wait-Job -Timeout $timeout_in_sec -Job $job
if ($job.State -eq 'Running') { Stop-Job -Job $job }
Remove-Job -Job $job
将$timeout_(以秒为单位)更改为适合您需要的任何值