填充数据网格视图

时间:2015-02-08 14:56:13

标签: powershell datagridview

我正在处理一个表单,该表单将搜索所有已连接的驱动器以查找PST文件。

我可以使用以下命令: -

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)

$array=@()

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

安迪

1 个答案:

答案 0 :(得分: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)

$array=@()

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

使用multiselect并返回

$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函数在那里但在第二个代码示例中省略以节省空间。

相关问题