2017-01-09 4 views
0

J'ai un fichier texte que je dois ouvrir dans Notepad.exe et demander à l'utilisateur d'y ajouter des entrées. Ensuite, je voudrais imprimer le fichier à AdobePDF lorsque l'utilisateur quitte le fichier.Powershell Ouvrir le fichier texte l'imprimer à la sortie

Voici ce que je dois ouvrir le fichier

Start-Process notepad.exe C:\path\to\text\file\MyTextFile.txt -NoNewWindow -Wait 

Je ne sais pas comment imprimer PDF à la sortie. AdobePDF est une imprimante installée mais ce n'est PAS l'imprimante par défaut. Toute aide ou conseil serait grandement apprécié.

Répondre

0

Vous pouvez essayer quelque chose comme ça,

$TxtFilePath = "C:\folder\txtfile.txt" 
Start-Process notepad.exe $TxtFilePath -NoNewWindow -Wait #Assuming user save the file 
Get-Content -Path $TxtFilePath| Out-Printer PDFCreator #PDFCreator is the printer name for PDF 

--EDIT--

Je ne pouvais pas comprendre une approche directe de conserver le nom de fichier, type de écrit un hack avec ms mot,

Function Save-TxtAsPDF 
{ 
    Param([string] $FilePath, [string] $OutFolder) 

    # Required Word Variables 
    $ExportASPDFConstant = 17 
    $DoNotSaveConstant = 0 

    # Create a hidden Word window 
    $word = New-Object -ComObject word.application 
    $word.visible = $false 

    # Add a Word document 
    $doc = $word.documents.add() 

    # Put the text into the Word document 
    $txt = Get-Content $FilePath 
    $selection = $word.selection 
    $selection.typeText($txt) 

    # Set the page orientation to landscape 
    $doc.PageSetup.Orientation = 1 

    $FileName = (Split-Path -Path $FilePath -Leaf).Replace(".txt",".pdf") 


    $DestinationPath = Join-Path $OutFolder $FileName 

    # Export the PDF file and close without saving a Word document 
    $doc.ExportAsFixedFormat($DestinationPath,$ExportASPDFConstant) 
    $doc.close([ref]$DoNotSaveConstant) 
    $word.Quit() 
} 


$TxtFilePath = "C:\folder\tt.txt" 
$OutFolder = "C:\Outputfolder" 

Start-Process notepad.exe $TxtFilePath -NoNewWindow -Wait #Assuming user save the file 

Save-TxtAsPDF -FilePath $TxtFilePath -OutFolder $OutFolder 

voir si cela aide!

+0

Cela a fonctionné très bien. Cependant, je me demande s'il existe un moyen de conserver le nom du fichier texte d'origine lors de l'enregistrement du pdf? – Eric

+0

Mis à jour la réponse, n'a pas pu trouver une approche directe! Voyez si ça aide! –