-
-
Save gbautista72/3345624978cfedf88e0c5761e1264ed8 to your computer and use it in GitHub Desktop.
Read word document - do an action based on text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clear-Variable content -erroraction SilentlyContinue | |
Clear-Variable list -erroraction SilentlyContinue | |
$documentPaths = New-Object System.Collections.ArrayList | |
$doctorResults = @{} | |
$sites = 'cdc','fh','svh','scdc','rdc' | |
$date = (Get-Date(Get-Date).AddDays(-1) -Format 'yyyy-MM-dd') | |
$dateTime = (Get-Date(Get-Date).AddDays(-1) -Format 'MM-dd-yyyy.hh.mm') | |
$csvPath = "D:\script\Isaacs-Scripts\scripts\get-doctorcount\CSVs\$dateTime.csv" | |
$completeTotal = 0 | |
$errorLog = "D:\script\Isaacs-Scripts\scripts\get-doctorcount\errorlogs\$dateTime.html" | |
$doctorLog = "D:\script\Isaacs-Scripts\scripts\get-doctorcount\logs\$dateTime.txt" | |
# array of all doctor names | |
# to add to it, simply add quotes and a comma (unless its the last in the array) | |
$doctors = @( | |
'John Q. Smith', | |
'John D. Doe', | |
'Jane X. Smith', | |
'Mary P. Doe', | |
) | |
try { | |
# add firstnames to list | |
$firstnames = New-Object System.Collections.ArrayList | |
foreach ($doctor in $doctors) { | |
$docname = ($doctor -split '\s') | |
$docname = $docname[0]+$docname[-1][0] | |
$firstnames += $docname | |
} | |
# foreach doctor name, name a new variable called the doctor name | |
foreach ($firstname in $firstnames) { | |
if (Get-Variable -name $firstname -ErrorAction SilentlyContinue) { | |
Remove-Variable $firstname | |
} | |
New-Variable -Name $firstname -Value @{} | |
$drvar = (Get-Variable -Name $firstname -ValueOnly) | |
$drvar.add('rdc',(New-Object System.Collections.Arraylist)) | |
$drvar.add('cdc',(New-Object System.Collections.Arraylist)) | |
$drvar.add('fh',(New-Object System.Collections.Arraylist)) | |
$drvar.add('svh',(New-Object System.Collections.Arraylist)) | |
$drvar.add('scdc',(New-Object System.Collections.Arraylist)) | |
} | |
foreach ($site in $sites) { | |
# if site is rdc use rdc path | |
if ($site -eq 'rdc') { | |
$items = gci "D:\reports\dr_reports\archive\$date" | |
} else { | |
$items = gci "D:\ininbound\$site\archive\$date" | |
} | |
foreach ($item in $items) { | |
$documentPaths += $item.fullname | |
} | |
} | |
foreach ($document in $documentPaths) { | |
#If ($Match = Select-String -Path $document -Pattern '\A\s*Dictated\s+by\s*:?\s*(.*?)\s*\Z' | Select-Object -Last 1 -ExpandProperty Matches) { | |
If ($Match = Select-String -Path $document -Pattern '^*Dictated\s+by\s*:?\s*(.*?)\s*\Z' | Select-Object -Last 1 -ExpandProperty Matches) { | |
$content = $Match.Groups[1].Value | |
$sitename = $document.split('\')[2] | |
if ($sitename -match 'dr_reports') { $sitename = 'rdc' } | |
Write-Host "`r`n" | |
Write-Host $document -ForegroundColor Cyan | |
Write-Host $content | |
foreach ($doctor in $doctors) { | |
if ($content -match $doctor) { | |
# create dyn string to call dr variable | |
$docsplit = $doctor.split(' ') | |
[string]$tempDrVar = $docsplit[0]+$docsplit[-1][0] | |
# call doctor firstname variable | |
$doctorVariable = Get-Variable -Name $tempDrVar | |
$doctorVariable.value.$sitename += $document | |
Write-Host "$document matched $doctor" -ForegroundColor Yellow | |
Add-Content -path $doctorLog -value "$doctor found on $document" | |
} | |
} | |
} Else { | |
# String not found | |
} | |
} | |
New-Item -Path $csvPath -itemtype file | |
Add-Content $csvPath -Value $dateTime | |
Add-Content $csvPath -value "DRName,TOTAL,RASLO,CDC,FH,SVH,SCDC" | |
foreach ($firstname in $firstnames) { | |
$total = 0 | |
$docVar = Get-Variable -name $firstname -ValueOnly | |
$keys = $docVar.keys | |
foreach ($key in $keys) { | |
$total = $docVar.$key.count + $total | |
} | |
$completeTotal = $completeTotal + $total | |
Add-Content $csvPath -Value "$firstname,$total,$($docVar.rdc.count),$($docVar.cdc.count),$($docVar.fh.count),$($docVar.svh.count),$($docVar.scdc.count)" | |
} | |
Add-Content $csvPath -Value "Total Doctor Count,$completeTotal" | |
Send-MailMessage -From 'reports@ra-slo.com' -to 'radreadcount@ra-slo.com' -Subject "Radiologist Daily Read Count for $date" -Body "Radiologist counts for $date" -SmtpServer 'aspmx.l.google.com' -port '25' -Attachments $csvPath | |
#Send-MailMessage -From 'reports@ra-slo.com' -to 'gbautista@ra-slo.com' -Subject "Radiologist Daily Read Count for $date" -Body "Radiologist counts for $date" -SmtpServer 'aspmx.l.google.com' -port '25' -Attachments $csvPath | |
$sortedContent = Get-Content $doctorLog | Sort-Object | |
Set-Content -path $doctorLog -Value $sortedContent | |
} catch { | |
Add-Content $errorlog -Value "<font color = 'red'>Error Content</font> -<br><br>Exception: $($_.Exception)<br><br> CategoryInfo: $($_.CategoryInfo)<br><br> StackLocation: $($_.ScriptStackTrace)<hr>" | |
Send-MailMessage -From 'reports@ra-slo.com' -to 'itdept@ra-slo.com' -Subject 'Error found with Doctor Count:' -Body "<font color = 'red'>Error Content</font> -<br><br>Exception: $($_.Exception)<br><br> CategoryInfo: $($_.CategoryInfo)<br><br> StackLocation: $($_.ScriptStackTrace)<hr>" -SmtpServer 'aspmx.l.google.com' -port '25' -BodyAsHtml | |
#Send-MailMessage -From 'reports@ra-slo.com' -to 'gbautista@ra-slo.com' -Subject 'Error found with Doctor Count:' -Body "<font color = 'red'>Error Content</font> -<br><br>Exception: $($_.Exception)<br><br> CategoryInfo: $($_.CategoryInfo)<br><br> StackLocation: $($_.ScriptStackTrace)<hr>" -SmtpServer 'aspmx.l.google.com' -port '25' -BodyAsHtml | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment