Guest User

WelcomeLetter

a guest
Jun 6th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. #Created by /u/Sys_Ad_MN
  2. #Used to on-board new users with a welcome letter
  3. #Edits word documents to replace certain words and hyperlinks
  4. #Option included to email using office 365
  5. #Option included to print to default printer
  6.  
  7.  
  8.  
  9. $WLPath = "C:\WelcomeLetter"
  10.  
  11. if (-Not (Test-Path $WLPath))
  12.  
  13. {
  14.  
  15. mkdir C:\welcomeletter
  16. Write-Host("C:\WelcomeLetters has been created")
  17.  
  18. }
  19.  
  20. $objWord = New-Object -comobject Word.Application
  21.  
  22. $objWord.Visible = $True
  23.  
  24.  
  25. $objDoc = $objWord.Documents.Open("\\Server\Share\WelcomeLetters\welcomeletter.docx") #path to word doc template
  26.  
  27. $ObjSelection = $objWord.Selection
  28.  
  29.  
  30.  
  31. $FindText = "FirstName"
  32. $MatchCase = $False
  33. $MatchWholeWord = $true
  34. $MatchWildcards = $False
  35. $MatchSoundsLike = $False
  36. $MatchAllWordForms = $False
  37. $Forward = $True
  38. $Wrap = $wdFindContinue
  39. $Format = $False
  40. $wdReplaceNone = 0
  41. $FirstName = Read-Host -Prompt "Input First Name"
  42. $ReplaceWith = $FirstName
  43. $wdFindContinue = 1
  44. $wdReplaceAll = 2
  45.  
  46. $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
  47. $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
  48. $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
  49.  
  50.  
  51.  
  52. If ($objSelection.Find.Found)
  53. {
  54. Write-Host("The first name has been set") } Else {
  55. Write-Host("The search text was not found.") }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. $FindText = "LastName"
  63. $MatchCase = $False
  64. $MatchWholeWord = $true
  65. $MatchWildcards = $False
  66. $MatchSoundsLike = $False
  67. $MatchAllWordForms = $False
  68. $Forward = $True
  69. $Wrap = $wdFindContinue
  70. $Format = $False
  71. $wdReplaceNone = 0
  72. $LastName = Read-Host -Prompt "Input Last Name"
  73. $ReplaceWith = $LastName
  74. $wdFindContinue = 1
  75. $wdReplaceAll = 2
  76.  
  77. $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
  78. $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
  79. $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
  80.  
  81.  
  82. If ($objSelection.Find.Found)
  83. {
  84. Write-Host("The Last name has been set") } Else {
  85. Write-Host("The search text was not found.") }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. $FindText = "pswd"
  92. $MatchCase = $True
  93. $MatchWholeWord = $true
  94. $MatchWildcards = $False
  95. $MatchSoundsLike = $False
  96. $MatchAllWordForms = $False
  97. $Forward = $True
  98. $Wrap = $wdFindContinue
  99. $Format = $False
  100. $wdReplaceNone = 0
  101. $Password = Read-Host -Prompt "Input Password"
  102. $ReplaceWith = $Password
  103. $wdFindContinue = 1
  104. $wdReplaceAll = 2
  105.  
  106. $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
  107. $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
  108. $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
  109.  
  110.  
  111. If ($objSelection.Find.Found)
  112. {
  113. Write-Host("The password has been set") } Else {
  114. Write-Host("The search text was not found.") }
  115.  
  116.  
  117.  
  118.  
  119. $FindText = "Title"
  120. $MatchCase = $False
  121. $MatchWholeWord = $true
  122. $MatchWildcards = $False
  123. $MatchSoundsLike = $False
  124. $MatchAllWordForms = $False
  125. $Forward = $True
  126. $Wrap = $wdFindContinue
  127. $Format = $False
  128. $wdReplaceNone = 0
  129. $Title = Read-Host -Prompt "Input Title"
  130. $ReplaceWith = $Title
  131. $wdFindContinue = 1
  132. $wdReplaceAll = 2
  133.  
  134. $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
  135. $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
  136. $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
  137.  
  138.  
  139. If ($objSelection.Find.Found)
  140. {
  141. Write-Host("The Title has been set") } Else {
  142. Write-Host("The search text was not found.") }
  143.  
  144.  
  145. $FindText = "Cell#"
  146. $MatchCase = $False
  147. $MatchWholeWord = $true
  148. $MatchWildcards = $False
  149. $MatchSoundsLike = $False
  150. $MatchAllWordForms = $False
  151. $Forward = $True
  152. $Wrap = $wdFindContinue
  153. $Format = $False
  154. $wdReplaceNone = 0
  155. $Cell = Read-Host -Prompt "Input Cell # ex 000.000.0000 or press enter to leave blank"
  156. $ReplaceWith = $Cell
  157. $wdFindContinue = 1
  158. $wdReplaceAll = 2
  159.  
  160. $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
  161. $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
  162. $Wrap,$Format,$ReplaceWith,$wdReplaceAll)
  163.  
  164.  
  165.  
  166. If ($objSelection.Find.Found)
  167. {
  168. Write-Host("The Cell phone number has been set") } Else {
  169. Write-Host("The search text was not found.") }
  170.  
  171.  
  172.  
  173.  
  174. #Edits the hyperlinks of email addresses
  175.  
  176. $OldHyperlink = “mailto:First.Last@domain.com”
  177. $NewHyperlink = "Mailto:" + $FirstName + "." + $LastName + "@domain.com"
  178.  
  179.  
  180.  
  181. ForEach ($objHyperlink in $objDoc.Hyperlinks)
  182. {
  183.  
  184. If ($objHyperlink.address -eq $OldHyperlink)
  185.  
  186. {
  187.  
  188. $objHyperlink.Address = $objHyperlink.Address -Replace $OldHyperlink, $NewHyperlink
  189.  
  190. }
  191.  
  192.  
  193. }
  194.  
  195.  
  196. $NewUser = $FirstName + "." + $LastName
  197. $NewUserEmail = $NewUser + "domain.com"
  198. $FileName = 'C:\WelcomeLetters\' + $NewUser + '.docx'
  199.  
  200. #Saves the document and quits word
  201. $objdoc.SaveAs([ref] $FileName)
  202. $objword.quit()
  203.  
  204.  
  205.  
  206. #Emails Welcome Letter to New User
  207.  
  208.  
  209. $mycreds = New-Object System.Management.Automation.PSCredential (Get-Credential)
  210.  
  211. $Subject = $NewUser + " Welcome Letter"
  212. $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"
  213.  
  214. $SendEmail = Read-Host -Prompt "Type yes to send an email to the new user"
  215.  
  216. If ($SendEmail -eq "yes")
  217.  
  218. {
  219.  
  220. 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
  221.  
  222. }
  223.  
  224.  
  225.  
  226. #Prints to default printer
  227.  
  228. $Print = Read-Host -Prompt "Type yes to print to your default printer"
  229.  
  230. If ($Print -eq "yes")
  231.  
  232. {
  233.  
  234. Start-Process -FilePath $FileName -Verb print
  235.  
  236. }
Add Comment
Please, Sign In to add comment