Luke Puplett
Luke Puplett

Reputation: 45105

How to use a .NET method that takes Type from PowerShell?

Simply, I want to use the FileSecurity.GetAccessRules API from PowerShell. The method signature looks like this in C#

public AuthorizationRuleCollection GetAccessRules(
    bool includeExplicit,
    bool includeInherited,
    Type targetType)

But I can't work out how to do a typeof from PS to specify the last argument.

Upvotes: 1

Views: 468

Answers (2)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

You don't need the [System.Type]::GetType() method, just enclose the type name in square brackets:

(Get-Acl D:\Path\To\Object).GetAccessRules($true,$true,[System.Security.Principal.NTAccount])

Upvotes: 4

Luke Puplett
Luke Puplett

Reputation: 45105

[System.Type]::GetType("System.Security.Principal.NTAccount")

E.g.

PS L:\> $accessEnum = [System.Security.AccessControl.AccessControlSections]::Access    
PS L:\> $fs = New-Object System.Security.AccessControl.FileSecurity -ArgumentList "L:\Logs", $accessEnum    
PS L:\> $rules = $fs.GetAccessRules($true, $false, [System.Type]::GetType("System.Security.Principal.NTAccount"))    

Upvotes: 1

Related Questions