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

Plus d'informations
il y a 8 ans 1 mois #24967 par PAWLAK
Oui désolé pour les extraits de code c'est parce que je peaufine le code au fur et à mesure.

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.

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

Oui désolé pour les extraits de code c'est parce que je peaufine le code au fur et à mesure.

Ton code a déjà fonctionné ou il est en cours de dev ?
rico76 écrit:

par contre je n'ai pas toute les infos renvoyés comme tu l'as demandé.

Difficile de t'aider dans ce cas.

Tutoriels PowerShell

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

Plus d'informations
il y a 8 ans 1 mois #24970 par PAWLAK
Mon code fonctionne correctement oui.

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.

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

Alors est-ce qu'il a du mal a récupérer des chiffres et non du texte ?

Je ne pense pas, mais là le pb est dans le structure de ton objet PS
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.

:P

Tutoriels PowerShell

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

Plus d'informations
il y a 8 ans 1 mois #24973 par Laurent Dardenne
Je me demandais également si le fichier contenant ton code n'a pas un pb d'encodage ou un copier/coller entre 2 sources :
[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.

Plus d'informations
il y a 8 ans 1 mois #24974 par PAWLAK
C'est là ou mes compétences s’arrêtent malheureusement...

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.

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