Skip to content

PS V7 how to obtain the access control information on a named pipe #23962

Closed

Description

@RG255

Prerequisites

Steps to reproduce

In PS 5.1 I can use this line of code:

$Pipe.GetAccessControl().access

to obtain this output:

PipeAccessRights : ReadWrite, Synchronize
AccessControlType : Allow
IdentityReference : <username>
IsInherited : False
InheritanceFlags : None
PropagationFlags : None

On V5 you use: [IO.Pipes.NamedPipeServerStream]::new
on V7 you use : [IO.Pipes.NamedPipeServerStreamacl]::Create

To create the access rules

$ps = [IO.Pipes.PipeSecurity]::new() $ar = @() $ar += [IO.Pipes.PipeAccessRule]::new($UserAccessName,'ReadWrite', 'Allow') foreach ($Item in $ar) {$ps.AddAccessRule($Item)} $ps
I know in V7 the rule works as I am denied access if the 'Allow' is changed to 'Deny' but I cannot work out how to obtain the access rights to check them on PS V7 Any help appreciated.

[psobject](Get-ChildItem -Path ('\.\pipe{0}' -f $Pipename)
does not appear to provide an answer that I can see.

Expected behavior

N/A

Actual behavior

N/A

Error details

N/A

Environment data

Name                           Value                                                                                                             
----                           -----                                                                                                             
PSVersion                      5.1.19041.4522                                                                                                    
PSEdition                      Desktop                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                           
BuildVersion                   10.0.19041.4522                                                                                                   
CLRVersion                     4.0.30319.42000                                                                                                   
WSManStackVersion              3.0                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                               
SerializationVersion           1.1.0.1 

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


Name                           Value
----                           -----
PSVersion                      7.5.0-preview.3
PSEdition                      Core
GitCommitId                    7.5.0-preview.3
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

N/A

Activity

  1. added
    Needs-Triage
    on Jun 18, 2024
  2. jborean93 commented on Jun 18, 2024

    @jborean93
    Collaborator

    As .NET is cross platform a lot of the Windows specific components like security descriptors were moved to extension methods through a separate assembly. In C# this mostly just works transparently as you can call them directly on the instance and the compiler will work in the background to call the extension method. In PowerShell this is not the case so you'll have to call the static extension method directly.

    For named pipes all the SD based methods are now in NamedPipeServerStreamAcl (to create a pipe with an explicit SD) and PipesAclExtensions (to get and set an SD on a pipe). So in PowerShell the code to get and set the SD on a PipeStream object would be

    # Whatever the code is to get the PipeStream object
    $pipe = ...
    
    # Can get the PipeSecurity object
    [System.IO.Pipes.PipesAclExtensions]::GetAccessControl($pipe)
    
    # Can set the PipeSecurity object
    [System.IO.Pipes.PipesAclExtensions]::SetAccessControl($pipe, $pipeSD)

    The same thing also applies to files where the GetAccessControl and SetAccessControl are no longer part of the base class implementation but part of an extension method, this time FileSystemAclExtensions. So if you have a file object that returned from Get-Item/Get-ChildItem you would do

    $file = Get-Item C:\Windows
    
    [System.IO.FileSystemAclExtensions]::GetAccessControl($file)
    [System.IO.FileSystemAclExtensions]::SetAccessControl($file, $fileSD)

    Unfortunately PowerShell cannot automatically paper over extension methods like the C# compiler has so you'll need to explicitly call them. This means using if statements to have a path when running 5.1 and 7.x if you need your script to run on both.

  3. RG255 commented on Jun 18, 2024

    @RG255
    Author

    Thank you for the information. I will make the changes tomorrow and then close off the case.

  4. added
    Issue-Question
    Resolution-Answered
    on Jun 19, 2024
  5. RG255 commented on Jun 19, 2024

    @RG255
    Author

    Yes that is exactly what I wanted - Issue closed

    Note: you also need to include:
    #requires -Modules Microsoft.PowerShell.Security

  6. microsoft-github-policy-service commented on Jun 19, 2024

    @microsoft-github-policy-service
    Contributor

    📣 Hey RG255, how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

  7. removed
    Needs-Triage
    on Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-Answered

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions