Insert value from pipeline to declare .NET class

Welcome Forums General PowerShell Q&A Insert value from pipeline to declare .NET class

Viewing 4 reply threads
  • Author
    Posts
    • #194723
      Participant
      Topics: 11
      Replies: 11
      Points: 47
      Rank: Member

      Is there is way to use a value from the pipeline to declare a .NET Class? this is what I tried, but it's not working:

      [System.Security.AccessControl.FileSystemAccessRule].GetProperties().Name | ForEach-Object {
          Write-Host "$_ values are:"
          [System.Security.AccessControl.$_]
          [System.Security.AccessControl.$($_)]
      }

      Edit: Corrected .Net class being used.

    • #194762
      Moderator
      Topics: 3
      Replies: 169
      Points: 889
      Helping Hand
      Rank: Major Contributor

      Hello,

      What is the requirement, and context?

      Thank you.

    • #194807
      Participant
      Topics: 11
      Replies: 11
      Points: 47
      Rank: Member

      It's to let me know what values are in each property. It's so I don't need to look at the Microsoft doc and I have it ready at a moment's notice.

    • #194819
      Moderator
      Topics: 3
      Replies: 169
      Points: 889
      Helping Hand
      Rank: Major Contributor

      I am not sure though, but you can get the properties of a given .Net class using the `Get-Member` CmdLet...

      
      [System.Security.AccessControl.FileSystemAccessRule] | Get-Member -MemberType *property
      
      
      • #194822
        Moderator
        Topics: 3
        Replies: 169
        Points: 889
        Helping Hand
        Rank: Major Contributor

        Also if .Net class is a type enum, then you can get the values using GetEnumNames() method

        For example

        # To check the .Net class is type Enum or not
        [System.Windows.Forms.Appearance].IsEnum
        # True
        
        # To get the values
        [System.Windows.Forms.Appearance].GetEnumNames()
        # Normal
        # Button
    • #194843
      Participant
      Topics: 11
      Replies: 11
      Points: 47
      Rank: Member

      I was able to figure this out:

       

      [System.Security.AccessControl.FileSystemAccessRule].GetProperties() | ForEach-Object {
          switch ($_.Name) {
              {("FileSystemRights PropagationFlags AccessControlType" -split " ") -eq $_} {
              Write-Host "value is $_ ----------------------------------------"
                  Invoke-Expression -Command "[System.Security.AccessControl.$($_)].GetEnumNames()"
                  Write-Host ""
              }
      
              InheritanceFlags {
              Write-Host "value is $_ ----------------------------------------"
                  Invoke-Expression -Command "[System.Security.AccessControl.$($_)].GetEnumNames()"
                  Write-Host ""
              }
          } 
      }
Viewing 4 reply threads
  • You must be logged in to reply to this topic.