2015-02-08 116 views
1

我可以在一個窗體,將搜索所有連接的驅動器的PST文件。填充datagridview

我可以得到它使用以下命令工作: -

Get-PSDrive -PSProvider "filesystem"|%{get-childitem $_.root -include *.pst -r}|select name, directoryname, @{name="Size (GB)";expression ={"{0:N2}" -f ($_.length/1GB)}} 

唯一的問題是它需要大約45分鐘通過所有驅動器運行並完成搜索。我想通過使用Windows搜索索引來加快速度。

我有這個....

function Searchindex{ 
$query="SELECT System.ItemName, system.ItemPathDisplay, System.ItemTypeText, System.Size FROM SystemIndex where system.itemtypetext = 'outlook data file'" 
$objConnection = New-Object -ComObject adodb.connection 
$objrecordset = New-Object -ComObject adodb.recordset 
$objconnection.open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';") 
$objrecordset.open($query, $objConnection) 

[email protected]() 

Try { $objrecordset.MoveFirst() } 
Catch [system.exception] { "no records returned" } 
do 
{ 
Write-host ($objrecordset.Fields.Item("System.ItemName")).value ` 
($objrecordset.Fields.Item("System.ItemPathDisplay")).value ` 
($objrecordset.Fields.Item("System.ITemTypeText")).value ` 
($objrecordset.Fields.Item("System.Size")).value 
if(-not($objrecordset.EOF)) {$objrecordset.MoveNext()} 
} Until ($objrecordset.EOF) 


$objrecordset.Close() 
$objConnection.Close() 
$objrecordset = $null 
$objConnection = $null 
[gc]::collect() 
} 

這個輸出在幾秒鐘這是完美的細節畫面,但我不能工作,如何在數據網格視圖中顯示它。

我正在使用原始表單來創建表單。

一旦數據被填充在我希望能夠選擇記錄並將它們複製到新的位置

誰能幫助DataGridView的?

TIA

安迪

回答

0

我不熟悉DataGridView但我覺得,如果你有一個對象,你會得到更好的能力來操縱它。

function Searchindex{ 
$query="SELECT System.ItemName, system.ItemPathDisplay, System.ItemTypeText, System.Size FROM SystemIndex where system.itemtypetext = 'outlook data file'" 
$objConnection = New-Object -ComObject adodb.connection 
$objrecordset = New-Object -ComObject adodb.recordset 
$objconnection.open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';") 
$objrecordset.open($query, $objConnection) 

[email protected]() 

Try { $objrecordset.MoveFirst() } 
Catch [system.exception] { "no records returned" } 
do 
{ 
    $array += [pscustomobject]@{ 
     Name = ($objrecordset.Fields.Item("System.ItemName")).value 
     Path = ($objrecordset.Fields.Item("System.ItemPathDisplay")).value 
     TypeText = ($objrecordset.Fields.Item("System.ITemTypeText")).value 
     Size = ($objrecordset.Fields.Item("System.Size")).value 
    } 

    If(-not($objrecordset.EOF)) {$objrecordset.MoveNext()} 
} Until ($objrecordset.EOF) 


$objrecordset.Close() 
$objConnection.Close() 
$objrecordset = $null 
$objConnection = $null 
[gc]::collect() 

$array 
} 

這將發出一個自定義的PowerShell的對象數組。您已經初始化變量$array。我們只需要填充它。

然後,你可以使用這樣的東西來篩選出你正在尋找的文件。

Searchindex | Out-GridView -PassThru 

打到Ok後,它只會輸出所選記錄。

的DataGridView

與多選,返回

$global:results = @() 

#...searchindex function is here .... 

$form = New-Object System.Windows.Forms.Form 
$form.Size = New-Object System.Drawing.Size(900,600) 
$dataGridView = New-Object System.Windows.Forms.DataGridView 
$dataGridView.Size=New-Object System.Drawing.Size(800,400) 
$dataGridView.SelectionMode = 'FullRowSelect' 
$dataGridView.MultiSelect = $true 
$go = New-Object System.Windows.Forms.Button 
$go.Location = New-Object System.Drawing.Size(300,450) 
$go.Size = New-Object System.Drawing.Size(75,23) 
$go.text = "Select" 
$form.Controls.Add($go) 
$form.Controls.Add($dataGridView) 


$arraylist = New-Object System.Collections.ArrayList 
$arraylist.AddRange((Searchindex)) 
$dataGridView.DataSource = $arraylist 


$dataGridView.Columns[0].width = 240 

$go.Add_Click(
{ 
    $dataGridView.SelectedRows| ForEach-Object{ 
     $global:results += [pscustomobject]@{ 
      Name = $dataGridView.Rows[$_.Index].Cells[0].Value 
      Path = $dataGridView.Rows[$_.Index].Cells[1].Value 
      TypeText = $dataGridView.Rows[$_.Index].Cells[2].Value 
      Size = $dataGridView.Rows[$_.Index].Cells[3].Value 
     } 
     $form.Close() 
    } 

}) 

$form.ShowDialog() 
$global:results 

有很多在這裏介紹,但看的例子,讓我知道這對你的作品。它會將所有選定的行作爲全局變量$global:results中的對象返回。它需要是全球性的,因爲輸出不會在$go.Add_Click之外持續存在。 searchindex函數在那裏,但在第二個代碼示例中省略以節省空間。

+0

這工作得很好謝謝。我怎樣才能得到輸出顯示在列中? – 2015-02-08 16:34:59

+0

你是在談論'DataGridView'還是'Out-GridView'?原因'Out-GridView'默認顯示列? – Matt 2015-02-08 16:59:06

+0

Datagridview理想情況下 – 2015-02-08 17:41:11