2

我有一个脚本,可以将一些 AD 属性导出到 csv 我想在 PsObject 中包含“首字母”属性的最后 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    `

谢谢 !

4

2 回答 2

2

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

"Test" = $_.initials[-3 .. -1] -join ''
于 2016-04-12T09:36:01.030 回答
2

改变这个:

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

对此:

"Last 3 strings ID" = $_.initials.substring($_.initials.length -3, 3)
于 2016-04-12T09:33:04.527 回答