Bienvenue à Blogs CodeS-SourceS Identification | Inscription | Aide

Julien Chable

He blogs, you blog, I blog ...

[Open XML] Use Powershell to secure and exchange your Open XML documents

First, let’s create a profile in order to use PowerTools each time you open a PS console. This is a completely optional step, it’s just for your convenience.

Creating a PowerShell profile

  1. To check if your profile is already set or not, use: test-path $PROFILE
  2. If the previous command returns false, then use the command: new-item –path $PROFILE –itemtype file – force
  3. Once your profile file is created, edit it with notepad (or another text editor) : notepad $PROFILE
  4. Add the command you think you need each time you use PowerShell (specific key stroke, snapin, etc), for example in our case : Set-ExecutionPolicy unrestricted; Add-PSSnapin OpenXml.PowerTools;
  5. Save the file

Using PowerTools to lock your documents (read-only)

To lock a WordprocessingML document in read-only mode, use the Lock-OpenXmlDocument cmdlet. This feature is use to prevent people to modify your document (be careful, this command doesn’t add a password protection, just a section lock). Here is an excerpt from the man (Get-Help command with –Detailed argument) :

SUMMARY 
    Locks one or more Wordprocessing documents.
SYNTAX 
    Lock-OpenXmlDocument [[-SuppressBackups]] [[-PassThru]] [-Document <OpenXmlPackage[]>] [[-Path] <String[]>] [-WhatI
    f] [-Confirm] [<CommonParameters>]
DETAILED DESCRIPTION 
    The Lock-OpenXmlDocument cmdlet sets a lock inside one or more Wordprocessing documents to prevent them from being edited.

ARGUMENTS 
    -SuppressBackups
        Use this switch to avoid generating backup files for documents specified by the Path parameter. It has no affect on objects piped into this command. 
    -Document <OpenXmlPackage[]>
        Specifies the item(s) from the pipeline that will be modified by this command.
    -Path <String[]>
        Specifies the path to the item(s) to lock. Wildcards are permitted. If you specify multiple paths, use commas to separate the paths. 
    --------------  Example 1 --------------
    C:\PS>Lock-OpenXmlDocument -Path test1.docx,test2.docx
    Sets a lock on test1.docx and test2.docx that prevents them from being modified.

Like the example, run the following command (assuming MyDoc.docx is an existing document in the current directory) to lock the specified file:

lock-OpenXmlDocument -Path 'MyDoc.docx'

Here’ the result :

image

Sign your  documents
The need to sign a document seems obvious today, however Open XML is one of the first office document file format to be ready for this feature. Signing a document is a proof that the document is emitted by the person who said he’s the author and that the document has not been altered during the transport over the wire.

The cmdlet Add-OpenXmlDigitalSignature  sign a document by taking the paths of the document to sign and the certificate to use:

Add-OpenXmlDigitalSignature -Path MyDoc.docx' -Certificate 'MyCertificate.pfx'

You can’t use a password protected certificate (hope that this ’bug’ will be resolved soon).

If you want to generate a certificate, use the following commands:

makecert –sv MyKey.pvk –n “CN=<your name>” MyCertificate.cer (when ask for password, don’t enter anything and confirm the “no password protection”)

pvk2pfx –pvk MyKey.pvk –spc MyCertificate.cer –pfx MyCertificate.pfx

image

Pipelining the cmdlets

So far we have seen how to lock and digital sign a document independently. Now, what about pipelining both cmdlets to lock and sign the document at the same time :

lock-OpenXmlDocument –Path “MyDoc.docx” | Add-OpenXmlDigitalSignature –Certificate “MyCertificate.pfx”

With this kind of command line you can lock and sign every Open XML documents you want to exchange with third parties outside your company. Some feature are still missing in PowerTools like personal information removal but these missing cmdlets will come soon with the new PowerTools team (Eric this is for you !). Oh yes, I forgot to tell you, I recently join the PowerTools virtual dev team (and this is really a great team with talented people), so stay tune !

Ce post vous a plu ? Ajoutez le dans vos favoris pour ne pas perdre de temps à le retrouver le jour où vous en aurez besoin :
Posted: mardi 15 juillet 2008 16:52 par neodante
Classé sous : ,

Commentaires

Pas de commentaires

Les commentaires anonymes sont désactivés

Les 10 derniers blogs postés

- Nouveau système d'aide pour Visual Studio 2010 : pour ceux qui n'apprécient pas trop l'absence d'index... par CoqBlog le il y a 1 heure et 8 minutes

- L'interface naturelle de Windows Phone 7 Series par Perspective le il y a 2 heures et 25 minutes

- Comment mapper une vue SQL sur une collection de complex type? par Matthieu MEZIL le 03-19-2010, 21:05

- SQL Server : Query Notification ou comment être notifié de modifications de données côté application (SqlDependency) par SQL Server vu par Christian Robert le 03-19-2010, 15:06

- [WF4] Un Binding Activity/ActivityDesigner qui passe mal? par Blog de Jérémy Jeanson le 03-19-2010, 13:42

- MyTIC – SharePoint 2010 : déjà un mythe Microsoft ? par Le Blog (Vert) d'Arnaud JUND le 03-19-2010, 08:54

- TechDays 2010 Genève : Retrouvez-moi pour une session sur la Haute disponibilité et le ScaleOut avec SQL Server par SQL Server vu par Christian Robert le 03-18-2010, 15:45

- [MIX10] Keynote deuxième journée – Internet Explorer 9, Html5, Visual Studio 2010, OData par Atteint de JavaScriptite Aiguë [Cyril Durand] le 03-17-2010, 19:40

- Certifications beta .NET 4 par Kévin Gosse le 03-17-2010, 19:33

- [Mix 2010] – Microsoft Translator Technology Preview V2 par RedoBlog - The .NET Gentleman !!! le 03-17-2010, 18:53