Question retranscrire un code vbs en powershell

Plus d'informations
il y a 6 ans 1 mois #24972 par youssef
Bonjour, je suis en train de convertir mon script vbs en powershell et je suis bloquer sur un point
Le code en vbs que j'aimerais convertir en powershell :

Set WshShell = WScript.CreateObject(\"WScript.Shell\"«»)
Lancer = MsgBox(\"Voulez vous avoir la signature Printemps?\",vbYesNo,\"Signature Printemps\"«»)
If Lancer = vbNo Then
Wscript.Quit
End If
uDepartement = InputBox(\"Indiquez votre fonction\",\"Signature Printemps\",\"exemple: Chef de projet....\",vbOKOnly)
'uStreet = InputBox(\"Indiquez l'emplacement de votre site\",vb\"Exemple: rue de provence\" )
WshShell.Popup \"Merci \"

' # Get user's data from Active Directory
Set objSysInfo = CreateObject(\"ADSystemInfo\"«»)
sUtente = objSysInfo.UserName
Set objUser = GetObject(\"LDAP://\" & sUtente)
uFirstName = objUser.givenName
uName = objUser.sn
'uTitle = objUser.Title
uTelephone = \"Tel. : \" & objUser.TelephoneNumber
if Len(objUser.Mobile)>0 then
uMobile = \" - Mob. : \" & objUser.Mobile
else
uMobile = \"\"
end if
uMail = objUser.mail
uStreet = objUser.StreetAddress
uPostal = objUser.PostalCode
uCity = objUser.l
uDepartement 'Pour afficher le departement des users ex: Direction informatique Production System



' # Create the Word document using COM objects
vBack2Line = chr(11)
vColorBlack = RGB(0,0,0) '6299648
vColorGray = RGB(128,128,128) '8418944
'vCompanyName = \"Printemps\"
'vCompanyUrl = \"www.printemps.com\"
'vCompanyLink = \"http://www.printemps.com\"
If ustreet = \"98 rue de la Victoire\" Then
vLogoImage = \"lien de l'image \"
Else If uStreet =\"haussman\" Then
vLogoImage = \"lien de l'image \"
Else
vLogoImage =\"lien de l'image \"
End if
End if

Set objWord = CreateObject(\"Word.Application\"«»)
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
Set objEmailOptions = objWord.EmailOptions
Set objSignatureObject = objEmailOptions.EmailSignature
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries
objSelection.Font.Name = \"Printania Sans\"
objSelection.Font.Size = 10
objSelection.TypeParagraph()
objSelection.Font.Color = vColorBlack
'objSelection.TypeText \"Cordialement,\"
objSelection.TypeText vBack2Line
objSelection.Font.Bold = True 'youssef
objSelection.TypeText uFirstName & \" \"
'objSelection.Font.Bold = True
objSelection.TypeText uName
objSelection.Font.Bold = False
objSelection.Font.Name = \"Printania Sans Light\"
objSelection.Font.Size = 10
objSelection.TypeText vBack2Line
'objSelection.TypeText uTitle
'objSelection.TypeText vBack2Line
objSelection.TypeText uDepartement 'Youssef
objSelection.TypeText vBack2Line
objSelection.Font.Color = vColorGray
objSelection.TypeText uTelephone & uMobile
objSelection.Font.Color = vColorGray
objSelection.TypeText vBack2Line
objSelection.TypeText uMail'youssef
objSelection.TypeText vBack2Line
objSelection.TypeText uStreet & \" - \" & uPostal & \" \" & uCity
'objSelection.TypeText vBack2Line
'objSelection.TypeText vBack2Line
'Set objLink = objSelection.Hyperlinks.Add(objSelection.Range, vCompanyLink,,, vCompanyUrl)
'objLink.Range.Font.Color = vColorBlue
'objLink.Range.Font.Name = \"Printania Sans Light\" readhost
'objLink.Range.Font.Size = 10
'ObjLink.Range.Font.Bold = true
objSelection.TypeText vBack2Line
objSelection.InlineShapes.AddPicture(vLogoImage)
Set objSelection = objDoc.Range()

' # Set the signature for new mail
TitleNew=vCompanyName & \" Signature Printemps Envoie \"
objSignatureEntries.Add TitleNew, objSelection
objSignatureObject.NewMessageSignature = TitleNew

' # Set the signature for reply
TitleReply=vCompanyName & \" Signature Printemps Reponse\"
objSignatureEntries.Add TitleReply, objSelection
objSignatureObject.ReplyMessageSignature = TitleReply

' # Save the document
objDoc.Saved = True
objWord.Quit

Dim WshShell
Set WshShell = CreateObject(\"WScript.Shell\"«»)
WshShell.RegWrite \"HKCU\SOFTWARE\Microsoft\Office\14.0\Common\MailSettings\NewSignature\", TitleNew, \"REG_EXPAND_SZ\"
WshShell.RegWrite \"HKCU\SOFTWARE\Microsoft\Office\14.0\Common\MailSettings\ReplySignature\", TitleReply, \"REG_EXPAND_SZ\"
'WshShell.RegWrite \"HKCU\SOFTWARE\Microsoft\Office\14.0\Common\General\Signatures\", \"Signatures\", \"REG_SZ\"
'WshShell.RegWrite \"HKCU\SOFTWARE\Microsoft\Windows\CurrrentVersion\Explorer\TypedPaths\url3\", \"%userprofile%\Application Data\Microsoft\Signatures\", \"REG_SZ\"
'Set objSysInfo = nothing

Actuellement je suis bloqué sur la partie en gras que je n'arrive pas a retranscrire en powershell.
Voici mon code en powershell

#variables
$pathPictures = \"printempso365.sharepoint.com/sites/plane...20Victoire.jpg\"; #chemin de l'image sur le réseau

$CompanyName = 'Printemps' #Nom de la compagnie pour nommer le fichier
$AppData=(Get-Item env:appdata).value # valeur du chemin local vers le dossier appdata
$SigPath = '\Microsoft\Signatures' #dossier ou se trouve la signature Outlook
$LocalSignaturePath = $AppData+$SigPath # chemin complet vers la signature Outlook
$SiteWeb = \"www.printemps.fr\" #url du site web

#Recupere les informations de l'utilisateur sur l'ad
$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
$ADFirstName = $ADUser.givenName #Prenom
$ADName = $ADUser.sn #NOM
$ADEmailAddress = $ADUser.mail
$ADTitle = $ADUser.title
$ADTelePhoneNumber = $ADUser.TelephoneNumber
$ADMobile = $ADUser.mobile
$ADDescription = $ADUser.description
$company = $ADUser.company
# Configuration de la signature par défaut pour tout nouveau message
$MSWord = New-Object -com word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
$EmailSignature.NewMessageSignature=$ADDisplayName
$MSWord.Quit()
# Configuration de la signature par défaut pour les réponses ou transfert de message
$MSWord = New-Object -com word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
$EmailSignature.ReplyMessageSignature=$ADDisplayName
$MSWord.Quit()
[ref]$SaveFormat = \"microsoft.office.interop.word.WdSaveFormat\" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $false
$doc = $word.documents.add()

$selection = $word.selection
$selection.font.color = 0

#$selection.ParagraphFormat.linespacing = 0
#$selection.style = \"0 pt\" #\"No Spacing\"
$selection.paragraphs.spaceafter = 0
#Insertion du prenom suivi du nom en majuscule
$selection.font.Name = \"Printania Sans\"
$selection.font.size = 10
$selection.font.bold = 1
$selection.font.Italic = 0
$selection.typeText(\"$ADFirstName\" + \" \" + ($ADName.tostring()).ToUpper() )
#$selection.ParagraphFormat.linespacing = 12
#insertion du titre de la fonction
$selection.TypeParagraph()
$selection.font.Name = \"Printania Sans\"
$selection.font.size = 10

$selection.font.Italic = 0
$selection.typeText($ADTitle)
$string5 = (\" \"«»)

If ($string5 -match '|'){$selection.TypeText($Matches[0])}
$selection.font.name = \"Font du texte\"
$selection.TypeText($string5 -replace '|')


#insertion du telephone
if ($ADTelePhoneNumber.ToString() -ne \"\"«»)
{

$selection.TypeParagraph()
$selection.font.Name = \"Printania Sans Light\"
$selection.font.size = 10
$selection.font.color = 0
$selection.font.bold = 0
$selection.font.Italic = 0
$selection.typeText(\"Tel : +33 (0)\" + $ADTelePhoneNumber)
}
#insertion du Mobile
if ($ADMobile.ToString() -ne \"\"«»)
{
$selection.TypeParagraph()
$selection.font.Name = \"Printania Sans Light\"
$selection.font.size = 10
$selection.font.bold = 0
$selection.font.Italic = 0
$selection.typeText(\"Mobile : +33(0)\" + $ADMobile)
}
#insertion du mail
if ($ADEmailAddress.ToString() -ne \"\"«»)
{
$selection.TypeParagraph()
$selection.font.Name = \"Printania Sans Light\"
$selection.font.size = 10
$selection.font.bold = 0
$selection.font.Italic = 0
$selection.typeText(\"E-Mail : \"«»)
#Add hyperlink in the document
$hlink = $doc.Hyperlinks.Add($selection.Range, \"mailto:\"+\"$ADEmailAddress\",'',\"mailto:\"+\"$ADEmailAddress\",\"$ADEmailAddress\"«»)
#Format the hyperlink
$hlink.Range.Font.Name = 'Printania Sans Light'
$hlink.Range.Font.Size = 10
$selection.TypeParagraph()
}

#insertion de la ligne pour la signature
$selection.TypeParagraph()
$selection.InlineShapes.AddPicture($PathSignaturesLine)
#Insertion de l'image dans le word
$selection.TypeParagraph()
$selection.InlineShapes.AddPicture($pathPictures)
$selection.TypeParagraph()
If (-not (Test-Path \"$LocalSignaturePath\"«»))
{
New-Item -ItemType Directory \"$LocalSignaturePath\"
}
$wdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word' -Passthru
$wdSaveFormat = $wdTypes | Where {$_.Name -eq \"wdSaveFormat\"}
#save as DOCX
$path = \"$LocalSignaturePath\"+ '\' + \"$ADDisplayName\" + \".docx\"
$doc.saveas([ref] \"$path\", [ref]$saveFormat::wdFormatDocument);
$doc.saveas(\"$path\", $saveFormat::wdFormatDocumentDefault);
#save as HTML
$path = \"$LocalSignaturePath\"+ '\' + \"$ADDisplayName\" + \".htm\"
$doc.saveas([ref]$path, [ref]$wdSaveFormat::wdFormatHTML);
$doc.saveas($path, $wdSaveFormat::wdFormatHTML);
#save as RTF
$path = \"$LocalSignaturePath\"+ '\' + \"$ADDisplayName\" + \".rtf\"
$doc.saveas([ref]$path, [ref]$wdSaveFormat::wdFormatRTF);
$doc.saveas($path, $wdSaveFormat::wdFormatRTF);

#Save TXT
$path = \"$LocalSignaturePath\"+ '\' + \"$ADDisplayName\" + \".txt\"
$doc.saveas([ref]$path, [ref]$wdSaveFormat::wdFormatText);
$doc.saveas($path, $wdSaveFormat::wdFormatText);
$doc.close();
$word.Quit();

Si vous avez aussi d'autres conseil je suis preneur
Merci
et bonne journée a tous<br><br>Message édité par: youssefduafg, à: 12/02/18 13:54

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

Plus d'informations
il y a 6 ans 1 mois #24978 par Laurent Dardenne
youssefduafg écrit:

Actuellement je suis bloqué sur la partie en gras

Précise le dans une balise code séparé, le 'gras' ne s'applique pas dans la balise Code, mais Quote.
youssefduafg écrit:

Si vous avez aussi d'autres conseil je suis preneur

Crée des fonctions pour éviter la duplication de code.
Cette écriture est correcte, mais trop verbeuse [code:1]$AppData=(Get-Item env:appdata).value[/code:1]
Puisqu'on préfixe le nom de variable avec le nom du provider, PS appel get-item à notre place :
[code:1]$env:appdata[/code:1]
Dans tes tests vérifie bien que l'instance Word est bien libérée, car VBS gère mieux les objets COM et demande moins de code (pour une fois :-) ).

Et pour ceci :
[code:1]
[ref]$SaveFormat = \&quot;microsoft.office.interop.word.WdSaveFormat\&quot; -as [type]
[/code:1]
L'usage de cette écriture simplifiera ton intention :
[code:1]
[Microsoft.office.interop.word]::WdSaveFormat
#ou ceci, je n'ai pas testé
[Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocument
[/code:1]

Tutoriels PowerShell

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

Plus d'informations
il y a 6 ans 1 mois #24980 par youssef
Merci pour la reponse e code en vbs que j'aimerais bien retranscrire en powershell c'est celui la
[code:1]If ustreet = \&quot;98 rue de la Victoire\&quot; Then

vLogoImage = \&quot;lien de l'image \&quot;

Else If uStreet =\&quot;haussman\&quot; Then

vLogoImage = \&quot;lien de l'image \&quot;

Else

vLogoImage =\&quot;lien de l'image \&quot;

End if[/code:1]

J'ai fait ça en powershell mais ça ne fonctionne pas

[code:1]If ($address = \&quot;hausman\&quot;«»)
{$pathPictures = \&quot;lien de l'image \&quot;} #chemin de l'image sur le réseau
Else if ($address=\&quot;98 rue de la victoire\&quot;«»)
{$pathPictures = \&quot;lien de l'image \&quot;}
Else
{$pathPictures =\&quot;lien de l'image \&quot;}[/code:1]

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

Plus d'informations
il y a 6 ans 1 mois #24985 par Laurent Dardenne
youssefduafg écrit:

J'ai fait ça en powershell mais ça ne fonctionne pas

C'est presque ça, c'est 'ElseIf'.
Tu as les fichiers de doc :
[code:1]
#Une partie de la doc de Powershell
get-help about_*

#Plus précisément
get-help about_if
[/code:1]

Tutoriels PowerShell

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

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