简体   繁体   English

Powershell - RegEx 通过 Get-Variable 按全名匹配表单元素

[英]Powershell - RegEx matching form elements by fullname via Get-Variable

How can you use Get-Variable pipe to operate on specific WinForm objects that match a RegEx to the Fullname property?如何使用 Get-Variable 管道对匹配 RegEx 到 Fullname 属性的特定 WinForm 对象进行操作? Attempts to use Where-Object {($_).GetType().FullName -match '^System\\.Windows\\.Forms'} fail.尝试使用Where-Object {($_).GetType().FullName -match '^System\\.Windows\\.Forms'}失败。

In the example below ($TestButton).GetType().FullName does return System.Windows.Forms.Button .在下面的示例中($TestButton).GetType().FullName确实返回System.Windows.Forms.Button But referencing with ($_).GetType().FullName returns System.Management.Automation.PSVariable .但是引用($_).GetType().FullName返回System.Management.Automation.PSVariable

Why does this not work, does a near or equivalent exist?为什么这不起作用,是否存在接近或等效的东西?

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Remove-Variable * -ErrorAction SilentlyContinue
$form                            = New-Object system.Windows.Forms.Form;
$form.ClientSize                 = New-Object System.Drawing.Point(100,100);
$form.text                       = "Form";
$TestButton                         = New-Object system.Windows.Forms.Button
$TestButton.text                    = "Test"
$TestButton.width                   = 85
$TestButton.height                  = 30
$TestButton.location                = New-Object System.Drawing.Point(0,0)
$TestButton.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',12)

($TestButton).GetType().FullName # This gives output
Get-Variable |
#Where-Object {($_).GetType().FullName -match '^System\.Windows\.Forms'} | # Line disabled for debugging only.
Foreach-Object{
                    $form.controls.Add($_) # Not working - obvious why due to output below - nothing matches...
                    ($_).GetType().FullName# due to System.Management.Automation.PSVariable
}
$form.controls.AddRange(@($TestButton)) # Works as expected. 
[void]$form.ShowDialog()

You could use你可以用

Get-Variable | where {$_.Value -is [system.windows.forms.button]}

or或者

Get-Variable | where {$_.Value.gettype().fullname -match 'system\.windows\.forms'}

It does work, just as intended.它确实有效,正如预期的那样。 You're just working with different object types.您只是在处理不同的对象类型。

$TestButton is an object of type System.Windows.Forms.Button $TestButtonSystem.Windows.Forms.Button类型的对象

When you run Get-Variable , it returns objects of type System.Management.Automation.PSVariable当您运行Get-Variable ,它返回System.Management.Automation.PSVariable类型的对象

if you run Get-Variable -Name TestButton You'll see that by default, it returns Name and a value field.如果您运行Get-Variable -Name TestButton您会看到默认情况下,它返回 Name 和一个值字段。 This is the name of the variable, and the value of said variable.这是变量的名称,以及所述变量的值。

What I assume you're looking to do, is extract the value of that variable, and get the type of the actual value.我假设您要做的是提取该变量的,并获取实际值的类型。 You would do this like this你会这样做

(Get-Variable -Name TestButton | % Value).GetType().Fullname

So for your Get-Variable pipe所以对于你的Get-Variable管道

Get-Variable |
  Where-Object {$_.value -is [System.Windows.Forms.Button]} | 
  Foreach-Object{
    $form.controls.Add($_.value) 
  }
                    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM