我有一个脚本,它通过一个CSV文件列表,每个文件有100个条目。基本上,它根据该行中的数据创建一个word文档,这是循环的开始:
while ( ( Get-Childitem "\\rspldata\QMS\Owain\Database-Anon-Resumes\DataFiles-new\").count -ne 0 ) {
[int]$hour = get-date -format HH
If ( $hour -gt 6 -and $hour -lt 6 ) {
Start-Sleep -s 1800100}
else {
foreach ( $file in ( Get-Childitem "\\rspldata\QMS\Owain\Database-Anon-Resumes\DataFiles-new\" ) ) {
# Connect Word
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$templatedir = "\\rspldata\QMS\Owain\Database-Anon-Resumes\Templates\Resumes\Out\"
$templateDir = Get-ChildItem ("\\rspldata\QMS\Owain\Database-Anon-Resumes\Templates\Resumes\Out\") | Get-Random -Count 1
$templateFile = $templateDir
$templateDir = ("\\rspldata\QMS\Owain\Database-Anon-Resumes\Templates\Resumes\Out\" + $templateDir)
if (!(checkLock $file.fullname) -and (Test-Path $file.FullName) ) {
$cand_CSV = Import-CSV $file.fullname
$LockFile = [System.io.File]::Open("$($file.fullname)", 'Open', 'Read','None')
Write-Host "[+] Processing file $($file.fullname)" -foregroundcolor GREEN
foreach ( $line in $cand_CSV ) {
这是结束:
}
$LockFile.Close()
Remove-Item $file.fullname -force
}
else {
Write-Host "$($file.fullname) :: locked - continuing"
}
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$objWord)
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
Remove-Variable objWord
}
}
}
foreach循环的其余部分只是将数据添加到Word中的书签。
基本上,我正在尝试创建大约1,000,000个恶搞简历,这在一台PC上耗时太长,所以我希望能够在几台PC上运行并离开它,问题是,经过测试昨晚我发现有大约150个WINWORD.EXE进程打开,计算机必须重新启动。
其他一台PC的打开了一个saveas框,阻止了脚本的继续。
以下不应该关闭Word进程吗?
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$objWord)
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
Remove-Variable objWord
答案 0 :(得分:1)
我不相信删除PowerShell变量就足以摆脱Word实例。您必须明确告诉Word退出:
error: non-const lvalue reference to type 'Number' cannot bind to a temporary of type 'Digit'
C(int x) : num(Digit(x)) {};
^ ~~
error: non-const lvalue reference to type 'Number' cannot bind to a temporary of type 'Text'
C(std::string x) : num(Text(x)) {};
^ ~~~~~
答案 1 :(得分:1)
停止关闭Word并重新启动它!将创建Word comobject的部分移动到循环之前。关闭文档,但不是Word。循环完成后,关闭Word并销毁comobject。
# Connect Word
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$templateRootDir = "\\rspldata\QMS\Owain\Database-Anon-Resumes\Templates\Resumes\Out"
while ( ( Get-Childitem "\\rspldata\QMS\Owain\Database-Anon-Resumes\DataFiles-new\").count -ne 0 ) {
[int]$hour = get-date -format HH
If ( $hour -gt 6 -and $hour -lt 6 ) { # Will NEVER happen. $hour can not be both greater than, and also less than 6.
Start-Sleep -s 1800100}
else {
foreach ( $file in ( Get-Childitem "\\rspldata\QMS\Owain\Database-Anon-Resumes\DataFiles-new\" ) ) {
$templateFile = Get-ChildItem $templateRootDir | Get-Random -Count 1
$templateDir = $templateFile.FullName
if (!(checkLock $file.fullname) -and (Test-Path $file.FullName) ) {
$cand_CSV = Import-CSV $file.fullname
$LockFile = [System.io.File]::Open("$($file.fullname)", 'Open', 'Read','None')
Write-Host "[+] Processing file $($file.fullname)" -foregroundcolor GREEN
foreach ( $line in $cand_CSV ) {
$docResume = $objWord.Documents.add($templateDir)
<more code>
$docResume.Save()
$docResume.Close($false)
}
$LockFile.Close()
Remove-Item $file.fullname -force
}
else {
Write-Host "$($file.fullname) :: locked - continuing"
}
}
}
}
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$objWord)
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
Remove-Variable objWord
另外,请在第一次If
声明后查看我的评论。