2016-08-17 82 views
-1

這對SQL沒有任何問題。它是在創建rtf對象時。如何在PowerShell中解決此錯誤?例外設置「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: [email protected] 
$Subject = "[email protected]" 

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

的可能的複製[SQL Server 2008中:錯誤創建窗口句柄(http://stackoverflow.com/questions/37064620/sql-server-2008-error-creating-window-handle) –

+0

不一樣問題 – Fidelis

+0

看起來像是因爲內容太多而導致資源過載是有道理的,但我想不是。 –

回答

0

我想通了。通過在開始時創建一個RTF對象實例來糾正我的錯誤。

#Who are you searching for? 
#Example User ID: [email protected] 
$Subject = "[email protected]" 

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

「'2' {rtf = New-Object -TypeName System.Windows.Forms.RichTextBox $ rtf.Rtf = $ convo.Body $ body = $ rtf。文本 $ rtf.Clear() }' –

0

不是一個真正的答案,而是一個建議,你應該把你的參數變成一個Param塊。如果您想從命令行調用腳本或將其轉換爲函數,它會更有用。

Param (
    # Parameter Subject 
    [Parameter(Mandatory = $true, 
       HelpMessage = 'Who are you searching for? e.g. User ID: [email protected]')] 
    $Subject = '[email protected]', 
    # 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' 
) 
+0

謝謝你的迴應,但是,我的問題是構建對象,並發現當我看着堆時它超載。一旦我發現我創建了一次實例並更新了該實例。 – Fidelis

+0

聽起來很像「SQL Server 2008的可能重複:創建窗口句柄時出錯」 –

+0

不,該問題必須處理MSSQL專用命令行:'執行批處理時發生錯誤。錯誤消息是:創建窗口句柄時出錯。'這個錯誤必須處理輸出。我的腳本收集所有數據並將其存儲到一個沒有問題的數組中。輸出收集並保存良好。 RTF對象實例被重載,因爲我一直在創建實例。通過將其控制爲一個實例,它解決了問題 – Fidelis

相關問題