Powershell write to Word document

12 February,2013 by Jack Vamvas

Question: I need to create  a snapshot of server cpu  information and write into a Microsoft Word Document using Powershell . I’ve read   the Powershell to HTML - SQL Server DBA  post , but the requirement is to output to a  Word Document.

I already have the code to iterate through the server list. The missing part is once on the server , obtaining the server cpu information , creating the Word document and writing into the Word document

Answer: The script below is a template for obtaining server information , creating the Microsoft Word document with a combject and  writing to the Word document with Powershell.

In this example – the $cpu_usage variable capture the current CPU usage. In a similar way you can obtain memory and disk usage details

 

$cpu = gwmi –computername $Server win32_Processor 
$cpu_usage = "{0:0.0} %" -f $cpu.LoadPercentage 

$word = New-Object -ComObject "Word.application"
$doc = $word.Documents.Add()
$doc.Activate()
 
$word.Selection.TypeText("CPU Percentage: $cpu_usage")
$word.Selection.TypeParagraph()
 
$file = "C:\projects\testing\laboratory\test.doc"
$doc.SaveAs([REF]$file)
$Word.Quit() 

 Read More

How to Convert Microsoft Word Format to HTML using Powershell

Export-CSV Powershell

SQL Server – Powershell Excel to HTML

Powershell , Excel charts and data presentation


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Powershell write to Word document


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer