我将我的脚本转换为使用作业:
Start-Job "C:\Users\user\Scripts\PowerShell\SQL-Server\Database Anon\CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 1, $totalCandidate_create -Name "Resume_List1"
Start-Job "C:\Users\user\Scripts\PowerShell\SQL-Server\Database Anon\CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 2, $totalCandidate_create -Name "Resume_List2"
Start-Job "C:\Users\user\Scripts\PowerShell\SQL-Server\Database Anon\CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 3, $totalCandidate_create -Name "Resume_List3"
Start-Job "C:\Users\user\Scripts\PowerShell\SQL-Server\Database Anon\CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 4, $totalCandidate_create -Name "Resume_List4"
Start-Job "C:\Users\user\Scripts\PowerShell\SQL-Server\Database Anon\CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 5, $totalCandidate_create -Name "Resume_List5"
基本上,脚本将文本文件拆分为6个较小的文件,然后使用该文件列表调用简历创建脚本。简历创建只是使用模板并使用Word COM对象和来自SQL服务器的数据填充书签。
我只用一份工作来完成这项工作,我也用12来运行它,但它对创建简历的速度并没有什么影响。它总是大约每秒2。我可以看到它实际上是正确运行脚本的每个实例:
WINWORD.EXE 26560 Console 1 100,556 K WINWORD.EXE 24192 Console 1 102,472 K WINWORD.EXE 7232 Console 1 100,072 K WINWORD.EXE 9300 Console 1 103,132 K WINWORD.EXE 21580 Console 1 101,416 K WINWORD.EXE 15896 Console 1 118,772 K WINWORD.EXE 22916 Console 1 102,352 K WINWORD.EXE 34512 Console 1 101,320 K WINWORD.EXE 17984 Console 1 101,208 K WINWORD.EXE 23964 Console 1 101,272 K WINWORD.EXE 4004 Console 1 101,928 K WINWORD.EXE 4700 Console 1 101,216 K
但我不确定为什么需要相同的时间。
检查工作的结果:
CandidateIDs1_part12.txt进度:39/90
CandidateIDs1_part11.txt进度:52/101
CandidateIDs1_part1.txt进度:45/101
调用脚本:
Param (
[string]$ServerAddress,
[string]$DatabaseName,
[string]$ScriptPath,
[int]$fileNum,
[int]$totalCandidate_create
)
$counter = 1 # counter for timing
$candList = ($ScriptPath + "test\CandidateIDs1_part$fileNum.txt") # path for candidate ID list
$stream = [IO.File]::OpenWrite($candList)
$stream.SetLength($stream.Length - 2)
$stream.Close()
$stream.Dispose()
# Connect Word
$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $false
$wordCounter = 0
$recordCount = (Get-Content $candlist).Count
# loop through candidate list and create resumes
foreach ($candID in Get-Content $candList) {
if ($candID) {
$candID = $candID -replace '\s',''
$templateDir = Get-ChildItem ($ScriptPath + "Templates\Resumes\") | Get-Random -Count 1
$templateFile = $templateDir
$templateDir = ($ScriptPath + "Templates\Resumes\" + $templateDir)
$lname = Invoke-Sqlcmd -Query "SELECT LEFT(personFirstName, 1) FROM tblPErson WHERE personID = $candID" -ServerInstance $ServerAddress -Database $DatabaseName
$sqlResults = Invoke-Sqlcmd -Query "SELECT personFirstName, personSurname, CandidateAddress, CandidateCity, CandidatePostcode,
personMobileTelephone, CandidateState, LEFT(personFirstName, 1) FROM tblPErson INNER JOIN tblCandidate ON tblPErson.personID = tblCandidate.CandidateID
WHERE personID = $candID" -ServerInstance $ServerAddress -Database $DatabaseName
$firstName = $sqlResults.ItemArray[0]
$surName = $sqlResults.ItemArray[1]
$Address1 = $sqlResults.ItemArray[2]
$City = $sqlResults.ItemArray[3]
$Postcode = $sqlResults.ItemArray[4]
$Mobile = $sqlResults.ItemArray[5]
$State = $sqlResults.ItemArray[6]
$lName = $sqlResults.ItemArray[7]
Write-Host "Progress: $counter / "$recordCount
$filename = ($firstname + " " + $surname + " - " + $candID + ".doc")
$filename2 = ($firstname + " " + $surname + " - " + $candID + ".txt")
$saveAs = ($scriptPath + "Documents\Original Resumes\" + $lName + "\" + $filename)
$saveAs2 = ($scriptPath + "Documents\Original Resumes\" + $lName + "\" + $filename2)
## Word integration
$doc=$objWord.documents.Add($templateDir)
$doc.Bookmarks.Item("surName").Range.Text = $sqlResults.ItemArray[0]
$doc.Bookmarks.Item("firstName").Range.Text = $sqlResults.ItemArray[1]
$doc.Bookmarks.Item("Address1").Range.Text = $sqlResults.ItemArray[2]
$doc.Bookmarks.Item("City").Range.Text = $sqlResults.ItemArray[3]
$doc.Bookmarks.Item("Postcode").Range.Text = $sqlResults.ItemArray[4]
$doc.Bookmarks.Item("Mobile").Range.Text = $sqlResults.ItemArray[5]
$doc.Bookmarks.Item("State").Range.Text = $sqlResults.ItemArray[6]
$doc.SaveAs([REF]$saveAs)
$doc.Close()
$doc2=$objWord.Documents.Add($saveAs)
$doc2.SaveAs([REF]$saveAs2, [REF] 2)
if ($wordCounter -eq 100) {
$objWord.Quit()
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$wordCounter = 0
}
$counter += 1 ## Add one to counter
$wordCounter += 1
$doc2.Close())
}
}
$objWord.Quit();
Stop-Job -Name ("Resume_List"+$fileNum);
Remove-Job -Name ("Resume_List"+$fileNum);
启动脚本:
# Kill existing word processes
taskkill /f /pid WINWORD.EXE
$candList = ($ScriptPath + "CandidateIDs1.txt")
# Delete all files first
Get-ChildItem -Path ($ScriptPath + "Documents\Original Resumes\") -Include *.* -File -Recurse | foreach { $_.Delete()}
# Generate candidate list
Invoke-Sqlcmd -Query "SELECT TOP 1200 CandidateID FROM tblCandidate" -ServerInstance $ServerAddress -Database $DatabaseName | Out-File -filePath $candList;
# Cleanse candidate list for reading
Get-Content $candList |
select -Skip 3 |
Set-Content "$candList-temp"
move "$candList-temp" $candList -Force
[int]$maxsize = (Get-Content $candList).Count / 5.9
$sourcefilename = ($scriptPath + "CandidateIDs1.txt")
$destinationfolderpath = ($scriptPath + "test")
$linecount = 0
$filenumber = 1
$srcfile = gci $sourcefilename
$filebasename = $srcfile.BaseName
$fileext = $srcfile.Extension
Get-Content $sourcefilename | Measure-Object | ForEach-Object { $sourcelinecount = $_.Count }
$content = get-content $sourcefilename | % {
#Add-Content $destinationfolderpath\$filebasename_$filenumber.txt "$_"
Add-Content $destinationfolderpath\$filebasename`_part$filenumber$fileext "$_"
$linecount ++
if ($linecount -eq $maxsize) {
$filenumber++
$linecount = 0
Write-Host "Writing part: $destinationfolderpath\$filebasename`_part$filenumber$fileext"
}
}
Start-Job "C:\Users\Scripts\PowerShell\SQL-Server\Database Anon\TRIS-CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 1, $totalCandidate_create -Name "Resume_List1"
Start-Job "C:\Users\Scripts\PowerShell\SQL-Server\Database Anon\TRIS-CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 2, $totalCandidate_create -Name "Resume_List2"
Start-Job "C:\Users\Scripts\PowerShell\SQL-Server\Database Anon\TRIS-CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 3, $totalCandidate_create -Name "Resume_List3"
Start-Job "C:\Users\Scripts\PowerShell\SQL-Server\Database Anon\TRIS-CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 4, $totalCandidate_create -Name "Resume_List4"
Start-Job "C:\Users\Scripts\PowerShell\SQL-Server\Database Anon\TRIS-CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 5, $totalCandidate_create -Name "Resume_List5"
Start-Job "C:\Users\Scripts\PowerShell\SQL-Server\Database Anon\TRIS-CreateSpoofDatabase_Resumes.ps1" { $args[0], $args[1], $args[2], $args[3], $args[4] } -ArgumentList $ServerAddress, $DatabaseName, $ScriptPath, 6, $totalCandidate_create -Name "Resume_List6"