I needed to create some 5-600 change-request documents for server migration, where some of the information was hostname, IP address and OS version. Server hostname information was already available in txt/xls/csv format, so not wanting to create all thes documents manually (lazy as one is...), i created a change-request "template" (save as ".doc" not ".dot") and defined bookmarks (see this article on how to define bookmarks in Word documents) where the server specific information where to be located.
Armed with .doc file with bookmarks, server lists and PS i was ready to go. OS version and IP address is gathered using WMI.
I have included a $cred value in the first line of the script. This can be uncommented if you want to run the script remotely. Also the -Credential $cred value for the ipaddr and osver functions has to be uncommented for this to work (described in the script itself)
Remember to change the input files, and the output directory to suit your environment. Script is attached as write2word.txt, just rename to .ps1.
# Writes data to Word using bookmarks
# by Rasmus Sjoerslev
# www.it-experts.dk/blogs/rsj
# If you want to specify credentials the WMI command, uncomment the $cred below, and also make sure to uncomment the -Credential $cred in the 2 functions.
#$cred = Get-Credential $User
function ipaddr
{
param([string]$CompName)
$wmi = Get-WmiObject -Query "SELECT * from Win32_NetworkAdapterConfiguration WHERE IPEnabled='true'" -ComputerName $CompName #-Credential $cred
foreach ($nic in $wmi)
{
$nic.IPAddress
}
}
function osver
{
param([string]$CompName)
$osver = Get-WmiObject -Query "SELECT Caption from Win32_OperatingSystem" -ComputerName $CompName #-Credential $cred
$osver.Caption
}
# specify full path to the list of servers you want to scan.
$hostInputFile = "C:\Scripting\servers.txt"
if (! (test-path $hostInputFile))
{
throw "$($hostInputFile) is not a valid path."
}
# specify fill path to the word doc where bookmarks are defined.
$docTemplate = "C:\Scripting\template.doc"
if (! (test-path $docTemplate))
{
throw "$($docTemplate) is not a valid path"
}
$computers = Get-Content $hostInputFile
$msWord = New-Object -Com Word.Application
foreach ($computer in $computers)
{
$wordDoc = $msWord.Documents.Open($docTemplate)
$wordDoc.Activate()
#defines IP address input
$objRange = $wordDoc.Bookmarks.Item("ip").Range
$objRange.Text = ipaddr -CompName $computer
$wordDoc.Bookmarks.Add("ip",$objRange)
#defines OS version input
$objRange = $wordDoc.Bookmarks.Item("osversion").Range
$objRange.Text = osver -CompName $computer
$wordDoc.Bookmarks.Add("osversion",$objRange)
#defines hostname input (from the $computer value above)
$objRange = $wordDoc.Bookmarks.Item("hostname").Range
$objRange.Text = $computer
$wordDoc.Bookmarks.Add("hostname",$objRange)
# Save the document to disk and close it. CHange $filename path to suit your environment.
$filename = "C:\OutputFolder\" + $computer + ".doc"
$wordDoc.SaveAs([REF]$filename)
$wordDoc.Close()
}
$msWord.Application.Quit()