-1

This has no issue with SQL. it's when creating the rtf object.

I am connecting to a sql database and pulling information. Some of the information is html,rtf, and plain text. After about 10 mins of running I get this:

Exception setting "Rtf": "Error creating window handle."
At line:24 char:76
+ ... Name System.Windows.Forms.RichTextBox; $rtf.Rtf = $convo.Body; $body  ...
+                                            ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting

Has anyone else ran into this issue?

Here is the script itself.

#Who are you searching for? 
#Example User ID: user@domain.com
$Subject = "changeme@domain.com"

#Set the date to search from 
#Example date format: 2016-08-16. 
#Leave it blank if you don't want to search for just dates.
$Date = ""

#Blank array to store the conversation history
$arr = @()

#Lync Archive Server
$SQLSvr = "ServerName Goes Here"

#Lync Archive Database
$Database = "LcsLog"

#Get the UserId's
$UserUri = Invoke-Sqlcmd -Query "Select UserUri,UserId From dbo.Users u;" -ServerInstance $SQLSvr -Database $Database

#Build the Select Statement
$select = "Select * from dbo.Users d left join dbo.Messages m on FromId = d.UserId or ToId = d.UserId Where d.UserUri = '$Subject' "
if($Date)
{
    $select = $select +"and m.MessageIdTime >= '$Date 00:00:01.550' order by m.MessageIdTime asc;"
}
else
{
    $select = $select + "order by m.MessageIdTime asc;"
}

#Get the conversation history
$ConvoData = Invoke-Sqlcmd -Query ($select) -ServerInstance $SQLSvr -Database $Database;

#Loop through each conversation
foreach($convo in $ConvoData)
{
    #Loop through each user.
    foreach($user in $UserUri)
    {
        #Verify the FromId
        if($convo.FromId -eq $user.UserId)
        {
            $FromID = $user.UserUri
        }

        #Verify the ToId
        if($convo.ToId -eq $user.UserId)
        {
            $ToId = $user.UserUri
        }
    }

#Parse the body for legible reading
switch ($convo.ContentTypeId)
{
    '1' {$html = New-Object -ComObject "HTMLFile"; $html.IHTMLDocument2_write($convo.Body);$body = $html.IHTMLDocument2_body.innerText; $html.close();}
    '2' {$rtf = New-Object -TypeName System.Windows.Forms.RichTextBox; $rtf.Rtf = $convo.Body; $body = $rtf.Text; $rtf.Clear();}
    '3' {$body = $convo.Body}
}

    #Build  the Message Output
    $obj = New-Object -TypeName psobject -Property @{User = $Subject; "Message Time" = $convo.MessageIdTime; From = $FromID; To = $ToId; Body = $body}

    #Add data to the array
    $arr += $obj
}

$arr | Select User,"Message Time",From,To,Body | Export-csv "$env:userprofile\desktop\$Subject - conversation report.csv"

2 Answers2

0

Not really an answer, but a recommendation that you should turn your parameters into a Param block. It would be more useful if you wanted to call the script from the command line or turn it into a function.

Param (
    # Parameter Subject
    [Parameter(Mandatory   = $true,
               HelpMessage = 'Who are you searching for? e.g. User ID: user@domain.com')]
    $Subject = 'changeme@domain.com',
    # Parameter Date
    [Parameter(HelpMessage = 'Set the date to search from. e.g. "2016-08-16"')]
    [String]
    $Date,
    # Parameter SQLSvr
    [Parameter(Mandatory   = $true,
               HelpMessage = 'ServerName Goes Here')]
    $SQLSvr,
    # Parameter Database
    [Parameter(Mandatory   = $true,
               HelpMessage = 'Lync Archive Database')]
    $Database = 'LcsLog'
)
Shawn Esterman
  • 2,292
  • 1
  • 10
  • 15
  • Thank you for the reponse, however, my issues was building the object and found out that it was overloading when I looked at the heap. Once I found that I created once instance and just update that instance. –  Aug 17 '16 at 17:23
  • Sounds a lot like "Possible duplicate of SQL Server 2008: Error creating window handle" – Shawn Esterman Aug 17 '16 at 19:23
  • No, that issue has to deal with MSSQL specifically command line: 'An error occurred while executing batch. Error message is: Error creating window handle.' This error had to deal with the output. My script gathers all the data an stores it to an array with no issues. the output is gathered and stored fine. The RTF Object instance was being overloaded because I kept making the instance. By controlling it to one instance it resolved the issue –  Aug 17 '16 at 20:22
  • another note when writing in powershell - you can also call a function like you would writing c# so function sampleFunction($param1, $param2) and it will work from the command line, but that's the lazy way of doing it. –  May 03 '18 at 05:43
0

I figured it out. By creating one instance of my RTF object at the beginning it corrected my error.

#Who are you searching for? 
#Example User ID: user@domain.com
$Subject = "changeme@domain.com"

#Set the date to search from 
#Example date format: 2016-08-16. 
#Leave it blank if you don't want to search for just dates.
$Date = ""

#Blank array to store the conversation history
$arr = @()

#Create RTF and HTML Objects
$html = New-Object -ComObject "HTMLFile";
$rtf = New-Object -TypeName System.Windows.Forms.RichTextBox; 

#Lync Archive Server
$SQLSvr = "Server Name goes here"

#Lync Archive Database
$Database = "LcsLog"

#Get the UserId's
$UserUri = Invoke-Sqlcmd -Query "Select UserUri,UserId From dbo.Users u;" -ServerInstance $SQLSvr -Database $Database

#Build the Select Statement
$select = "Select * from dbo.Users d left join dbo.Messages m on FromId = d.UserId or ToId = d.UserId Where d.UserUri = '$Subject' "
if($Date)
{
    $select = $select +"and m.MessageIdTime >= '$Date 00:00:01.550' order by m.MessageIdTime asc;"
}
else
{
    $select = $select + "order by m.MessageIdTime asc;"
}

#Get the conversation history
$ConvoData = Invoke-Sqlcmd -Query ($select) -ServerInstance $SQLSvr -Database $Database;

#Loop through each conversation
foreach($convo in $ConvoData)
{
    #Loop through each user.
    foreach($user in $UserUri)
    {
        #Verify the FromId
        if($convo.FromId -eq $user.UserId)
        {
            $FromID = $user.UserUri
        }

        #Verify the ToId
        if($convo.ToId -eq $user.UserId)
        {
            $ToId = $user.UserUri
        }
    }

    #Parse the body for legible reading
    switch ($convo.ContentTypeId)
    {
        '1' {$html.IHTMLDocument2_write($convo.Body);$body = $html.IHTMLDocument2_body.innerText; $html.close();}
        '2' {$rtf.Rtf = $convo.Body; $body = $rtf.Text; $rtf.Clear();}
        '3' {$body = $convo.Body}
    }

    #Build  the Message Output
    $obj = New-Object -TypeName psobject -Property @{User = $Subject; "Message Time" = $convo.MessageIdTime; From = $FromID; To = $ToId; Body = $body}

    #Add data to the array
    $arr += $obj
}

$arr | Select User,"Message Time",From,To,Body | Export-csv "$env:userprofile\desktop\$Subject - conversation report.csv"
  • `'2' { $rtf = New-Object -TypeName System.Windows.Forms.RichTextBox $rtf.Rtf = $convo.Body $body = $rtf.Text $rtf.Clear() }` – hdhruna Aug 17 '16 at 17:20