我无法使用 Powershell StartsWith 函数
I can't get Powershell StartsWith function to work
我正在尝试创建一个 Powershell 脚本,该脚本仅打印出文件夹权限设置中的某些 AD 组。但是由于某些原因,Powershell 无法识别 StartsWith 函数。
("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).StartsWith("sl_test")) { continue }; $_ }
当我 运行 这个时,我得到了与每个 foreach 对象类似的错误:
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
关于如何让它工作有什么建议吗?
根据您的错误消息,IdentityReference
是 [System.Security.Principal.NTAccount]
。
但是.StartWith
是String类型上的方法。如果你调用一个方法,Powershell 不会为你施展魔法,AFAIK。
尝试 ... ($_.IdentityReference) -match "^sl_test" ...
,它应该进行隐式字符串转换。
尝试:
Get-Acl -Path "C:\folder" | Select-Object -ExpandProperty Access | Where-Object {$_.IdentityReference -like "sl_test*" }
您可以使用额外的 | Select-Object -Property XY
自定义输出
如果你想要一个 IdentityReference
的字符串表示(不管它是和 NTAccount
对象还是 SID),你可以引用 Value
属性:
$_.IdentityReference.Value.StartsWith('sl_test')
我正在尝试创建一个 Powershell 脚本,该脚本仅打印出文件夹权限设置中的某些 AD 组。但是由于某些原因,Powershell 无法识别 StartsWith 函数。
("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).StartsWith("sl_test")) { continue }; $_ }
当我 运行 这个时,我得到了与每个 foreach 对象类似的错误:
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
关于如何让它工作有什么建议吗?
IdentityReference
是 [System.Security.Principal.NTAccount]
。
但是.StartWith
是String类型上的方法。如果你调用一个方法,Powershell 不会为你施展魔法,AFAIK。
尝试 ... ($_.IdentityReference) -match "^sl_test" ...
,它应该进行隐式字符串转换。
尝试:
Get-Acl -Path "C:\folder" | Select-Object -ExpandProperty Access | Where-Object {$_.IdentityReference -like "sl_test*" }
您可以使用额外的 | Select-Object -Property XY
如果你想要一个 IdentityReference
的字符串表示(不管它是和 NTAccount
对象还是 SID),你可以引用 Value
属性:
$_.IdentityReference.Value.StartsWith('sl_test')