Powershell脚本很慢

时间:2019-11-26 16:26:45

标签: performance powershell

我有一个运行良好的Powershell脚本。它完成了应该做的事情。 但是,它非常慢。如果要搜索xlsx和csv文件(约100个文件),则需要几个小时。

有没有办法使它更快?我已经建立了到共享库的网络连接

#Client modules laden (https://www.microsoft.com/en-us/download/details.aspx?id=42038)
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"   
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Search.dll"

#Folder om mee verbinding te maken
$Folder = Read-Host -Prompt "Folder (TK8, TK9, TK10)"
$team = Read-Host -Prompt "Team (ICT, CT&M, F&V,D)"
$SourceFolder = "Z:\$Folder\$team"

#Keywords waarop gezocht moet worden
$keyword1 = Read-Host -Prompt "Crebocode (25189, enz.)"
$keyword2 = Read-Host -Prompt "Studentnummer (151510, enz.)"
$keyword3 = Read-Host -Prompt "Cohort (2017, enz.)"

$keyword4 = Read-Host -Prompt "Leerweg (BOL, BBL)"
#$keyword5 = Read-Host -Prompt "Soort product (examenplan, examenproduct)"
$Forward = $true
$MatchWholeWord = $true

#Folder waarheen weggeschreven wordt
$PadDossier = Get-ChildItem  "Z:\Gedeelde  documenten\Examendossiers" | Where name -Like "*$($keyword1)*"
$TargetFolder = "Z:\Gedeelde  documenten\Examendossiers\$($PadDossier.Name)\$($keyword4.ToUpper())\$keyword3\Studentendossiers" 
$TargetFolder1 = Get-ChildItem "Z:\Gedeelde  documenten\Examendossiers\$($PadDossier.Name)\$($keyword4.ToUpper())\$keyword3\Studentendossiers" | Where name -Like "*$($keyword2)*"
$TargetFolder2 = "Z:\Gedeelde  documenten\Examendossiers\$($PadDossier.Name)\$($keyword4.ToUpper())\$keyword3\Studentendossiers\$($TargetFolder1.Name)" 

$files = Get-ChildItem -Path $SourceFolder -Recurse | Where-Object {$_.Name -like '*.xls*'-or $_.Name -like '*.csv'}

foreach ($file in $files)
{
    $path = $file.FullName
    $objExcel = New-Object -ComObject Excel.Application
    $objExcel.Visible = $false
    $workBook = $objExcel.Workbooks.Open($path) 
    $sheets = $workBook.Sheets
    $results = @()

      foreach ($sheet in $sheets)
    {
        $sheetName = $sheet.Name
        $filter2 = $workBook.Sheets.Item("$sheetName").UsedRange.Find("$keyword2")
        
        if ($filter2.Text -match $keyword2)
        {
            $results += $true
            write-host -f Cyan $path $sheetName $filter2.Text
            #Copy-Item -Path $path -Destination $destination -Verbose
        }
 #        elseif ($filter2.Text -cnotcontains $MatchWholeWord)
 #   {
        #$word.Application.ActiveDocument.Close()
 #       Write-Host -f Red $path $sheetName $filter2.Text
 #   }
    }
   }
Stop-Process -Name "EXCEL"

0 个答案:

没有答案