Hi all,
The companyn I work for has a service sheet that is in Word, with loads of tables (just how it’s done)…my issue is, I can create tables from Powershell to Word, but can’t see a way around using get-content (which has no way to format the contents) whih is causing me problems…i would like to output the get-content into 1 cell, but formatted in column lines…Is ther another way to do ths, that I’m missing? Any help greatly appreciated…
# Disk Info:
$CSV = "C:\Test\Disk.csv"
$Disk =(Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} |
Sort-Object -Property Name |
Select-Object Name,
@{"Label"=" Size(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}},
@{"Label"=" Free(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}},
@{"Label"=" %Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} | Export-CSV -delimiter “,” $CSV -noTypeInformation)
(Get-Content C:\Test\Disk.csv) | % {$_ -replace '"', ""} | out-file -FilePath C:\Test\Disk.csv -Force -Encoding ascii
(Get-Content C:\Test\Disk.csv) | % {$_ -replace ',', " "} | out-file -FilePath C:\Test\Disk.csv -Force -Encoding ascii
$Info = (Get-Content $CSV)
$word = New-Object -ComObject word.application
$word.visible = $true
$word.application.DisplayAlerts = 0
$doc = $word.documents.add()
$selection = $word.selection
$selection.Font.Name = "Arial"
$selection.Font.Size = 14
$selection.Font.Bold = $true;
$selection.Font.Color = 255
$selection.typeText("This is a Test")
$selection.paragraphFormat.Alignment = 1
$selection.TypeParagraph()
$selection.Font.Name = "Arial"
$selection.Font.Size = 10
$selection.Font.Color = 1
$selection.paragraphFormat.Alignment = 2
$selection.Font.Bold = $false;
$selection.TypeText($fDATE);$selection.TypeParagraph()
$selection.Font.Name = "Arial"
$selection.Font.Size = 11
$selection.paragraphFormat.Alignment = 1
$selection.TypeParagraph()
$number_Of_Rows = 3;$number_Of_Columns = 3
$paragraph = $doc.Content.Paragraphs.Add()
$Range = $paragraph.Range
$Table = $doc.Tables.Add($Range,$number_Of_Rows,$number_Of_Columns)
$Table = $doc.Tables.item(1)
$Table.Borders.OutsideLineStyle = 1
$Table.Borders.InsideLineStyle = 1
$Table.shading.BackgroundPatternColor = $NTS
$Table.Cell(1,1).Range.Font.Bold = $True
$Table.Cell(1,1).Range.Font.Italic = $True
$Table.Cell(1,1).Range.Font.Name =”Calibri”
#$Table.Cell(1,1).Range.shading.BackgroundPatternColor = $NTS
#$Table.Cell(1,3).Range.shading.BackgroundPatternColor = $NTS
$Table.Cell(1,3).Range.Font.Name =”Calibri”
$Table.Cell(1,2).Range.Font.Bold = $True
$Table.Cell(1,3).Range.Font.Bold = $True
$Table.Cell(1,1).PreferredWidthType = 3
$Table.Cell(1,1).Width = 50
$Table.Cell(1,2).PreferredWidthType = 3
$Table.Cell(1,2).Width = 75
$Table.Cell(1,3).PreferredWidthType = 3
$Table.Cell(1,3).Width = 200
$Table.Cell(2,1).PreferredWidthType = 3
$Table.Cell(2,1).Width = 50
$Table.Cell(2,2).PreferredWidthType = 3
$Table.Cell(2,2).Width = 75
$Table.Cell(2,3).PreferredWidthType = 3
$Table.Cell(2,3).Width = 200
$Table.Cell(3,1).PreferredWidthType = 3
$Table.Cell(3,1).Width = 50
$Table.Cell(3,2).PreferredWidthType = 3
$Table.Cell(3,2).Width = 75
$Table.Cell(3,3).PreferredWidthType = 3
$Table.Cell(3,3).Width = 200
$Table.Cell(1,1).Range.Text = "$Date"
$Table.Cell(1,2).Range.Text = "COL 2"
$Table.Cell(1,3).Range.Text = "$Info"
$Table.Cell(1,3).Range.Font.Name = "$Arial"
$Table.Cell(1,3).Range.Font.Size = 10
$selection.Font.Bold = $false;
$Table.Cell(2,1).Range.Text = "A Value"
$Table.Cell(2,2).Range.Text = "B Value"
$Table.Cell(2,3).Range.Text = "C Value"
$Table.Cell(3,1).Range.Text = "D Value"
$Table.Cell(3,2).Range.Text = "E Value"
$Table.Cell(3,3).Range.Text = "F Value"
$selection.EndKey(6)
$selection.TypeParagraph()
As you can see, I’ve tried ‘formatting’ spaces to achieve the results, but a bit flakey to be honest!
I would look into Excel output, to create the tables, then transfer to Word, but haven’t had any experience with Excel and Powershell - yet!