Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Created by /u/Sys_Ad_MN
- #Used to on-board new users with a welcome letter
- #Edits word documents to replace certain words and hyperlinks
- #Option included to email using office 365
- #Option included to print to default printer
- $WLPath = "C:\WelcomeLetter"
- if (-Not (Test-Path $WLPath))
- {
- mkdir C:\welcomeletter
- Write-Host("C:\WelcomeLetters has been created")
- }
- $objWord = New-Object -comobject Word.Application
- $objWord.Visible = $True
- $objDoc = $objWord.Documents.Open("\\Server\Share\WelcomeLetters\welcomeletter.docx") #path to word doc template
- $ObjSelection = $objWord.Selection
- $FindText = "FirstName"
- $MatchCase = $False
- $MatchWholeWord = $true
- $MatchWildcards = $False
- $MatchSoundsLike = $False
- $MatchAllWordForms = $False
- $Forward = $True
- $Wrap = $wdFindContinue
- $Format = $False
- $wdReplaceNone = 0
- $FirstName = Read-Host -Prompt "Input First Name"
- $ReplaceWith = $FirstName
- $wdFindContinue = 1
- $wdReplaceAll = 2
- $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
- $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
- $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
- If ($objSelection.Find.Found)
- {
- Write-Host("The first name has been set") } Else {
- Write-Host("The search text was not found.") }
- $FindText = "LastName"
- $MatchCase = $False
- $MatchWholeWord = $true
- $MatchWildcards = $False
- $MatchSoundsLike = $False
- $MatchAllWordForms = $False
- $Forward = $True
- $Wrap = $wdFindContinue
- $Format = $False
- $wdReplaceNone = 0
- $LastName = Read-Host -Prompt "Input Last Name"
- $ReplaceWith = $LastName
- $wdFindContinue = 1
- $wdReplaceAll = 2
- $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
- $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
- $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
- If ($objSelection.Find.Found)
- {
- Write-Host("The Last name has been set") } Else {
- Write-Host("The search text was not found.") }
- $FindText = "pswd"
- $MatchCase = $True
- $MatchWholeWord = $true
- $MatchWildcards = $False
- $MatchSoundsLike = $False
- $MatchAllWordForms = $False
- $Forward = $True
- $Wrap = $wdFindContinue
- $Format = $False
- $wdReplaceNone = 0
- $Password = Read-Host -Prompt "Input Password"
- $ReplaceWith = $Password
- $wdFindContinue = 1
- $wdReplaceAll = 2
- $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
- $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
- $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
- If ($objSelection.Find.Found)
- {
- Write-Host("The password has been set") } Else {
- Write-Host("The search text was not found.") }
- $FindText = "Title"
- $MatchCase = $False
- $MatchWholeWord = $true
- $MatchWildcards = $False
- $MatchSoundsLike = $False
- $MatchAllWordForms = $False
- $Forward = $True
- $Wrap = $wdFindContinue
- $Format = $False
- $wdReplaceNone = 0
- $Title = Read-Host -Prompt "Input Title"
- $ReplaceWith = $Title
- $wdFindContinue = 1
- $wdReplaceAll = 2
- $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
- $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
- $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
- If ($objSelection.Find.Found)
- {
- Write-Host("The Title has been set") } Else {
- Write-Host("The search text was not found.") }
- $FindText = "Cell#"
- $MatchCase = $False
- $MatchWholeWord = $true
- $MatchWildcards = $False
- $MatchSoundsLike = $False
- $MatchAllWordForms = $False
- $Forward = $True
- $Wrap = $wdFindContinue
- $Format = $False
- $wdReplaceNone = 0
- $Cell = Read-Host -Prompt "Input Cell # ex 000.000.0000 or press enter to leave blank"
- $ReplaceWith = $Cell
- $wdFindContinue = 1
- $wdReplaceAll = 2
- $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
- $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
- $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
- If ($objSelection.Find.Found)
- {
- Write-Host("The Cell phone number has been set") } Else {
- Write-Host("The search text was not found.") }
- #Edits the hyperlinks of email addresses
- $OldHyperlink = “mailto:First.Last@domain.com”
- $NewHyperlink = "Mailto:" + $FirstName + "." + $LastName + "@domain.com"
- ForEach ($objHyperlink in $objDoc.Hyperlinks)
- {
- If ($objHyperlink.address -eq $OldHyperlink)
- {
- $objHyperlink.Address = $objHyperlink.Address -Replace $OldHyperlink, $NewHyperlink
- }
- }
- $NewUser = $FirstName + "." + $LastName
- $NewUserEmail = $NewUser + "domain.com"
- $FileName = 'C:\WelcomeLetters\' + $NewUser + '.docx'
- #Saves the document and quits word
- $objdoc.SaveAs([ref] $FileName)
- $objword.quit()
- #Emails Welcome Letter to New User
- $mycreds = New-Object System.Management.Automation.PSCredential (Get-Credential)
- $Subject = $NewUser + " Welcome Letter"
- $Body = $FirstName + "," + "<br>" + "<br>" + "Welcome, attached is a welcome letter with contact and login information.<br><br>If you need any assistance, please feel free to contact helpdesk@domain.com"
- $SendEmail = Read-Host -Prompt "Type yes to send an email to the new user"
- If ($SendEmail -eq "yes")
- {
- Send-MailMessage -To $NewUserEmail -Cc "john.doe@domain.com" -Attachments $FileName -SmtpServer "smtp.office365.com" -Credential $mycreds -UseSsl $Subject -Port "587" -Body $Body -From "helpdesk@domain.com" -BodyAsHtml
- }
- #Prints to default printer
- $Print = Read-Host -Prompt "Type yes to print to your default printer"
- If ($Print -eq "yes")
- {
- Start-Process -FilePath $FileName -Verb print
- }
Add Comment
Please, Sign In to add comment