Kelechi93 Posted January 23, 2020 Posted January 23, 2020 HELP!!!!!!!!! I'm trying to upload a powershell script i made via Group Policy Management to generate email signatures for Staff via Active Directory, but every time i upload the script on and login, all i get is a black screen and when i remove it, it goes back to normal. I've checked Event Viewer and can't seem to find the source, any one have any ideas???????? Here's the script below if anyone whose curious: #Custom variables $SignatureName = 'Signature' $SigSource = "\\PREP-AD02\AD Resources$\Signature.docx" #Path to the *.docx file$SignatureVersion = "1.7" #Change this if you have updated the signature. If you do not change it, the script will quit after checking for the version already on the machine $ForceSignature = '1' #Set to 1 if you don't want the users to be able to change signature in Outlook #Environment variables $AppData=(Get-Item env:appdata).value $SigPath = '\Microsoft\Signatures' #Path to signatures. $LocalSignaturePath = $AppData+$SigPath $RemoteSignaturePathFull = $SigSource #Check signature path (needs to be created if a signature has never been created for the profile if (-not(Test-Path -path $LocalSignaturePath)) { New-Item $LocalSignaturePath -Type Directory} #Copy version file If (-not(Test-Path -Path $LocalSignaturePath\$SignatureVersion)) { If (Test-Path HKCU:'\Software\Microsoft\Office\16.0') { $ForceKeyPathReply = (Get-ItemProperty 'HKCU:\Software\Microsoft\Office\16.0\Common\MailSettings').ReplySignature -eq $null $ForceKeyPathNew = (Get-ItemProperty 'HKCU:\Software\Microsoft\Office\16.0\Common\MailSettings').NewSignature -eq $null If ($ForceKeyPathReply -eq $False) {Remove-ItemProperty -path 'HKCU:\Software\Microsoft\Office\14.0\Common\MailSettings' -name ReplySignature} Else {Write-Host "The reg value does not exist"} If ($ForceKeyPathNew -eq $False) {Remove-ItemProperty -path 'HKCU:\Software\Microsoft\Office\14.0\Common\MailSettings' -name NewSignature} Else {Write-Host "The reg value does not exist"} } If ($ForceSignature -eq '1') { Get-ChildItem -Path $LocalSignaturePath -Recurse | foreach {remove-item $_.fullname -recurse -force -ErrorAction SilentlyContinue} } New-Item -Path $LocalSignaturePath\$SignatureVersion -ItemType Directory } Elseif (Test-Path -Path $LocalSignaturePath\$SignatureVersion) { break } #Get Active Directory information for current user $UserName = $env:username $Filter = "(&(objectCategory=User)(samAccountName=$UserName))" $Searcher = New-Object System.DirectoryServices.DirectorySearcher $Searcher.Filter = $Filter $ADUserPath = $Searcher.FindOne() $ADUser = $ADUserPath.GetDirectoryEntry() $ADDisplayName = $ADUser.DisplayName $ADEmailAddress = $ADUser.mail $ADTitle = $ADUser.title $ADTelePhoneNumber = $ADUser.TelephoneNumber $ADModify = $ADUser.whenChanged #Copy signature templates from source to local Signature-folder Write-Output "Copying Signatures" Copy-Item "$Sigsource" $LocalSignaturePath -Recurse -Force $ReplaceAll = 2 $FindContinue = 1 $MatchCase = $False $MatchWholeWord = $True $MatchWildcards = $False $MatchSoundsLike = $False $MatchAllWordForms = $False $Forward = $True $Wrap = $FindContinue $Format = $False #Insert variables from Active Directory to rtf signature-file $MSWord = New-Object -ComObject word.application $fullPath = $LocalSignaturePath+'\'+$SignatureName+'.docx' $MSWord.Documents.Open($fullPath) #User Name $FindText = "DisplayName"$ReplaceText = $ADDisplayName.ToString()$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll ) #Title $FindText = "Title" $ReplaceText = $ADTitle.ToString() $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll ) #Email Address $FindText = "e-mail" $ReplaceText = $ADEmailAddress.ToString() $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll ) #Save new message signature Write-Output "Saving signatures"#Save HTML $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatHTML"); $path = $LocalSignaturePath+'\'+$SignatureName+".htm" $MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat) #Save RTF $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatRTF"); $path = $LocalSignaturePath+'\'+$SignatureName+".rtf" $MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat) #Save TXT $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatText"); $path = $LocalSignaturePath+'\'+$SignatureName+".txt" $MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$SaveFormat) $MSWord.ActiveDocument.Close() $MSWord.Quit() #Office 2016 signature If (Test-Path HKCU:Software\Microsoft\Office\16.0) { If ($ForceSignature -eq '0') { $ForceKeyPathReply = (Get-ItemProperty 'HKCU:\Software\Microsoft\Office\16.0\Common\MailSettings').ReplySignature -eq $null $ForceKeyPathNew = (Get-ItemProperty 'HKCU:\Software\Microsoft\Office\16.0\Common\MailSettings').NewSignature -eq $null If ($ForceKeyPathReply -eq $False) {Remove-ItemProperty -path 'HKCU:\Software\Microsoft\Office\16.0\Common\MailSettings' -name ReplySignature} Else {Write-Host "The reg value does not exist"} If ($ForceKeyPathNew -eq $False) {Remove-ItemProperty -path 'HKCU:\Software\Microsoft\Office\16.0\Common\MailSettings' -name NewSignature} Else {Write-Host "The reg value does not exist"} $MSWord = New-Object -comobject word.application $EmailOptions = $MSWord.EmailOptions $EmailSignature = $EmailOptions.EmailSignature $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries $EmailSignature.NewMessageSignature=$SignatureName $EmailSignature.ReplyMessageSignature=$SignatureName $MSWord.Quit() } If ($ForceSignature -eq '1') { $MSWord = New-Object -comobject word.application $EmailOptions = $MSWord.EmailOptions $EmailSignature = $EmailOptions.EmailSignature $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries $EmailSignature.NewMessageSignature=$SignatureName $EmailSignature.ReplyMessageSignature=$SignatureName $MSWord.Quit() If (Get-ItemProperty -Name 'NewSignature' -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings') { } Else { New-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'NewSignature' -Value $SignatureName -PropertyType 'String' -Force } If (Get-ItemProperty -Name 'ReplySignature' -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings') { } Else { New-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'ReplySignature' -Value $SignatureName -PropertyType 'String' -Force } } }
thimon Posted January 23, 2020 Posted January 23, 2020 Call the powershell script via a batch file, if you want it completely invisible use a vbs script instead. Batch file will look like this: %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "FILE LOCATION" 1
Kelechi93 Posted January 23, 2020 Author Posted January 23, 2020 Much Obliged, it's not too big an issue placing that anywhere on my script is there?????
thimon Posted January 23, 2020 Posted January 23, 2020 The below is all you put in a .bat batch file, you just need to enter the location of the .ps1 PowerShell script file. All should work fine as long as the PowerShell scripting is good. Does it work okay if you open PowerShell as the user and enter in the PowerShell script contents? %SystemRoot%\syswow64\WindowsPowerShell\v1.0\power shell.exe -ExecutionPolicy Bypass -File "FILE LOCATION"
thebatman Posted January 23, 2020 Posted January 23, 2020 We use a VBS script that runs as a logon script in Group Policy Manager, this way we don't need to change it for different types of Outlook. On Error Resume NextSet objSysInfo = CreateObject("ADSystemInfo")strUser = objSysInfo.UserName Set objUser = GetObject("LDAP://" & strUser)strName = objUser.FullName strTitle = objUser.Title strInitials = objUser.initialsstrFirstName = objUser.FirstNamestrLastName = objUser.LastNamestrDepartment = objUser.Department strCompany = objUser.Company strPhone = objUser.telephoneNumber strMobile = objUser.mobile ' Make text file in users home dir to allow them to add what book they are reading Set objFSO=CreateObject("Scripting.FileSystemObject")strCurrentlyReadingFile = "N:\!CurrentlyReading.txt"If objFSO.FileExists(strCurrentlyReadingFile) ThenElseSet objFileToMake = objFSO.CreateTextFile(strCurrentlyReadingFile,True)objFileToMake.Write "Please type the name of the book you are currently reading in the line below." & vbCrLfobjFileToMake.CloseSet objFileToMake = NothingEnd IfConst ForReading = 1Set objFileToRead = objFSO.OpenTextFile(strCurrentlyReadingFile, ForReading)For i = 1 to 1objFileToRead.ReadLineNextstrCurrentlyReading = objFileToRead.ReadLineobjFileToRead.CloseSet objFileToRead = NothingSet objWord = CreateObject("Word.Application")Set objDoc = objWord.Documents.Add() Set objSelection = objWord.SelectionSet objEmailOptions = objWord.EmailOptions Set objSignatureObject = objEmailOptions.EmailSignatureSet objSignatureEntries = objSignatureObject.EmailSignatureEntriesobjSelection.Style = "No Spacing"objSelection.Font.Bold = TrueobjSelection.Font.Name = "Arial"objSelection.Font.Size = 12objSelection.Font.Color = RGB(0,0,0)'InitialsIf strInitials = "" ThenElsestrInitials = strInitials & " "End If'First NameIf strFirstName = "" ThenElsestrFirstName = strFirstName & " "End If'Last NameIf strLastName = "" ThenElsestrLastName = strLastName & " "End If'Full NameobjSelection.Font.Bold = TrueobjSelection.TypeText strInitials & strFirstName & strLastName & vbLFobjSelection.Font.Bold = False'Job TitleIf strTitle = "" ThenElseobjSelection.TypeText strTitle & vbLFEnd If'DepartmentIf strDepartment = "" ThenElseobjSelection.TypeText strDepartment & vbLFEnd IfobjSelection.TypeText vbLF'Direct NumberIf strPhone = "" ThenElseobjSelection.Font.Bold = TrueobjSelection.TypeText "Direct: "objSelection.Font.Bold = FalseobjSelection.TypeText strPhone & vbLFEnd If'I am currently readingIf strCurrentlyReading = "" ThenElseobjSelection.Font.Name = "Arial"objSelection.Font.Size = 12objSelection.Font.Color = RGB(0,0,0)objSelection.Font.Bold = TrueobjSelection.TypeText "I am currently reading: "objSelection.Font.Bold = FalseobjSelection.TypeText strCurrentlyReading & vbLFEnd If'comment out the next lines to remove "Respect the work-life balance"objSelection.Font.Bold = FalseobjSelection.Font.Name = "Arial"objSelection.Font.Size = 10objSelection.Font.Color = RGB(0,170,240)objSelection.TypeText vbLFobjSelection.TypeText "Schools Name respects the work-life balance of its staff, who are not expected to read or respond to emails outside of their working hours."objSelection.TypeText vbLF & vbLF'comment out to remove school logo and address.objSelection.Font.Bold = FalseobjSelection.Font.Name = "Georgia"objSelection.Font.Size = 10objSelection.Font.Color = RGB(0,0,0)Set colShapes = objDoc.ShapesSet objShape = objSelection.InlineShapes.AddPicture("https://schoolsname.sch.uk/EMailFooter/Signature.jpg")Set objSelection = objDoc.Range()objSignatureEntries.Add "AD Signature", objSelection objSignatureObject.NewMessageSignature = "AD Signature"objSignatureObject.ReplyMessageSignature = "AD Signature"objDoc.Saved = True objWord.Quit
Kelechi93 Posted January 23, 2020 Author Posted January 23, 2020 so my path would be: \\PREP-AD02\AD Resources$\Signature.docx - ExecutionPolicy Bypass -File "Signature.ps1" (something like this????)
thimon Posted January 23, 2020 Posted January 23, 2020 (edited) No. You only need to enter the location of your .ps1 file after the -File switch, I've put an example location in %SystemRoot%\syswow64\WindowsPowerShell\v1.0\power shell.exe -ExecutionPolicy Bypass -File "C:\SignatureScript.ps1" Put that into a batch file, run it normally as the user, not as an Admin. Edited January 23, 2020 by thimon 1
Kelechi93 Posted January 23, 2020 Author Posted January 23, 2020 ahhhhhh i see, i just wanted to make sure, i'll give this a whirl and let you guys know how i get on
Kelechi93 Posted February 12, 2020 Author Posted February 12, 2020 No. You only need to enter the location of your .ps1 file after the -File switch, I've put an example location in %SystemRoot%\syswow64\WindowsPowerShell\v1.0\power shell.exe -ExecutionPolicy Bypass -File "C:\SignatureScript.ps1" Put that into a batch file, run it normally as the user, not as an Admin. So i've put the batch file path on the GPO and i've logged in as a normal user expecting it to just run as normal. The expected result is that it should have run the script on startup and generated the signature but nothing's appeared when i click on the Signature section in Outlook. Any advice would be greatly appreciated.
thebatman Posted February 12, 2020 Posted February 12, 2020 Have you put it as a user or computer policy, this will need to be in the user policy side.
Kelechi93 Posted February 12, 2020 Author Posted February 12, 2020 Have you put it as a user or computer policy, this will need to be in the user policy side. That's correct, i've double-checked
thebatman Posted February 12, 2020 Posted February 12, 2020 Can you post your PS1 file as an attachment so I can have a look at it at test it here to see what is happening?
Kelechi93 Posted February 12, 2020 Author Posted February 12, 2020 Can you post your PS1 file as an attachment so I can have a look at it at test it here to see what is happening? There you go. Much Obliged for the assistance.Signature.ps1
thebatman Posted February 13, 2020 Posted February 13, 2020 EmailSig.docx When testing, I didn't need any Parameters set, what I did do was put the ps1 file into the PowerShell folder for that GPO. (Clicking Show Files and pasting the ps1 file in that folder)
Kelechi93 Posted February 13, 2020 Author Posted February 13, 2020 [ATTACH]56715[/ATTACH] When testing, I didn't need any Parameters set, what I did do was put the ps1 file into the PowerShell folder for that GPO. (Clicking Show Files and pasting the ps1 file in that folder) Thanks for the tip. Just out of curiosity what was your end result????? did Outlook generate a signature for you????? At the moment, i've got these files stored in a hidden share atm i'm generating the signature from there locally to my PC for testing purposes, should i move these files to the powershell folder as well?
thebatman Posted February 13, 2020 Posted February 13, 2020 Outlook did generate a signature for me, I had the file in the same folder and had ".\Signature.docx" set in path but you should also be able to have it set to a share too "\\ShareName\Share$\Signature.docx". Have you been changing the $SignatureVersion number every time you save the file? if you don't it will just ignore any changes you have made. 1
Kelechi93 Posted February 13, 2020 Author Posted February 13, 2020 Outlook did generate a signature for me, I had the file in the same folder and had ".\Signature.docx" set in path but you should also be able to have it set to a share too "\\ShareName\Share$\Signature.docx". Have you been changing the $SignatureVersion number every time you save the file? if you don't it will just ignore any changes you have made. ah, i didn't think i had to because i wasn't changing the code directly, i'll change the version number and see how that goes.
Kelechi93 Posted February 13, 2020 Author Posted February 13, 2020 Outlook did generate a signature for me, I had the file in the same folder and had ".\Signature.docx" set in path but you should also be able to have it set to a share too "\\ShareName\Share$\Signature.docx". Have you been changing the $SignatureVersion number every time you save the file? if you don't it will just ignore any changes you have made. This is the story so far: I've even changed the Version in the code itself, the script runs fine but the Signature's not appearing. Is there anything i'm missing in particular?????
thebatman Posted February 13, 2020 Posted February 13, 2020 You don't need to have the Signature.docx setting in the script tab. The bit you need to link to the Signature.dox file is in the PS1 file at the top $SigSource = "C:\AD Resources\Signature.docx" #Path to the *.docx file or if it is in the same folder as the ps1 file $SigSource = ".\Signature.docx" #Path to the *.docx file What have you got in the Signature.docx file? the links are case sensitive ( DisplayName Title e-mail ) and don't forget to edit the Version Number every time you change the Signature.docx or Signature.ps1 file $SignatureVersion = "1.24"
Kelechi93 Posted February 14, 2020 Author Posted February 14, 2020 You don't need to have the Signature.docx setting in the script tab. The bit you need to link to the Signature.dox file is in the PS1 file at the top $SigSource = "C:\AD Resources\Signature.docx" #Path to the *.docx file or if it is in the same folder as the ps1 file $SigSource = ".\Signature.docx" #Path to the *.docx file What have you got in the Signature.docx file? the links are case sensitive ( DisplayName Title e-mail ) and don't forget to edit the Version Number every time you change the Signature.docx or Signature.ps1 file $SignatureVersion = "1.24" Inside the Signature.docx file is my template of the signature itself with the variable (DisplayName Title e-mail) I've tried to put in one of the changes ($SigSource = ".\Signature.docx" #Path to the *.docx file) changing it word-for-word on the powershell script and changing the version and the script runs when i log in keep getting these kind of errors (so bizarre). I'm pretty sure that i've missed something key, i can't put my finger on it (was never good at writing scripts at university ) What's you take??????????
thebatman Posted February 14, 2020 Posted February 14, 2020 That looks like there is a issue with the way your profiles are setup and trying to save the normal.dotm, we had this issue with sims once. will have a look at our logs on monday to see what i did. 1
Kelechi93 Posted February 14, 2020 Author Posted February 14, 2020 That looks like there is a issue with the way your profiles are setup and trying to save the normal.dotm, we had this issue with sims once. will have a look at our logs on monday to see what i did. You're too kind, THANK YOU SO MUCH, sorry to be a bother
thebatman Posted February 17, 2020 Posted February 17, 2020 After looking into our report, our error was from Egress editing the file and the user not having access to save to that location. You can try some of the options from this link: https://support.microsoft.com/en-gb/help/291352/you-are-prompted-to-save-the-changes-to-the-normal-dot-or-normal-dotm
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now