Question [Résolu] Exception lors de l'appel de «ToString»

Plus d'informations
il y a 8 ans 1 mois #25010 par Laurent Dardenne
rico76 écrit:

le faites qu'il remplace les numéros par System.DirectoryServices.PropertyValueCollection - System.DirectoryServices.PropertyValueCollection
Je n'en ai pas la moindre idée ! :S

A priori ton objet contient plusieurs objets, une collection et une chaine, par défaut une collection affiche son nom de classe.
Dans une chaine le contenu d'une variable tableau est automatiquement énuméré :
[code:1]
$Nested=@('Un','deux','trois')
\"$Nested\"
#Un deux trois

$T=@($nested,'06.01.02')
\"$T\"
#System.Object[] 06.01.02
[/code:1]
Depuis le début tes pb sont liès à la structure de l'objet que tu récupères, sous PS d'analyser la structure du/des objets manipulé permet de trouver très souvent l'origine du problème.

Que la propriété AD que tu recherches existe ou pas Powershell en interne semble la garder dans l'objet récupérè et sa structure apparait être un peu mystérieuse :blink:
rico76 écrit:

Ce n'est pas bien grave de toute manière, le principal était le message d'erreur à l'origine.

Dans ton cas oui, si on compte écrire une fonction générique celle-ci doit gérer ce cas.

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 1 mois #25011 par PAWLAK
ah j'en suis pas loin :woohoo:

[code:1]if($ADTelephoneNumber -ne \"\"«»){
$FindText = \"TelephoneNumber\"
$ReplaceText = 'Ligne Directe : '+ $ADTelephoneNumber.ToString()
}
Else{
$FindText = \"TelephoneNumber\"
$ReplaceText = \"\"
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

If ($ADMobile -ne \"\"«»){
$FindText = \"Mobile\"
$ReplaceText = 'Mobile : '+ $ADMobile.ToString()
}
Else {
$FindText = \"Mobile\"
$ReplaceText = \"\"
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

if($ADTelephoneNumber -ne \"\" -and $ADMobile -ne \"\"«»){
$ReplaceText = ' - '
}
Else {}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

[/code:1]

Là il m'affiche bien le tiret uniquement mais il me bouffe mobile :

Ligne Directe : 02 32 32 32 32 - : 06 45 45 45 45


j'en suis pas loin !

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 1 mois #25012 par Laurent Dardenne
rico76 écrit:

j'en suis pas loin !

ça finira bien par tomber en marche :P

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 1 mois #25017 par PAWLAK
J'ai fini par trouver enfin !

Bon finalement je trouve cela un peu bizarre mais je vais pas faire la fine bouche :P

[code:1]if($ADTelephoneNumber -ne \"\"«»){
$FindText = \"TelephoneNumber\"
$ReplaceText = 'Ligne Directe : '+ $ADTelephoneNumber.ToString()
}
Else{
$FindText = \"TelephoneNumber\"
$ReplaceText = ''
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

If ($ADMobile -ne \"\"«»){
$FindText = 'TelephoneMobile'
$ReplaceText = 'TelephoneMobile Mobile : '+ $ADMobile.ToString()
}
Else {
$FindText = \"TelephoneMobile\"
$ReplaceText = ''
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

if($ADTelephoneNumber -ne \"\" + $ADMobile -ne \"\"«»){
$ReplaceText = ' - ' -f $ADTelephoneNumber, $ADMobile
}
Else {
$ReplaceText = ''
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)
[/code:1]

Ce que je trouve bizarre :whistle: c'est que je sois obligé d'y mettre TelephoneMobile avant Mobile : alors que pour TelephoneNumber je n'ai pas besoin de le mettre avant Ligne Directe :.

If ($ADMobile -ne \"\"){
$FindText = 'TelephoneMobile'
$ReplaceText = 'TelephoneMobile Mobile : '+ $ADMobile.ToString()



mais ça fonctionne ainsi, en cas des 2 numéros de téléphone j'ai bien le tiret, en cas d'un des 2 seulement je n'ai pas le tiret, comme je souhaitais :cheer:

Merci à tous les intervenants pour avoir partager des solutions et autres pistes. On est toujours plus fort à plusieurs, merci pour tout. Pour ma part ma GPO signature est terminé ! :woohoo:

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 1 mois #25018 par PAWLAK
Et pour finir si cela peut servir, voici le code complet final permettant grâce à une GPO et du code powershell
de remonter des signatures automatiquement dans les mails sur Microsoft Outlook, 2010, 2013 et 2016.
Les informations des employés sont récupérés dans l'Active Directory.
Petite précision également avec une signature complète pour les nouveaux messages et une signature minime pour les réponses. Ses 2 nouvelles signatures remplacent automatiquement les signatures des employés en cours une fois le script lancé.

je tiens juste à préciser que le code vient pas de moi mais ici , j'ai ajouté la compatibilité avec Office 2010, l'ajout de différente informations employés, l'ajout d'un message réponse, d'autres choses et et l'aide de ce forum :P .

[code:1] $CompanyName = '###'
$TemplateName = 'Nouveau message'
$TemplateName2 = 'Réponse message'
$DomainName = '###' #nom de domaine
$ModulePath = \"\\$DomainName\netlogon\sig_files\$CompanyName\\"
$SigSource = \"\\$DomainName\netlogon\sig_files\$CompanyName\\"
$SetSignatureNew = '1' #Determines wheter to set the signature as the default for new messages on first run. This is overridden if $ForceSignatureNew = 1. 0 = don't set, 1 = set
$SetSignatureReplyForward = '1' #Determines wheter to set the signature as the default for reply/forward messages on first run. This is overridden if $ForceSignatureReplyForward = 1. 0 = don't set, 1 = set
$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
$ADMobile = $ADUser.Mobile #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)

If ($ADDepartment -ne \"\"«») {
$FindText = \"Department\"
$ReplaceText = \"- \" + $ADDepartment.ToString()
}
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($ADTelephoneNumber -ne \"\"«»){
$FindText = \"TelephoneNumber\"
$ReplaceText = 'Ligne Directe : '+ $ADTelephoneNumber.ToString()
}
Else{
$FindText = \"TelephoneNumber\"
$ReplaceText = ''
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

If ($ADMobile -ne \"\"«»){
$FindText = 'TelephoneMobile'
$ReplaceText = 'TelephoneMobile Mobile : '+ $ADMobile.ToString()
}
Else {
$FindText = \"TelephoneMobile\"
$ReplaceText = ''
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

if($ADTelephoneNumber -ne \"\" + $ADMobile -ne \"\"«»){
$ReplaceText = ' - ' -f $ADTelephoneNumber, $ADMobile
}
Else {
$ReplaceText = ''
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll)

$MSWord.ActiveDocument.Save()
$wdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word' -Passthru
$wdSaveFormat = $wdTypes | Where {$_.Name -eq \"wdSaveFormat\"}

#Save HTML
$path = $LocalSignaturePath+'\'+$TemplateName+\".htm\"
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$wdSaveFormat::wdFormatHTML);
$MSWord.ActiveDocument.saveas($path, $wdSaveFormat::wdFormatHTML);

#Save RTF
$path = $LocalSignaturePath+'\'+$TemplateName+\".rtf\"
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref]$wdSaveFormat::wdFormatRTF);
$MSWord.ActiveDocument.SaveAs($path, $wdSaveFormat::wdFormatRTF);

#Save TXT
$path = $LocalSignaturePath+'\'+$TemplateName+\".txt\"
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref]$wdSaveFormat::wdFormatText);
$MSWord.ActiveDocument.SaveAs($path, $wdSaveFormat::wdFormatRTF);

$MSWord.ActiveDocument.Close();
$MSWord.Quit();

#Forcing signature for new messages if enabled
if ($SetSignatureNew -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()
}
}

#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 \"\"«») {
$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()
$wdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word' -Passthru
$wdSaveFormat = $wdTypes | Where {$_.Name -eq \"wdSaveFormat\"}

#Save HTML
$path = $LocalSignaturePath+'\'+$TemplateName2+\".htm\"
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$wdSaveFormat::wdFormatHTML);
$MSWord.ActiveDocument.saveas($path, $wdSaveFormat::wdFormatHTML);

#Save RTF
$path = $LocalSignaturePath+'\'+$TemplateName2+\".rtf\"
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref]$wdSaveFormat::wdFormatRTF);
$MSWord.ActiveDocument.SaveAs($path, $wdSaveFormat::wdFormatRTF);

#Save TXT
$path = $LocalSignaturePath+'\'+$TemplateName2+\".txt\"
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref]$wdSaveFormat::wdFormatText);
$MSWord.ActiveDocument.SaveAs($path, $wdSaveFormat::wdFormatRTF);

$MSWord.ActiveDocument.Close();
$MSWord.Quit();

#Forcing signature for reply/forward messages if enabled
if ($SetSignatureReplyForward -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()
}

}

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 ($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]<br><br>Message édité par: rico76, à: 15/02/18 12:01

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.055 secondes
Propulsé par Kunena