by rambog at 2013-03-13 12:08:01
I have created a .docx from some information contained within VMWare (using cmdlets from PowerCLI). Rather than creating a blank document, I would rather use a word template into which to import the dynamic content. I have looked into OpenXML but can’t seem to get the snapins to register. Any other suggested ways I could create the docx from a pre-existing template? I have toyed with the idea of using the convert-to-xml for the inforamtion and then somehow bringing that into the template (also in xml format) but it may be another rabbitt hole.by rambog at 2013-03-13 12:59:11
Here is the pertinent section of the code I have thus far:
$Team=$Team.ToUpper()
$VMHashTable=@{}
connect-VIServer $VMServer
$MyDocuments=[Environment]::GetFolderPath("MyDocuments")
$TempHash=Import-csv $MyDocuments\scripts\serverrefresh.csv -Header "Servername","ATL"
foreach ($row in $TempHash) {$VMHashTable[$row.Servername]=$row.ATL}
$ATL=@()
$MyVMs=get-vm|where-object {$_.Folder -like "$Team"}
#Generate Questionnaire
$word=new-object -ComObject "Word.Application"
$doc=$word.documents.Add()
$selection=$word.selection
$selection.TypeText("Date & Time: ")
$selection.TypeText((get-date))
$selection.TypeParagraph()
$selection.Font.Size=16
$selection.TypeText("Team: $Team")
$selection.Font.Size=12
$selection.TypeParagraph()
$selection.TypeParagraph()
$selection.TypeParagraph()
$selection.TypeText("ServerttOperating SystemtttttCluster")<br>foreach ($VM in $MyVMs){<br>#Discover the ATL for all the servers<br><br>$ATLi=$VMHashTable[$VM.name]<br>if ($ATL -notcontains $ATLi) {<br>$ATL+=$ATLi<br>} #end if search of ATL to see if it already contains a unique entry for ATL<br><br>$Cluster=$VM.vmhost.parent.name<br>$selection.TypeParagraph()<br>$selection.Font.Color="wdColorBlack"<br>if ($Cluster -match "Cluster1" -or $Cluster -match "Cluster3") {<br>$selection.Font.Color="wdColorRed"<br>} #end if on the color change<br>$selection.TypeText("$($VM.name)t$($VM.guest.OSFullName)`t$($VM.vmhost.parent.name)")
} #end foreach VM
In doing some more digging, I have discovered that the template (.dotx) has a number of fields defined therein. If I could use that to leverage the import of information, this may be an option.by rambog at 2013-03-15 07:21:26
I have figured out that formfields contained in the template can be addresses as below. The lingering issue is that I would like to modify the color of the text input into some of these fields and the original "$selection.Font.Color="wdColorRed" doesn’t seem to do the trick. Below is the revised code. Note: If I simply put the template in the documents.Add parenthesis, it seems to work fine (as long as I use $doc.SaveAs([ref]"filename.docx") to save to a different filename).
$Team=$Team.ToUpper()
$VMHashTable=@{}
connect-VIServer $VMServer
$MyDocuments=[Environment]::GetFolderPath("MyDocuments")
$TempHash=Import-csv $MyDocuments\scripts\serverrefresh.csv -Header "Servername","ATL"
foreach ($row in $TempHash) {$VMHashTable[$row.Servername]=$row.ATL}
$ATL=@()
$MyVMs=get-vm|where-object {$_.Folder -like "$Team"}
#Create the document based upon template and populate the Mnemonic
$word=new-object -ComObject "Word.Application"
$doc=$word.documents.Add("$MyDocuments\ServerInfo\Questionnaire.dotx")
$selection=$word.selection
$AppTeam=$doc.Bookmarks.Item("Text128").Range
$AppTeam.Text="$Team"