PowerShell脚本需要几个小时才能完成

时间:2017-08-22 13:42:51

标签: mysql powershell

我编写了这个PowerShell脚本并且它工作正常,但它在一个文件上运行需要55秒,但是我需要为435个文本文件运行它,其中每个文件都是~650 kB的文本。 / p>

要完成435个文件需要7个小时!有没有办法让这个过程更快?也许把这个过程减半?我不知道问题是否是循环。

$Path = "C:\Users\rfp6fkj\Desktop\Group\*"
#OK so the Get-ChildItem cmdlet in Powershell creates an array to enumerate/loop thru
$Files = Get-ChildItem "$Path.Group"
(Get-Content $Path) -notmatch '^#' | Where { $_.Trim(" `t") } | Set-Content $Path

function GoParseandInsert {
    try {
        #$connection.Open()
        $cmd = $connection.CreateCommand()
        $insert_stmt = "INSERT INTO PasswordAge.[dbo].[tblDataParsed]
            ([Server]
            ,[Group]
            ,[UserID])

        SELECT I.Server,[Group],F.Val AS TheUserID
        FROM  PasswordAge.[dbo].[GroupPasswordAge] I
              CROSS APPLY PasswordAge.[dbo].ParseDelimValues(CASE WHEN I.UserID = '' THEN 'No Data in Column' ELSE I.UserID END,',') F
        WHERE I.UserID <> ''" 

        $cmd.CommandText = $insert_stmt
        #Write-Output $insert_stmt
        $cmd.ExecuteNonQuery()
    } finally {
        if ($connection -and ($connection.State -eq 'Open')) {
            $connection.Close()
        }
    }
} 

try {
    foreach ($File in $Files) {
        $TheFileName = $File.Basename 
        $StringContent = Get-Content $File 

        foreach ($Thing in $StringContent) {
            $index = $Thing.IndexOf(":")
            $GroupName = $Thing.Substring(0, $index)
            $cmd = $Connection.CreateCommand()
            $BetterThing = $Thing.Split(":")[3]

            $insert_stmt = "INSERT INTO [dbo].[GroupPasswordAge]
                ([Server] 
                ,[Group] 
                ,[UserID])
            VALUES
                ('$thefileName','$GroupName','$BetterThing')" -replace "\s+"," "

            $cmd.CommandText = $insert_stmt
            Write-Host $insert_stmt -ForegroundColor DarkCyan
            $cmd.ExecuteNonQuery()
        }
    } 
} catch {
    #if Files dont exist throw a flag
    Write-Host "Caught an exception:" -ForegroundColor Red
    Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor DarkRed
    Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
} finally {
    Invoke-Sqlcmd -Query "Truncate table PasswordAge..tblDataParsed"
    GoParseandInsert
}

if ($connection -and ($connection.state -eq 'Open')) {
    $connection.Close()
}

1 个答案:

答案 0 :(得分:0)

也许还尝试多线程方法。每个文本文件的“线程”?

https://blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell/