首页 > 解决方案 > (Powershell) 文本框搜索文本

问题描述

我有一个列出机器上所有软件的文本框,但我想在它下面有一个搜索栏,让我可以从该列表中搜索东西。

最好我想在我输入时开始解析原始文本框中的项目。但如果它必须有一个搜索按钮,那也很好。

这在 powershell 中也可能不可行,不是 100% 确定的。任何建议都值得赞赏,通常我会给出我尝试过的一行代码,但实际上我在这里不知所措,不知道从哪里开始,因为我尝试过的几件事并没有远程完成我想要的。

- 编辑

$SysInfo = New-Object System.Windows.Forms.Textbox
$SysInfo.Location = New-Object System.Drawing.Point(5,5)
$SysInfo.Size = New-Object System.Drawing.Size(520,290)
$SysInfo.AutoSize = $true
$SysInfo.Multiline = $true
$SysInfo.ScrollBars = "Vertical"
$SysInfo.AcceptsReturn = $true
$SysInfo.AcceptsTab = $false
$SysInfo.WordWrap = $true
$SysInfo.Enabled = $True

$SysInfo.text = Get-WmiObject -Computer ($ComputerName) -Class Win32_Product | Select-Object -Property Name, Version |
            Select-Object -Property * | Sort-Object Name |
            Out-String

--EDIT 使用下面提到的 reddit 帖子。我不能让这些工作。

$Services = Get-Service | Select-object $Fields

$Services = Get-Service -ComputerName $ComputerName | Select-object $Fields

$Services = Get-WmiObject -Computer $ComputerName -Class Win32_Product | Select-Object -Property $Fields |
            Select-Object -Property * | Sort-Object $Fields |
            Out-String

跑步

$Services = Get-Service -Computername $ComputerName| Select-object $Fields

只拉我的机器,它不让我从其他机器上拉信息。它没有抛出任何错误或给我任何调试日志

- -解决了

Function Software-Pull {

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window Name="Form"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="Common Software" Height="488.773" Width="797.65" Icon = "\\bconac01\ds-support\GS_IT\Tools\Test Tools (Alx)\Tool\icon.ico" ShowInTaskbar="False"> 
    <Grid Margin="0,0,-8,-21">
        <DataGrid Name="DataGrid1" HorizontalAlignment="Left" Height="368" VerticalAlignment="Top" Width="772" Margin="10,41,0,0"/>
        <Label Content="Filter" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <TextBox Name="FilterTextBox" HorizontalAlignment="Left" Height="26" Margin="78,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="172"/>
    </Grid>
</Window>
'@

#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form2=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

# Store Form Objects In PowerShell
$xaml.SelectNodes("//*[@Name]") | ForEach-Object{
    Set-Variable -Name ($_.Name) -Value $Form2.FindName($_.Name)
    Write-host $_.Name
}

$Fields = @(
    'Name'
    'Version'
)



$Services = Get-WmiObject -Computer ($prebox.text + $device.text) -Class Win32_Product | Select-object -Property *



# Add Services to a datatable
$Datatable = New-Object System.Data.DataTable
[void]$Datatable.Columns.AddRange($Fields)
foreach ($Service in $Services)
{
    $Array = @()
    Foreach ($Field in $Fields)
    {
        $array += $Service.$Field
    }
    [void]$Datatable.Rows.Add($array)
}
#$filter = "DisplayName LIKE 'B%'"
#$Datatable.DefaultView.RowFilter = $filter

# Create a datagrid object and populate with datatable
$DataGrid1.ItemsSource = $Datatable.DefaultView
$DataGrid1.CanUserAddRows = $False
$DataGrid1.IsReadOnly = $True
$DataGrid1.GridLinesVisibility = "None"

$FilterTextBox.Add_TextChanged({
    $InputText = $FilterTextBox.Text
    $filter = "Name LIKE '$InputText%'"
    $Datatable.DefaultView.RowFilter = $filter
    $DataGrid1.ItemsSource = $Datatable.DefaultView
})

# Shows the form
$statusBar1.Text = "Done."
$Form2.Add_Shown({$Form2.Activate()})
$Form2.ShowDialog() | out-null

}

让它打开我的第二个论坛拉数据,AddRemoveProgram 似乎无法使用它,所以这会导致它冻结几秒钟以提取数据,但一切正常。

标签: powershellsearch

解决方案


推荐阅读