Advertisement
Combreal

publiPostageWin7.ps1

Sep 13th, 2019
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. This script import a csv file to .
  4.  
  5. .DESCRIPTION
  6. This script import a csv file containing users informations to create pre-constructed letter for each user.
  7. Much like Outlook Publipostage.
  8.  
  9. .PARAMETER CsvPath
  10. Used to specify the path of the Csv file.
  11.  
  12. .PARAMETER LetterPath
  13. Used to specify the path of the letter type file.
  14.  
  15. .PARAMETER OutPutType
  16. Used to specify the type of out put desired.
  17. It can be TextFile or Mail.
  18.  
  19. .PARAMETER OutPutPath
  20. Used to specify the path of the out put files.
  21.  
  22. .EXAMPLE
  23. .\publiPostWin7.ps1 -CsvPath ".\win7users.csv" -LetterPath ".\win7-users.txt"
  24. .\publiPostWin7.ps1 -CsvPath ".\win7users.csv" -LetterPath ".\win7-users.txt" -OutPutType Mail
  25. #>
  26.  
  27. param
  28. (
  29.     [string]$CsvPath = "NotSet",
  30.     [string]$LetterPath = "NotSet",
  31.     [string]$OutPutType = "TextFile",
  32.     [string]$OutPutPath = ".\"
  33. )
  34.  
  35. if($CsvPath -eq "NotSet")
  36. {
  37.     Write-Host "CSV file path not entered"
  38.     Exit
  39. }
  40. if($LetterPath -eq "NotSet")
  41. {
  42.     Write-Host "Letter file path not entered"
  43.     Exit
  44. }
  45.  
  46. $CsvFile = Import-Csv $csvPath
  47. $cred = Get-Credential
  48. $cred = Get-StoredCredential -Target myWinStoredPassword
  49.  
  50. Foreach($user in $CsvFile)
  51. {
  52.     $Letter = get-content $LetterPath
  53.     $Letter = $Letter.replace("@Name@", "$($user.UserName)")
  54.     $Letter = $Letter.replace("@machineName@", "$($user.machineName)")
  55.    
  56.     if($OutPutType -eq "TextFile")
  57.     {
  58.         $FileName = $OutPutPath + "LettreWin7-" + $user.User + ".txt"
  59.         $Letter | Out-File $FileName -Force
  60.         $FileName = $FileName.Remove(0,2)
  61.         Write-Host "$($FileName) created."
  62.     }
  63.     elseif($OutPutType -eq "Mail")
  64.     {
  65.         $FileName = "LettreWin7-" + $user.User
  66.         Send-MailMessage -From "maxime.jolly@domain.fr" -to $user.UserMail -Subject "Mise a jour machine Windows 7" -Body ($Letter | Out-String) -SmtpServer "smtp.domain.fr" -port "587" -UseSsl -Credential $cred
  67.         Write-Host "$($FileName) created."
  68.     }
  69.     else
  70.     {
  71.         Write-Host "Unknown file type specified."
  72.         exit
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement