Я пытаюсь создать документ, который можно будет отправить новым сотрудникам с информацией об их новой учетной записи. У меня сценарий устанавливает переменные, которые работают, и генерирует пароль, который работает, а затем выполняет поиск / замену в шаблоне слова, а затем сохраняет как, что работает. У меня проблема в том, что все значения переменных, которые я установил, кажутся преобразованными в «ToUpper» при их нахождении / замене в файле Word. Это проблема c, особенно когда дело касается переменной пароля. Может ли кто-нибудь понять, почему он конвертирует его в верхний, когда он применяется к файлу Word?
[string]$FName = Read-Host -Prompt 'First Name'
[string]$LName = Read-Host -Prompt 'Last Name'
[Int32]$EmpID = Read-Host -Prompt 'Employee ID'
$Name = $FName + ' ' + $LName
$UserName = ($FName.Substring(0,1)+$LName).ToLower()
$Email = ($FName.Substring(0,1)+$LName + 'redacted').ToLower()
#Generates a random password for the user.
function Get-RandomCharacters($length, $characters) {
$random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
$private:ofs=""
return [String]$characters[$random]
}
function Scramble-String([string]$inputString){
$characterArray = $inputString.ToCharArray()
$scrambledStringArray = $characterArray | Get-Random -Count $characterArray.Length
$outputString = -join $scrambledStringArray
return $outputString
}
$password = Get-RandomCharacters -length 3 -characters 'abcdefghiklmnprstuvwxyz'
$password += Get-RandomCharacters -length 1 -characters 'ABCDEFGHKLMNPRSTUVWXYZ'
$password += Get-RandomCharacters -length 2 -characters '1234567890'
$password += Get-RandomCharacters -length 2 -characters '!$%&=?@#'
[string]$password = Scramble-String $password
Write-Host $password
#Sets criteria for find/replace and path variable. Eg #FindEmail is replaced with $ReplaceEmail
$Document = "C:\testing\template.docx"
$FindName = "FULLNAME"
$ReplaceName = "$Name"
$FindFname = "FNAME"
$ReplaceFname = "$FName"
$FindUPN = "UNAME"
$ReplaceUPN = "$UserName"
$FindPasswd = "PASSWD"
$ReplacePasswd = "$password"
$FindEmail = "UPN"
$ReplaceEmail = "$Email"
$FindEmpID = "EMPLOYEEID"
$ReplaceEmpID = "$EmpID"
$SaveAS = "C:\testing\" + $UserName + '.docx'
$ReplaceAll = 2
$FindContinue = 1
$MatchCase = $False
$MatchWholeWord = $True
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $FindContinue
$Format = $True
$Word = New-Object -comobject Word.Application
$Word.Visible = $False
$OpenDoc = $Word.Documents.Open($Document)
$Selection = $Word.Selection
#Replaces Full Name
$Selection.Find.Execute(
$FindName,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceName,
$ReplaceAll
) | Out-Null
#Replaces First Name
$Selection.Find.Execute(
$FindFname,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceFname,
$ReplaceAll
) | Out-Null
#Replaces Email address
$Selection.Find.Execute(
$FindUPN,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceUPN,
$ReplaceAll
) | Out-Null
#Replaces Password
$Selection.Find.Execute(
$FindPasswd,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplacePasswd,
$ReplaceAll
) | Out-Null
#Replaces Email
$Selection.Find.Execute(
$FindEmail,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceEmail,
$ReplaceAll
) | Out-Null
#Replaces Password
$Selection.Find.Execute(
$FindPasswd,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplacePasswd,
$ReplaceAll
) | Out-Null
#Replaces EmployeeID
$Selection.Find.Execute(
$FindEmpID,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceEmpID,
$ReplaceAll
) | Out-Null
$OpenDoc.SaveAs($SaveAS)
$OpenDoc.Close()
$Word.quit()