How to use PowerShell Get-Member cmdlet

后端 未结 4 454
[愿得一人]
[愿得一人] 2020-12-15 22:52

A newbie question:

The command:

[Math] | Get-Member

Returns all members of System.RuntimeType. Why is that?

Al

相关标签:
4条回答
  • 2020-12-15 23:08

    I just wrote a blog post on exploring static members of classes with PowerShell, which might help.

    What is happening when you pipe [Math] to Get-Member, you are passing in an object of System.RunTimeType, and it does return the members of that type.

    There is a switch parameter for Get-Member which allows you to examine all the static members of a class:

    [Math] | get-member -static
    

    If you need to find instance members, you will need to create an instance of the class and pipe that to Get-Member.

    0 讨论(0)
  • 2020-12-15 23:08
    1. Using the get-help get-member command, you get this output

    NAME Get-Member

    SYNOPSIS Gets the properties and methods of objects.

    SYNTAX Get-Member [[-Name] ] [-Force] [-InputObject ] [-MemberType {AliasProperty | CodeProperty | Pro perty | NoteProperty | ScriptProperty | Properties | PropertySet | Method | CodeMethod | ScriptMethod | Methods | P arameterizedProperty | MemberSet | Event | All}] [-Static] [-View {Extended | Adapted | Base | All}] []

    DESCRIPTION The Get-Member cmdlet gets the "members" (properties and methods) of objects.

    To specify the object, use the InputObject parameter or pipe an object to Get-Member. To retrieve information about
     static members (members of the class, not of the instance), use the Static parameter. To get only certain types of
     members, such as NoteProperties, use the MemberType parameter.
    

    RELATED LINKS Online version: http://go.microsoft.com/fwlink/?LinkID=113322 Add-Member Get-Help Get-Command Get-PSDrive

    REMARKS To see the examples, type: "get-help Get-Member -examples". For more information, type: "get-help Get-Member -detailed". For technical information, type: "get-help Get-Member -full".

    1. From the above statement, it shows that get-member only takes in objects and not classes. You use the -Static to view the static members of an object.

    2. When you use [String] without the -Static parameter, you are telling powershell that you are putting an object of type [String], however, you havent created a String object yet from any of your statement so it assumes that [String] is a System.Runtime object representing class String.This is similar if you use [Int] or [Boolean] and other class types.

    3. However, if you use -Static parameter, the powershell interpreter now understands that you want the Static members of String Objects.

    0 讨论(0)
  • 2020-12-15 23:15

    Also, does Get-member takes any positional parameters? How can I tell?

    If the parameter name is wrapped in '[]' then the name is optional, so the parameter is positional. For example for Get-Member (definition below), Name is positional but InputObject is not.

    Get-Member [[-Name] ] [-Force] [-InputObject ] [-MemberType {AliasProperty | CodeProperty | Pro perty | NoteProperty | ScriptProperty | Properties | PropertySet | Method | CodeMethod | ScriptMethod | Methods | P arameterizedProperty | MemberSet | Event | All}] [-Static] [-View {Extended | Adapted | Base | All}] []

    For the 1st 2 questions, it seems like you expect them to behave like objects but you are entering a namespace/class. If you do "1 | gm" or "gm -inputobject 1" you will see more like what you want/expect.

    0 讨论(0)
  • 2020-12-15 23:21

    You are getting a System.RuntimeType from [Math] because that is what it is. It's a Class type as opposed to an object of that particular type. You haven't actually constructed a [Math] object. You will get the same output if you typed:

    [String] | gm
    

    However, if you constructed a string object from the String type, you would get the string members:

    PS C:\> [String]("hi") | gm
    
    
       TypeName: System.String
    
    Name             MemberType            Definition
    ----             ----------            ----------
    Clone            Method                System.Object Clone()
    CompareTo        Method                System.Int32 CompareTo(Object value), System.Int32 CompareTo(String strB)
    Contains         Method                System.Boolean Contains(String value)
    CopyTo           Method                System.Void CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIn...
    etc...
    

    Since System.Math has only static members, you can't construct an object of it. To see it's members you can use the GetMembers() function of System.RuntimeType:

    [Math].GetMethods()
    

    You can use one of the format-* cmdlets to format the output:

    [Math].GetMethods() | format-table
    

    Edit: Oh, and I should add, to invoke one of the static members, you would do it like this:

    [Math]::Cos(1.5)
    
    0 讨论(0)
提交回复
热议问题