Populating a table in Word

by rambog at 2013-03-22 07:51:50

I have a template in which I need to populate with server information. I am able to access the table but, for some reason, a couple of the columns only have one letter while a couple of others have the information intact. It appears that the columns where only partial information is contained is somehow wrapping the next letter onto the next row. I have mucked with the word warpping of the dotx file to now avail. I am looking at how to properly fit the server names into all the columns of my table. Any suggestions are welcome.

Here is the pertinent section of code:
#Now, let’s generate the surveys
#first, specify location under My Documents
#The information will strictly arise from the csv file and not vmware
#gather the unique occurrances of mnemonics from csv file
$list=Import-csv serverreport.csv
$mnemonics=$list|select mnemonic|get-unique -AsString|foreach {$.Mnemonic}
Write-Debug "$mnemonics=$mnemonics&quot;<br>#Step thru each Mnemonic in the CSV file and create a separate word doc<br>foreach ($app in $mnemonics) {<br>$word=new-object -ComObject &quot;Word.Application&quot;<br>#here is the template file to be used<br>$doc=$word.documents.Add(&quot;$MyDocuments\ServerInfo\ServerInventory.dotx&quot;)<br>Write-Debug &quot;$app=$app"
#assemble all the VMs based upon their roles
$testserverlist=$list|where {$
.Mnemonic -eq $app -and $.Role -eq ‘Test’}|select Servername|foreach {$.Servername}
Write-Debug "$testserverlist=$testserverlist&quot;<br>Write-Debug &quot;Number of test servers=$testserverlist.count&quot;<br>$qaserverlist=$list|where {$_.Mnemonic -eq $app -and $_.Role -eq 'QA'}|select Servername|foreach {$_.Servername}<br>Write-Debug &quot;$qaserverlist=$qaserverlist"
Write-Debug "Number of QA servers=$qaserverlist.count"
$prodserverlist=$list|where {$.Mnemonic -eq $app -and $.Role -eq ‘Production’}|select Servername|foreach {$.Servername}
Write-Debug "$prodserverlist=$prodserverlist&quot;<br>Write-Debug &quot;Number of Prod servers=$prodserverlist.count&quot;<br>$drserverlist=$list|where {$_.Mnemonic -eq $app -and $_.Role -eq 'Disaster Recovery'}|select Servername|foreach {$_.Servername}<br>Write-Debug &quot;$drserverlist=$drserverlist"
Write-Debug "Number of DR servers=$drserverlist.count"
#need to get the amount of rows needed in table from higer value of test,qa,prod or dr servers(used to expand the table, if necessary)
if ($testserverlist.count -ge $qaserverlist.count) {
else {$numberofrows=$qaserverlist.count}
if ($numberofrows -lt $prodserverlist.count) {
if ($numberofrows -lt $drserverlist.count) {

$ATLperson.Text=$List|where {$
.Mnemonic -eq $app}|select ATL|get-unique -AsString|foreach {$.ATL}
#Insert the server information into table
#The server entries begin at the 5th row of the table and the server name entries go from column 2 through 5 on each row
for ($j=1; $j -le $numberofrows; $j++) {
$tablerowposition=$tablerowposition + 1
#add rows if needed to accomodate larger server environments
if ($tablerowposition -ge 9) {
} #end creation of table for servers

#Finish the document and close
} #end foreach Mnemonic

Here is what my output looks like (in the existing table within word):
Test Environment QA Environment Production Environment DR Environment
s server004 server001 s
e server005 server002 e

When it should look like the following:
Test Environment QA Environment Production Environment DR Environment
server00a server004 server001 server003
server005 server002
by ArtB0514 at 2013-03-22 12:59:25
Your code is pretty hard to read as is. Can you use the PowerShell or Code buttons to help?

Are you sure that $TestServerList contains the correct information? Try using this code segment to verify:
$testserverlist=@($list|where {$
.Mnemonic -eq $app -and $_.Role -eq 'Test'}|select -ExpandProperty Servername)
"Number of test servers=$($testserverlist.Count)"

The @() forces the results to be an array, even if 0 or 1 objects are returned. Neither $Null nor a simple string have a Count property, so they will each return $null instead of 0 or 1.

To get a property of an object in a string, you need to tell PowerShell to evaluate it by enclosing the expression in $().
by rambog at 2013-03-27 13:23:03
You solved my problem. In solving it, however, I discovered that servername would spill over into another section of the table in *.dotx. In other words, it appears to all be one table and I need the rows to be added after the first "section" (see section "A1" in attached *.jpg) and leave the other portion of the table alone.

Unfotunately, the place where I am working has blocked all photo sharing sites so I cannot show you what the table in the template looks like before and after the script.