I am running a Powershell script against a view on a Lotus Notes server. It currently produces output in the manner that follows:
UserID|Name|Internet Address
user1|John Smith|john.smith@mycompany.com
user2|Joe Smith|joe.smith@mycompany.com
user3|Bill Smith|bill.smith@mycompany.com
Note: The title line isn't currently part of the output, it is only shown for informational purposes.
The output currently goes to a text file, but I now need to change it to be in XML. The data is extracted using the following Powershell script.
$session = New-Object -ComObject lotus.notessession
$session.initialize()
$session.notesversion
if (Test-Path -Path "c:\myfile.txt") {
Remove-Item -Path "c:\myfile.txt"
}
$db = $session.GetDatabase("lotusServer","names.nsf")
$db.title
$view = $db.GetView('People')
Write-Host "View read : " $view.Name
# Show number of documents
$nbrDocs = $view.AllEntries.Count
Write-Host "Num of Docs : " $nbrDocs
# Get First Document in the View
$doc = $view.GetFirstDocument()
#Read fields for current document
$delim = "|"
$i = 0
while ($doc -ne $null) {
$internetAddress = [string] $doc.GetItemValue("InternetAddress")
$companyname = [string] $doc.GetItemValue("CompanyName")
$fullname = [string] $doc.GetItemValue("FullName")
if ( $companyname -eq "My Company") {
$i = $i + 1
# format the full name field
$fullname = $fullname.Substring(3)
$s = $fullname
$c1 = $s.LastIndexOf(" ")
$c2 = $s.IndexOf("/")
$name = $s.Substring(0,$c2)
$userID = $s.Substring($c1+1)
$zOut = $userID + $delim + $name + $delim + $internetAddress
Write-Host $zOut
Write-Output $zOut| out-file "c:\myfile.txt" -append -width 2000
} #end if
$doc = $view.GetNextDocument($doc)
} #end while
I would like the output to now be in an XML format resembling the following:
<?xml version="1.0" ?>
<Company>
<UserID>user1
<Name>John Smith</Name>
<InternetAddress>john.smith@mycompany.com</InternetAddress>
</UserID>
</Company>
The data shouldn't have any duplicates, and (at some point) there may be multiple companies in the output but overall the structure wouldn't differ drastically from the above sample (Name and InternetAddress are children of the UserID).
However, the way I'm parsing thru the data now (while $doc -ne $null) I am only seeing one row at a time. Examples I've found showing how to export/write to XML do it at one time & doesn't do it a row at a time. The output XML is going to be validated before being used elsewhere. Can I write the output of this script to an XML file, and if so, how?
Any thoughts/suggestions appreciated.
Thanks!