如何在PowerShell中解决此错误?异常设置" Rtf":"创建窗口句柄时出错。"

时间:2016-08-17 16:38:02

标签: powershell

这对SQL没有任何问题。它是在创建rtf对象时的。

我正在连接到sql数据库并提取信息。一些信息是html,rtf和纯文本。跑了大约10分钟后,我得到了这个:

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

还有其他人遇到过这个问题吗?

这是脚本本身。

#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 个答案:

答案 0 :(得分:0)

不是一个真正的答案,但建议您将参数转换为Param块。如果您想从命令行调用脚本或将其转换为函数,那将更有用。

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

答案 1 :(得分:0)

我明白了。通过在开头创建我的RTF对象的一个​​实例,它纠正了我的错误。

#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"