december 2008 - Posts

PowerShell: write to word using bookmarks
11 dec 2008 12:00

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()

tile and cascade all your wonderfull windows.
9 dec 2008 23:50

I had to compare some Word documents today, and got annoyed that i had to ALT+TAB all the time to compare.

The tile and cascade options are available for grouped documents, but what i did not know is that you can CTRL mark windows in the taskbar. That way you can tile different application windows.

Just hold CTRL and left click the windows you want to tile or cascade, when you have made your selection right click and chose "Show Windows Stacked" or "Show Windows side by side". That way i could directly compare 2 word documents in 100% view on a monitor with 1680*xxx resolution. high-tech stuff, i know :)

i thought i had seen all of those fancy short-cuts and CTRL+ALT+SHIFT+... oh well.

SP2 for Windows Server 2008/Vista beta (CPP) released
5 dec 2008 14:01

http://technet.microsoft.com/en-us/windows/dd262148.aspx