2

I want to loop over a static class's properties. [EnvDTE.Constants] | get-member -static | where-object {$_.MemberType -eq "Property" -and $_.Name -like 'vsP*'}

Instead of then going and manually typing the names like: [EnvDTE.Constants]::vsProjectItemKindMisc

tried:

  • | Select-Object {$_.Value}
  • | Select-Object {$([EnvDTE.Constants]::$_.Name)}
  • | Invoke-Expression "[EnvDTE.Constants]::$_.Name"
Maslow
  • 18,464
  • 20
  • 106
  • 193

3 Answers3

3

First of all, filter left (here it probably does not change much, but its good habit):

[EnvDTE.Constants] | Get-Member -Static -MemberType Property -Name vsP*

One you have MemberDefinition objects:

| Foreach-Object { [EnvDTE.Constants]::"$($_.Name)" }

Your last attempt would work, if you would use subexpression there (though I recommend against it, Invoke-Expression should be used only if really necessary).

BartekB
  • 8,492
  • 32
  • 33
  • Typo: Look at the time I've used int `Foreach-Object` block - I missed 'v' in type name... Now fixed. – BartekB May 28 '14 at 12:40
1

You can go the .NET BCL route:

[EnvDTE.Constants].UnderlyingSystemType.GetFields('Static,Public').Where({$_.Name -match 'vsP*'}).Foreach({$_.Name + " = " + $_.GetValue($null)})

Or perhaps a bit more PowerShelly:

[EnvDTE.Constants].UnderlyingSystemType.GetFields('Static,Public') | 
    Where Name -match vsP* | Foreach {$_.Name + " = " + $_.GetValue($null)}

Or:

[EnvDTE.Constants] | gm -static -MemberType Property -Name vsP* | 
    Foreach { invoke-expression "'$($_.Name) = ' + [$($_.TypeName)]::$($_.Name)" }
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
0

The first example you gave is correct, but doesn't appear to work for that namespace. Both of these work:

[system.math] | Get-Member
[system.net.webrequest] | Get-Member

If you have the dll file you can load it manually.

Community
  • 1
  • 1
Tim Ferrill
  • 1,648
  • 1
  • 12
  • 15
  • the namespace is working the same as `[System.math] | get-member -static | where-object {$_.MemberType -eq "Property"} ` what doesn't seem to work is being able to access the value of what you get back from executing them this way without going back and typing the names out – Maslow May 28 '14 at 01:07