2015-02-08 106 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