2

I got a script which export to csv some AD attributes I want to have the last 3 characters of the 'initials' attribute in a PsObject but i have an error and i spent Hours on this... Could you help me ?

The Error:

*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*

The Code :

 $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    `

Thank you !

Avshalom
  • 8,657
  • 1
  • 25
  • 43
olivier
  • 45
  • 2
  • 8
  • Note: -properties * is not really a good idea as it gets every single ad attribute that would exist. Instead list out the properties that you want like so: `-properties Initials,extensionAttribute10,extensionAttribute9` – Kiran Reddy Apr 12 '16 at 09:34
  • Ok i did modify the * thanks – olivier Apr 12 '16 at 12:43

2 Answers2

2

Change this:

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

To This:

"Last 3 strings ID" = $_.initials.substring($_.initials.length -3, 3)
Avshalom
  • 8,657
  • 1
  • 25
  • 43
2

Avshalom showed you the error. You can also use the following to access the last 3 characters:

"Test" = $_.initials[-3 .. -1] -join ''
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172