Question
[Résolu] Exception lors de l'appel de «ToString»
- PAWLAK
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
- Messages : 17
- Remerciements reçus 0
par contre je n'ai pas toute les infos renvoyés comme tu l'as demandé.
Je te met le code que j'ai en entier (j'ai intentionnellement mis des ### sur mes infos) :
[code:1]
$CompanyName = '###'
$TemplateName = 'Nouveau message'
$TemplateName2 = 'Réponse message'
$DomainName = '###'
$ModulePath = \"\\$DomainName\netlogon\sig_files\$CompanyName\\" #insert log module path
$SigSource = \"\\$DomainName\netlogon\sig_files\$CompanyName\\"
$ForceSignatureNew = '1'
$ForceSignatureReplyForward = '1'
$AppData=(Get-Item env:appdata).value
$SigPath = \"\Microsoft\Signatures\\"
$LocalSignaturePath = $AppData+$SigPath
$RemoteSignaturePathFull = $SigSource+\"$TemplateName.docx\"
$RemoteSignaturePathFull2 = $SigSource+\"$TemplateName2.docx\"
#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 #Fullname
$ADTitle = $ADUser.title #Title
$ADDepartment = $ADUser.Department #Department
$ADCompany = $ADUser.Company #Company
$ADTelePhoneNumber = $ADUser.TelephoneNumber #Telephone
$ADTelephoneMobile = $ADUser.TelephoneMobile #MobileNumber
$ADEmailAddress = $ADUser.mail #E-Mail
$ADModify = $ADUser.whenChanged
$ADModify2 = $ADUser.whenChanged
#Setting registry information for the current user
$CompanyRegPath = \"HKCU:\Software\\"+$CompanyName
$SignatureRegPath = $CompanyRegPath+'\'+$TemplateName
$SignatureRegPath2 = $CompanyRegPath+'\'+$TemplateName2
if (Test-Path $CompanyRegPath)
{}
else
{New-Item -path \"HKCU:\Software\\" -name $CompanyName}
if (Test-Path $SignatureRegPath)
{}
else
{New-Item -path $CompanyRegPath -name $TemplateName}
if (Test-Path $SignatureRegPath2)
{}
else
{New-Item -path $CompanyRegPath -name $TemplateName2}
$SigVersion = (gci $RemoteSignaturePathFull).LastWriteTime
$SigVersion2 = (gci $RemoteSignaturePathFull2).LastWriteTime
#When was the last time the signature was written
$SignatureVersion = (Get-ItemProperty $CompanyRegPath\"\$TemplateName\"«»).SignatureVersion
$SignatureVersion2 = (Get-ItemProperty $CompanyRegPath\"\$TemplateName2\"«»).SignatureVersion
$UserModify = (Get-ItemProperty $SignatureRegPath).UserAccountModifyDate
$UserModify2 = (Get-ItemProperty $SignatureRegPath2).UserAccountModifyDate
$ForcedSignatureNew = (Get-ItemProperty $CompanyRegPath\"\$TemplateName\"«»).ForcedSignatureNew
$ForcedSignatureReplyForward = (Get-ItemProperty $CompanyRegPath\"\$TemplateName2\"«»).ForcedSignatureReplyForward
Set-ItemProperty $CompanyRegPath\"\$TemplateName\" -name SignatureSourceFiles -Value $SigSource
$SignatureSourceFiles = (Get-ItemProperty $CompanyRegPath\"\$TemplateName\"«»).SignatureSourceFiles
Set-ItemProperty $CompanyRegPath\"\$TemplateName2\" -name SignatureSourceFiles -Value $SigSource
$SignatureSourceFiles2 = (Get-ItemProperty $CompanyRegPath\"\$TemplateName2\"«»).SignatureSourceFiles
#Copying signature sourcefiles and creating signature if signature-version are different from local version
if (($SignatureVersion -eq $SigVersion) -and ($UserModify -eq $ADModify)) {}
else
{
#Copy signature templates from domain to local Signature-folder
Copy-Item \"$RemoteSignaturePathFull\" $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 -com word.application
$fullPath = $LocalSignaturePath+\"$TemplateName\"+’.docx’
$MSWord.Documents.Open($fullPath)
$FindText = \"DisplayName\"
$ReplaceText = $ADDisplayName.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
$FindText = \"Title\"
$ReplaceText = $ADTitle.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
$FindText = \"Company\"
$ReplaceText = $ADCompany.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
If ($ADDepartment -ne \"\"«») {
$FindText = \"Department\"
$ReplaceText = \"- \" + $ADDepartment.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
}
Else{
$FindText = \"Department\"
$ReplaceText = \"\"
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
$ReplaceText = $ADEmailAddress.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
if ($MSWord.Selection.Find.Execute(“Email”) ) {
$MSWord.ActiveDocument.Hyperlinks.Add($MSWord.Selection.Range, “mailto:”+$ADEmailAddress.ToString(), $missing, $missing, $ADEmailAddress.ToString())
}
if($null -eq $TelephoneNumber){
$FindText = \"TelephoneNumber\"
$ReplaceText = 'Ligne Directe : '+ $ADTelephoneNumber
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
}
Else{$FindText = \"TelephoneNumber\"
$ReplaceText = \"\"
}
if($null -eq $TelephoneMobile){
$FindText = \"TelephoneMobile\"
$ReplaceText = 'Mobile : '+ $ADTelephoneMobile
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
}
Else{$FindText = \"TelephoneMobile\"
$ReplaceText = \"\"
}
$MSWord.ActiveDocument.Save()
$saveFormat = [Enum]::«»Parse([Microsoft.Office.Interop.Word.WdSaveFormat], \"wdFormatHTML\"«»);
[ref]$BrowserLevel = \"microsoft.office.interop.word.WdBrowserLevel\" -as [type]
$MSWord.ActiveDocument.WebOptions.OrganizeInFolder = $true
$MSWord.ActiveDocument.WebOptions.UseLongFileNames = $true
$MSWord.ActiveDocument.WebOptions.BrowserLevel = $BrowserLevel::wdBrowserLevelMicrosoftInternetExplorer6
$path = $LocalSignaturePath+’\'+$TemplateName+”.htm”
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat)
$saveFormat = [Enum]::«»Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatRTF”);
$path = $LocalSignaturePath+’\'+$TemplateName+”.rtf”
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)
$saveFormat = [Enum]::«»Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatText”);
$path = $LocalSignaturePath+’\’+$TemplateName+”.txt”
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)
$MSWord.ActiveDocument.Close()
$MSWord.Quit()
}
#Copying signature sourcefiles and creating signature if signature-version are different from local version
if (($SignatureVersion2 -eq $SigVersion2) -and ($UserModify2 -eq $ADModify2)) {}
else
{
#Copy signature templates from domain to local Signature-folder
Copy-Item \"$RemoteSignaturePathFull2\" $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 -com word.application
$fullPath = $LocalSignaturePath+\"$TemplateName2\"+’.docx’
$MSWord.Documents.Open($fullPath)
$FindText = \"DisplayName\"
$ReplaceText = $ADDisplayName.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
$FindText = \"Title\"
$ReplaceText = $ADTitle.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
$FindText = \"Company\"
$ReplaceText = $ADCompany.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
If($ADDepartment -ne $null) {
$FindText = \"Department\"
$ReplaceText = \"- \" + $ADDepartment.ToString()
}
Else{$FindText = \"Department\"
$ReplaceText = \"\"
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
$MSWord.ActiveDocument.Save()
$saveFormat = [Enum]::«»Parse([Microsoft.Office.Interop.Word.WdSaveFormat], \"wdFormatHTML\"«»);
[ref]$BrowserLevel = \"microsoft.office.interop.word.WdBrowserLevel\" -as [type]
$MSWord.ActiveDocument.WebOptions.OrganizeInFolder = $true
$MSWord.ActiveDocument.WebOptions.UseLongFileNames = $true
$MSWord.ActiveDocument.WebOptions.BrowserLevel = $BrowserLevel::wdBrowserLevelMicrosoftInternetExplorer6
$path = $LocalSignaturePath+’\'+$TemplateName2+”.htm”
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat)
$saveFormat = [Enum]::«»Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatRTF”);
$path = $LocalSignaturePath+’\'+$TemplateName2+”.rtf”
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)
$saveFormat = [Enum]::«»Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatText”);
$path = $LocalSignaturePath+’\’+$TemplateName2+”.txt”
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)
$MSWord.ActiveDocument.Close()
$MSWord.Quit()
}
Set-ItemProperty $SignatureRegPath -name UserAccountModifyDate -Value $ADModify.ToString()
Set-ItemProperty $SignatureRegPath2 -name UserAccountModifyDate -Value $ADModify2.ToString()
#Stamp registry-values for Outlook Signature Settings if they doesn`t match the initial script variables. Note that these will apply after the second script run when changes are made in the \"Custom variables\"-section.
if ($ForcedSignatureNew -eq $ForceSignatureNew){}
else
{Set-ItemProperty $CompanyRegPath\"\$TemplateName\" -name ForcedSignatureNew -Value $ForceSignatureNew}
if ($ForcedSignatureReplyForward -eq $ForceSignatureReplyForward){}
else
{Set-ItemProperty $CompanyRegPath\"\$TemplateName2\" -name ForcedSignatureReplyForward -Value $ForceSignatureReplyForward}
if ($SignatureVersion -eq $SigVersion){}
else
{Set-ItemProperty $CompanyRegPath\"\$TemplateName\" -name SignatureVersion -Value $SigVersion}
if ($SignatureVersion2 -eq $SigVersion2){}
else
{Set-ItemProperty $CompanyRegPath\"\$TemplateName2\" -name SignatureVersion -Value $SigVersion2}
#Forcing signature for new messages if enabled
if ($ForcedSignatureNew -eq ‘1’)
{
#Set company signature as default for New messages
$MSWord = New-Object -com word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
$EmailSignature.NewMessageSignature=$TemplateName
$MSWord.Quit()
}
#Forcing signature for reply/forward messages if enabled
if ($ForcedSignatureReplyForward -eq ‘1’)
{
#Set company signature as default for Reply/Forward messages
$MSWord = New-Object -com word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
$EmailSignature.ReplyMessageSignature=$TemplateName2
$MSWord.Quit()
}
[/code:1]
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
- Messages : 6311
- Remerciements reçus 68
Ton code a déjà fonctionné ou il est en cours de dev ?Oui désolé pour les extraits de code c'est parce que je peaufine le code au fur et à mesure.
rico76 écrit:
Difficile de t'aider dans ce cas.par contre je n'ai pas toute les infos renvoyés comme tu l'as demandé.
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- PAWLAK
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
- Messages : 17
- Remerciements reçus 0
Sauf si dans l'active directory, il n'y a pas le numéro de téléphone mobile, j'ai donc ce message d'erreur dans mon 1er post.
Par contre si j'ai bien le numéro de téléphone mobile dans l'AD, le code fonctionne correctement. Mais comme dans ma boite certains ont les 2 et d'autres l'un des 2, cela pose donc problème.
Et si les 2 sont renseignés, cela fonctionne aussi.
Bizarrement je n'ai pas d'erreur par exemple pour $ADDepartment si ce n'est pas renseigné.
Alors est-ce qu'il a du mal a récupérer des chiffres et non du texte ?
J'ai trouvé une parade en indiquant les 2 numéros de téléphones dans la ligne $ADTelePhoneNumber mais c'est pas très esthétique dirons nous.
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
- Messages : 6311
- Remerciements reçus 68
Je ne pense pas, mais là le pb est dans le structure de ton objet PSAlors est-ce qu'il a du mal a récupérer des chiffres et non du texte ?
Place des traces Write-debug (ou utilise le debugger de VSCode) pour chacune des lignes demandées, je ne vois pas comment faire autrement pour comprendre ce pb.
rico76 écrit:
J'ai trouvé une parade en indiquant les 2 numéros de téléphones dans la ligne $ADTelePhoneNumber mais c'est pas très esthétique dirons nous.
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
- Messages : 6311
- Remerciements reçus 68
[code:1]
if ($ForcedSignatureReplyForward -eq ‘1’)
...
$path = $LocalSignaturePath+’\'+$TemplateName+”.htm”
[/code:1]
Est-ce que dans la console l'objet récupéré ( avec un code de test simple) s'affiche correctement ?
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- PAWLAK
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
- Messages : 17
- Remerciements reçus 0
Ou placer des Write-debug ?
j'ai effectivement supprimer le code suivant qui n'était plus valide mais sans plus de succès.
[code:1]if ($ForcedSignatureReplyForward -eq ‘1’)[/code:1]
Ce que j'arrive pas à comprendre c'est pourquoi $ADDepartment fonctionne très bien avec ou sans information dans l'AD et pas $ADTelephoneMobile.
Connexion ou Créer un compte pour participer à la conversation.
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- [Résolu] Exception lors de l'appel de «ToString»