我编写了这个PowerShell脚本,它工作正常,但它需要55秒才能在一个文件上运行,但我需要运行435个文本文件,其中每个文件大约650 kB.
要完成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()
}
Run Code Online (Sandbox Code Playgroud)
小智 -1
也许还可以尝试多线程方法。每个文本文件一个“线程”?
https://blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell/