【问题标题】:Powershell - Get Last 3 char from AD attribute in PsObjectPowershell - 从 PsObject 中的 AD 属性获取最后 3 个字符
【发布时间】:2016-08-02 19:09:47
【问题描述】:

我有一个脚本可以导出到 csv 一些 AD 属性 我想在 PsObject 中包含“initials”属性的最后 3 个字符,但我有一个错误,我花了几个小时在这上面...... 你能帮帮我吗?

错误:

*Method invocation failed because [Microsoft.ActiveDirectory.Management.ADUser] doesn't contain a method named 'substring'.
At C:\scripts\ExtractDWH\Untitled2.ps1:15 char:26
+                 "Test" = $_.substring <<<< ($_.initials.length - 3, 3)
    + CategoryInfo          : InvalidOperation: (substring:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound*

代码:

 $output = 'c:\scripts\ExtractDWH\consultants_test.csv'
 $users = Get-ADUser -Filter * -SearchBase "ou=Rennes,ou=Consultants,ou=Utilisateurs,ou=FedFinance,dc=dfedinterim,dc=fr" -     Properties *  | ? { ($_.initials -notlike 'IC*') -and ($_.initials -notlike 'IM*') -and ($_.initials -ne $null) }
      fileIn | % {
        $users | % { 
           New-Object psobject -Property @{
               "ID" = $_.initials
               "Last 3 strings ID" = $_.substring($_.initials.length - 3, 3) 
               "Centre Imputation" = $_.extensionAttribute10
               "Date Entrée" = $_.extensionAttribute9
                # The line below does not work                 
               "Test" = $_.substring($_.initials.length - 3, 3) 
             }
         }
     } | Select-Object ID, 'Centre Imputation', 'Date Entrée', 'Test' 
 | Export-CSV $output -Delimiter ';' -Encoding "UTF8" -NoTypeInformation    `

谢谢!

【问题讨论】:

  • 注意:-properties * 并不是一个好主意,因为它会获取可能存在的每一个广告属性。而是列出您想要的属性,如下所示:-properties Initials,extensionAttribute10,extensionAttribute9
  • 好的,我确实修改了 * 谢谢

标签: string csv powershell psobject


【解决方案1】:

改变这个:

"Last 3 strings ID" = $_.substring($_.initials.length - 3, 3) 

到这里:

"Last 3 strings ID" = $_.initials.substring($_.initials.length -3, 3)

【讨论】:

  • 它也很完美!非常感谢
【解决方案2】:

Avshalom 向您显示了错误。您还可以使用以下命令访问最后 3 个字符:

"Test" = $_.initials[-3 .. -1] -join ''

【讨论】:

  • 是不是应该更像"Test"[-3 .. -1] -join ""去掉数组?
  • 完美运行!谢谢
猜你喜欢
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 2017-07-20
  • 1970-01-01
相关资源
最近更新 更多