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"

3 Answers 3

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).

1
  • Typo: Look at the time I've used int Foreach-Object block - I missed 'v' in type name... Now fixed.
    – BartekB
    May 28, 2014 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)" }
0
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.

1
  • 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, 2014 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.