简体   繁体   中英

I can't get Powershell StartsWith function to work

I'm trying to create a Powershell script that prints out only certain AD groups from the Folder Permission settings. However for some reason Powershell doesn't recognize StartsWith function.

("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).StartsWith("sl_test")) { continue }; $_ }

When I run this I got errors similar to this for every foreach object:

Method invocation failed because [System.Security.Principal.NTAccount] does not contain a method named 'StartsWith'. At C:\\temp\\test.ps1:1 char:56 + ("C:\\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).St ... + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

Any suggestions on how to get this to work?

IdentityReference is a [System.Security.Principal.NTAccount] according to your error message.

But .StartWith is a method on the String type. If you call a method, Powershell does no magic for you, AFAIK.

Try ... ($_.IdentityReference) -match "^sl_test" ... , which should do the implicit string conversion.

如果要使用IdentityReference的字符串表示形式(无论它是NTAccount对象还是SID),都可以引用Value属性:

$_.IdentityReference.Value.StartsWith('sl_test')

Try:

Get-Acl -Path "C:\folder" | Select-Object -ExpandProperty Access | Where-Object {$_.IdentityReference -like "sl_test*" }

You can customize the output with an additional | Select-Object -Property XY | Select-Object -Property XY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM