【问题标题】:I can't get Powershell StartsWith function to work我无法让 Powershell StartsWith 功能工作
【发布时间】:2019-03-05 08:58:57
【问题描述】:

我正在尝试创建一个 Powershell 脚本,该脚本仅从文件夹权限设置中打印出某些 AD 组。但是由于某种原因,Powershell 无法识别 StartsWith 函数。

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

当我运行它时,每个 foreach 对象都会出现类似的错误:

方法调用失败,因为 [System.Security.Principal.NTAccount] 不包含名为“StartsWith”的方法。 在 C:\temp\test.ps1:1 char:56 + ("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).St ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

关于如何让它工作的任何建议?

【问题讨论】:

    标签: powershell startswith


    【解决方案1】:

    根据您的错误消息,IdentityReference[System.Security.Principal.NTAccount]

    但是.StartWith 是String 类型的一个方法。如果你调用一个方法,Powershell 对你没有任何作用,AFAIK。

    试试... ($_.IdentityReference) -match "^sl_test" ...,它应该进行隐式字符串转换。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        试试:

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

        您可以使用额外的| Select-Object -Property XY 自定义输出

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-04
          • 1970-01-01
          相关资源
          最近更新 更多