none
Get-ADUser : Object reference not set to an instance of an object RRS feed

  • Question

  • Hi guys!

    I created a script to extract a users list from Active Directory, but it works only if I specify that I want users created today.



    When I have removed the created date, my script doesn't worked. 

    Get-ADUser -Filter {(enabled -eq $true) -and (extensionattribute3 -eq "colaborador") -or (extensionattribute3 -eq "terceiro")} -Properties * | Select @{ expression={$_.Surname}; label='TSLASTNAME' },@{ expression={$_.GivenName}; label='TSFIRSTNAME'},@{
     expression={$_.DisplayName}; label='TSDISPLAYNAME'},@{ expression={$_.PostalCode}; label='TSPOSTALCODE'},@{ expression={$_.Country}; label='TSCOUNTRY'},@{ expression={$_.StreetAddress}; label='TSADDRESSLINE1'},@{ expression={$_.State}; label='TSSTATEPROVINCE'},@{
     expression={$_.Office}; label='TSLOCATIONSITE'},@{ expression={$_.OfficePhone}; label='TSPRIMARYPHONE'},@{ expression={$_.UserPrincipalName}; label='TSPRIMARYEMAIL'},@{ expression={$_.Title}; label='TSTITLE'},@{ expression={$_.Department}; label='TSDEPARTMENT'},@{
     expression={$_.City}; label='TSCITY'},@{ expression={$_.UserPrincipalName}; label='TSLOGINID'},@{ expression={$_.UserPrincipalName}; label='TSUSERID'},@{ expression={$_.msExchUserCulture}; label='TSLOCALE'},@{ expression={$_.extensionAttribute14}; label='TSTYPE'},@{
     expression={$_.extensionAttribute14}; label='TSGROUPUSER' },@{expression={$_.extensionAttribute14}; label='TSPLUSPCUSTVENDOR'},@{expression={$_.extensionAttribute14}; label='TSLANGUAGE'},@{expression={$_.extensionAttribute14}; label='TSTIMEZONE'},@{expression={$_.UserPrincipalName};
     label='TSPERSONID'},@{expression={$_.extensionAttribute14}; label='TSJOBCODE'} | Export-Csv "\\server\integracao.adibm\teste.csv" -NoTypeInformation -Encoding unicode -Force
    '99999999 LASTRECORD'| Out-File  "\\server\integracao.adibm\teste.csv" -Append
    (Get-Content \\server\integracao.adibm\teste.csv) | % {$_ -replace '"', ""} | out-file -FilePath "\\server\integracao.adibm\teste.csv" -Force -Encoding unicode


    Error message:

    Get-ADUser : Object reference not set to an instance of an object.
    At C:\Scripts\Matricula\script_IBM_V2.ps1:2 char:1
    + Get-ADUser -Filter {(enabled -eq $true) -and (extensionattribute3 -eq "colaborad ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADUser], NullReferenceException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.NullReferenceException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Do you know how to fix this problem?

    Thanks.


    David Soares MCTS:MBS - MCTS - MCITP


    Thursday, January 7, 2016 7:29 PM

Answers

  • Here is how you can output custom objects where certain properties are always the same:


    #requires -version 3
    
    $attributes = [Ordered] @{
      "TSADDRESSLINE1"   = "StreetAddress"
      "TSCITY"           = "City"
      "TSCOUNTRY"        = "Country"
      "TSDEPARTMENT"     = "Department"
      "TSDISPLAYNAME"    = "DisplayName"
      "TSFIRSTNAME"      = "GivenName"
      "TSGROUPUSER"      = ""
      "TSJOBCODE"        = ""
      "TSLANGUAGE"       = ""
      "TSLASTNAME"       = "Surname"
      "TSLOCALE"         = ""
      "TSLOCATIONSITE"   = "Office"
      "TSLOCATIONSTATE"  = ""
      "TSLOGINID"        = "UserPrincipalName"
      "TSPERSONID"       = "UserPrincipalName"
      "TSPLUSCUSTVENDOR" = ""
      "TSPOSTALCODE"     = "PostalCode"
      "TSPRIMARYEMAIL"   = "UserPrincipalName"
      "TSPRIMARYPHONE"   = "OfficePhone"
      "TSSTATEPROVINCE"  = "State"
      "TSTIMEZONE"       = ""
      "TSTITLE"          = "Title"
      "TSTYPE"           = ""
      "TSUSERID"         = "UserPrincipalName"
    }
    
    $params = @{
      "LDAPFilter" = "(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(extensionAttribute3=colaborador)(extensionAttribute3=terceiro)))"
      "Properties" = $attributes.Values | Where-Object { $_ } | Sort-Object -Unique
    }
    Get-ADUser @params | ForEach-Object {
      $user = $_
      $output = New-Object PSObject
      $attributes.Keys | ForEach-Object {
        switch ( $_ ) {
          "TSGROUPUSER" {
            $output | Add-Member $_ "IBMENDUSER"
          }
          "TSJOBCODE" {
            $output | Add-Member $_ ""
          }
          "TSLANGUAGE" {
            $output | Add-Member $_ "BR"
          }
          "TSLOCALE" {
            $output | Add-Member $_ "pt_BR"
          }
          "TSLOCATIONSTATE" {
            $output | Add-Member $_ "FLEURY"
          }
          "TSPLUSCISTVENDOR" {
            $output | Add-Member $_ "FLY-00"
          }
          "TSTIMEZONE" {
            $output | Add-Member $_ "BRAZIL/EAST"
          }
          "TSTYPE" {
            $output | Add-Member $_ "TYPE 1"
          }
          default {
            $output | Add-Member $_ $user.$($attributes[$_])
          }
        }
      }
      $output
    }
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 12, 2016 4:14 PM
    Moderator

All replies

  • Your code is very hard to read. Please use the "Insert code block" feature in the editor to post the code properly. As it stands, your code is posted like one long run-on sentence.

    -- Bill Stewart [Bill_Stewart]

    Thursday, January 7, 2016 8:20 PM
    Moderator
  • Sorry, Bill.

    I forgot to edit the post.


    David Soares MCTS:MBS - MCTS - MCITP

    Thursday, January 7, 2016 8:28 PM
  • You seem to be doing things in a convoluted way.

    If you want to create CSV output, you should use Export-Csv rather than Out-File.

    Why Out-File, Get-Content, -replace? What are you actually trying to do?


    -- Bill Stewart [Bill_Stewart]

    Thursday, January 7, 2016 8:42 PM
    Moderator
  • If users would learn how t format code so that it is readable they might get more help.

    $filter = 'Enabled -eq $true -and (extensionattribute3 -eq "colaborador" -or extensionattribute3 -eq "terceiro"'
    $props=@(
    	@{ e = { $_.Surname }; n = 'TSLASTNAME' }, 
    	@{ e = { $_.GivenName }; n = 'TSFIRSTNAME' }, 
    	@{e = { $_.DisplayName }; n = 'TSDISPLAYNAME'},
    	@{ e = { $_.PostalCode }; n = 'TSPOSTALCODE' },
    	@{ e = { $_.Country }; n = 'TSCOUNTRY' },
    	@{ e = { $_.StreetAddress }; n = 'TSADDRESSLINE1' },
    	@{ e = { $_.State }; n = 'TSSTATEPROVINCE' },
    	@{e = { $_.Office }; n = 'TSLOCATIONSITE'},
    	@{ e = { $_.OfficePhone }; n = 'TSPRIMARYPHONE' },
    	@{ e = { $_.UserPrincipalName }; n = 'TSPRIMARYEMAIL' }, 
    	@{ e = { $_.Title }; n = 'TSTITLE' }, 
    	@{ e = { $_.Department }; n = 'TSDEPARTMENT' }, 
    	@{e = { $_.City }; n = 'TSCITY'},
    	@{ e = { $_.UserPrincipalName }; n = 'TSLOGINID' },
    	@{ e = { $_.UserPrincipalName }; n = 'TSUSERID' }, 
    	@{ e = { $_.msExchUserCulture }; n = 'TSLOCALE' },
    	@{ e = { $_.extensionAttribute14 }; n = 'TSTYPE' }, 
    	@{e = { $_.extensionAttribute14 }; n = 'TSGROUPUSER'},
    	@{ e = { $_.extensionAttribute14 }; n = 'TSPLUSPCUSTVENDOR' },
    	@{ e = { $_.extensionAttribute14 }; n = 'TSLANGUAGE' }, 
    	@{ e = { $_.extensionAttribute14 }; n = 'TSTIMEZONE' }, 
    	@{e = { $_.UserPrincipalName };n = 'TSPERSONID'},
    	@{ e = { $_.extensionAttribute14 }; n = 'TSJOBCODE' }
    )
    
    Get-ADUser -Filter $filter -Properties * |
    	Select $props |
    	Export-Csv \\server\integracao.adibm\teste.csv -NoTypeInformation -Encoding unicode -Force
    
    '99999999 LASTRECORD' | Out-File  \\server\integracao.adibm\teste.csv -Append
    (Get-Content \\server\integracao.adibm\teste.csv) | 
    	% { $_ -replace '"', "" } | 
    	out-file -FilePath \\server\integracao.adibm\teste.csv -Force -Encoding unicode

    Now you can see your issues.

    If you just want to change the header then just replace the first line in the file.  No need to rename every property.


    \_(ツ)_/


    • Edited by jrv Friday, January 8, 2016 2:14 PM
    Thursday, January 7, 2016 9:34 PM
  • Thanks Bill and jrv.

    Basically, I'm trying to export a list of Active Directory.

    First, I made a Get-ADUser. Second, I need to rename all properties. Then, I need to export the results to a csv file and input "'99999999 LASTRECORD'" at last line.


    When I run this script only 448 objects are returned, but we have about 12000 enabled users.

    Also that, PowerShell stoped and returned the following error:

    Get-ADUser : Object reference not set to an instance of an object.
    At C:\Scripts\Matricula\script_IBM_V2.ps1:28 char:1
    + Get-ADUser -Filter $filter -Properties * |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADUser], NullReferenceException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.NullReferenceException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Do you know how to ignore errors and to conclude the task?

    Thanks again.


    David Soares MCTS:MBS - MCTS - MCITP



    Friday, January 8, 2016 12:11 PM
  • Thanks Bill and jrv.

    Basically, I'm trying to export a list of Active Directory.

    First, I made a Get-ADUser. Second, I need to rename all properties. Then, I need to export the results to a csv file and input "'99999999 LASTRECORD'" at last line.


    When I run this script only 448 objects are returned, but we have about 12000 enabled users.

    Also that, PowerShell stoped and returned the following error:

    Get-ADUser : Object reference not set to an instance of an object.
    At C:\Scripts\Matricula\script_IBM_V2.ps1:28 char:1
    + Get-ADUser -Filter $filter -Properties * |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADUser], NullReferenceException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.NullReferenceException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Do you know how to ignore errors and to conclude the task?

    Thanks again.


    David Soares MCTS:MBS - MCTS - MCITP



    You have to fix that error.

    Just test your filter like this:

    $filter = 'Enabled -eq $true -and (extensionattribute3 -eq "colaborador" -or extensionattribute3 -eq "terceiro" )'
    
    Get-Aduser -filter $filter


    \_(ツ)_/




    • Edited by jrv Friday, January 8, 2016 2:11 PM
    Friday, January 8, 2016 2:04 PM
  • Yes - the logic of your filter was faulty.  You used to many parens and in the wrong places.

    \_(ツ)_/

    Friday, January 8, 2016 2:15 PM
  • Yes, the filter was faulty.

    I had tested the modified filter and it worked fine, but when I run this full script the same error is shown.


    David Soares MCTS:MBS - MCTS - MCITP

    Friday, January 8, 2016 2:59 PM
  • Does


    Get-ADUser -Filter *
    

    successfully enumerate all users in the domain?


    -- Bill Stewart [Bill_Stewart]

    Friday, January 8, 2016 3:31 PM
    Moderator
  • Yes, the filter was faulty.

    I had tested the modified filter and it worked fine, but when I run this full script the same error is shown.


    David Soares MCTS:MBS - MCTS - MCITP

    What script?  What error? We cannot see your screen.

    Did you run the sample I just posted?


    \_(ツ)_/


    • Edited by jrv Friday, January 8, 2016 3:38 PM
    Friday, January 8, 2016 3:37 PM
  • Bill, when I run the script using "*" the same error message is shown:

    "Get-ADUser : Object reference not set to an instance of an object."

    jrv, I run your sample, but same error was shown.

    See the attached images.



    David Soares MCTS:MBS - MCTS - MCITP

    Friday, January 8, 2016 5:00 PM
  • Not a scripting issue.  Point your query at a different DC.  You may have an issue with AD.

    I do not see where you have run this:

    Get-ADUser -Filter *

    Just that line.  Nothing else.  Start by opening a  new copy of PS and just paste that line in.


    \_(ツ)_/


    • Edited by jrv Friday, January 8, 2016 5:03 PM
    Friday, January 8, 2016 5:01 PM
  • When I run "Get-ADUser -Filter *" works fine.

    In a different DC the result is the same.


    David Soares MCTS:MBS - MCTS - MCITP

    Friday, January 8, 2016 5:23 PM
  • OK, so now try this exact command:


    Get-ADUser -LDAPFilter "(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(extensionAttribute3=colaborador)(extensionAttribute3=terceiro)))"
    

    (i.e., users that are not disabled and have extensionAttribute3 set to either 'colaborador' or 'terceiro')

    Does this command run successfully without errors?


    -- Bill Stewart [Bill_Stewart]

    Friday, January 8, 2016 5:36 PM
    Moderator
  • This command run successfully without errors.


    David Soares MCTS:MBS - MCTS - MCITP

    Friday, January 8, 2016 6:10 PM
  • When I run "Get-ADUser -Filter *" works fine.

    In a different DC the result is the same.



    David Soares MCTS:MBS - MCTS - MCITP

    We don't really need a lot of pictures.

    Now run just this:

    $filter = 'Enabled -eq $true -and (extensionattribute3 -eq "colaborador" -or extensionattribute3 -eq "terceiro" )'
    Get-Aduser -filter $filter
    This runs correctly on my AD.


    \_(ツ)_/


    • Edited by jrv Friday, January 8, 2016 6:21 PM
    Friday, January 8, 2016 6:21 PM
  • I sent the pictures only to facilitate understanding.

    This script worked on my AD.

    David Soares MCTS:MBS - MCTS - MCITP

    Friday, January 8, 2016 7:17 PM
  • Now comment out most of the properties in the $props array and run it until you narrow down the property that is wrong or misspelled.


    \_(ツ)_/

    Friday, January 8, 2016 7:26 PM
  • Here is a hint as to how to do this:

    $props=@(
    <#	@{ e = { $_.Surname }; n = 'TSLASTNAME' }, 
    	@{ e = { $_.GivenName }; n = 'TSFIRSTNAME' }, 
    	@{e = { $_.DisplayName }; n = 'TSDISPLAYNAME'},
    	@{ e = { $_.PostalCode }; n = 'TSPOSTALCODE' },
    	@{ e = { $_.Country }; n = 'TSCOUNTRY' },
    	@{ e = { $_.StreetAddress }; n = 'TSADDRESSLINE1' },
    	@{ e = { $_.State }; n = 'TSSTATEPROVINCE' },
    	@{e = { $_.Office }; n = 'TSLOCATIONSITE'},
    	@{ e = { $_.OfficePhone }; n = 'TSPRIMARYPHONE' },
    	@{ e = { $_.UserPrincipalName }; n = 'TSPRIMARYEMAIL' }, 
    	@{ e = { $_.Title }; n = 'TSTITLE' }, 
    	@{ e = { $_.Department }; n = 'TSDEPARTMENT' }, 
    	@{e = { $_.City }; n = 'TSCITY'},
    	@{ e = { $_.UserPrincipalName }; n = 'TSLOGINID' },
    	@{ e = { $_.UserPrincipalName }; n = 'TSUSERID' }, 
    	@{ e = { $_.msExchUserCulture }; n = 'TSLOCALE' },
    	@{ e = { $_.extensionAttribute14 }; n = 'TSTYPE' }, 
    	@{e = { $_.extensionAttribute14 }; n = 'TSGROUPUSER'},
    	@{ e = { $_.extensionAttribute14 }; n = 'TSPLUSPCUSTVENDOR' },
    	@{ e = { $_.extensionAttribute14 }; n = 'TSLANGUAGE' }, 
    	@{ e = { $_.extensionAttribute14 }; n = 'TSTIMEZONE' }, 
    	@{e = { $_.UserPrincipalName };n = 'TSPERSONID'},
    #>
    	@{ e = { $_.extensionAttribute14 }; n = 'TSJOBCODE' }
    )
    
    

    Just move the bottom #> up a few rows at a time and see where it fails.

    That is why I created this.  It makes building and debugging it easier.


    \_(ツ)_/

    Friday, January 8, 2016 7:30 PM
  • IMO, here is a slightly cleaner and more maintainable way to handle the attributes:


    $attributes = @{
      "TSCITY"            = "City"
      "TSCOUNTRY"         = "Country"
      "TSDEPARTMENT"      = "Department"
      "TSDISPLAYNAME"     = "DisplayName"
      "TSGROUPUSER"       = "extensionAttribute14"
      "TSJOBCODE"         = "extensionAttribute14"
      "TSLANGUAGE"        = "extensionAttribute14"
      "TSPLUSPCUSTVENDOR" = "extensionAttribute14"
      "TSTIMEZONE"        = "extensionAttribute14"
      "TSTYPE"            = "extensionAttribute14"
      "TSFIRSTNAME"       = "GivenName"
      "TSLOCALE"          = "msExchUserCulture"
      "TSLOCATIONSITE"    = "Office"
      "TSPOSTALCODE"      = "PostalCode"
      "TSSTATEPROVINCE"   = "State"
      "TSADDRESSLINE1"    = "StreetAddress"
      "TSLASTNAME"        = "Surname"
      "TSTITLE"           = "Title"
      "TSPRIMARYPHONE"    = "OfficePhone"
      "TSLOGINID"         = "UserPrincipalName"
      "TSPERSONID"        = "UserPrincipalName"
      "TSPRIMARYEMAIL"    = "UserPrincipalName"
      "TSUSERID"          = "UserPrincipalName"
    }
    
    $selectList = $attributes.Keys | ForEach-Object {
      @{Name = "$_"; Expression = [ScriptBlock]::Create("`$_.$($attributes[$_])")}
    }
    
    $params = @{
      "LDAPFilter" = "(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(extensionAttribute3=colaborador)(extensionAttribute3=terceiro)))"
      "Properties" = $attributes.Values | Sort-Object -Unique
    }
    Get-ADUser @params | Select-Object $selectList
    


    -- Bill Stewart [Bill_Stewart]


    Friday, January 8, 2016 7:43 PM
    Moderator
  • Bill, do you know how can I export this attributes ($attributes), include others properties in same csv file and remove double quotes?

    I'm trying this way:

    $attributes = @{
      "TSCITY"            = "City"
      "TSCOUNTRY"         = "Country"
      "TSDEPARTMENT"      = "Department"
      "TSDISPLAYNAME"     = "DisplayName"
      "TSGROUPUSER"       = "extensionAttribute14"
      "TSJOBCODE"         = "extensionAttribute14"
      "TSLANGUAGE"        = "extensionAttribute14"
      "TSPLUSPCUSTVENDOR" = "extensionAttribute14"
      "TSTIMEZONE"        = "extensionAttribute14"
      "TSTYPE"            = "extensionAttribute14"
      "TSFIRSTNAME"       = "GivenName"
      "TSLOCALE"          = "msExchUserCulture"
      "TSLOCATIONSITE"    = "Office"
      "TSPOSTALCODE"      = "PostalCode"
      "TSSTATEPROVINCE"   = "State"
      "TSADDRESSLINE1"    = "StreetAddress"
      "TSLASTNAME"        = "Surname"
      "TSTITLE"           = "Title"
      "TSPRIMARYPHONE"    = "OfficePhone"
      "TSLOGINID"         = "UserPrincipalName"
      "TSPERSONID"        = "UserPrincipalName"
      "TSPRIMARYEMAIL"    = "UserPrincipalName"
      "TSUSERID"          = "UserPrincipalName"
    }
    
    $selectList = $attributes.Keys | ForEach-Object {
      @{Name = "$_"; Expression = [ScriptBlock]::Create("`$_.$($attributes[$_])")}
    }
    
    $params = @{
      "LDAPFilter" = "(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(extensionAttribute3=colaborador)(extensionAttribute3=terceiro)))"
      "Properties" = $attributes.Values | Sort-Object -Unique
    }
    Get-ADUser @params | Select-Object $selectList | Export-Csv "C:\Scripts\Matricula\teste.csv" -NoTypeInformation -Encoding unicode -Force
    '99999999 LASTRECORD'| Out-File  "C:\Scripts\Matricula\teste.csv" -Append
    (Get-Content C:\Scripts\Matricula\teste.csv) | % {$_ -replace '"', ""} | Select @{n = { $_.TSJOBCODE}; e = ''}, @{n = { $_.TSLOCATIONSTATE}; e = 'FLEURY'}, @{n = { $_.TSTIMEZONE}; e = 'Brazil/East'}, @{n = { $_.TSLOCALE}; e = 'pt_BR'}, @{n = { $_.TSLANGUAGE}; e = 'BR'}, @{n = { $_.TSPLUSCUSTVENDOR}; e = 'FLY-00'}, @{n = { $_.TSTYPE}; e = 'TYPE 1'}, @{n = { $_.TSGROUPUSER}; e = 'IBMENDUSER'} | out-file -FilePath "C:\Scripts\Matricula\teste.csv" -Force -Encoding unicode


    David Soares MCTS:MBS - MCTS - MCITP

    • Proposed as answer by BLG Boy Tuesday, January 12, 2016 2:34 PM
    • Unproposed as answer by Bill_StewartModerator Tuesday, January 12, 2016 3:16 PM
    Tuesday, January 12, 2016 12:42 PM
  • Why remove double quotes? If any of the attributes contains a comma (a comma is a legal character in Active Directory), your output file will be corrupt. Also, should the output file contain a header lines?

    -- Bill Stewart [Bill_Stewart]



    Tuesday, January 12, 2016 3:16 PM
    Moderator
  • Here is how you can output custom objects where certain properties are always the same:


    #requires -version 3
    
    $attributes = [Ordered] @{
      "TSADDRESSLINE1"   = "StreetAddress"
      "TSCITY"           = "City"
      "TSCOUNTRY"        = "Country"
      "TSDEPARTMENT"     = "Department"
      "TSDISPLAYNAME"    = "DisplayName"
      "TSFIRSTNAME"      = "GivenName"
      "TSGROUPUSER"      = ""
      "TSJOBCODE"        = ""
      "TSLANGUAGE"       = ""
      "TSLASTNAME"       = "Surname"
      "TSLOCALE"         = ""
      "TSLOCATIONSITE"   = "Office"
      "TSLOCATIONSTATE"  = ""
      "TSLOGINID"        = "UserPrincipalName"
      "TSPERSONID"       = "UserPrincipalName"
      "TSPLUSCUSTVENDOR" = ""
      "TSPOSTALCODE"     = "PostalCode"
      "TSPRIMARYEMAIL"   = "UserPrincipalName"
      "TSPRIMARYPHONE"   = "OfficePhone"
      "TSSTATEPROVINCE"  = "State"
      "TSTIMEZONE"       = ""
      "TSTITLE"          = "Title"
      "TSTYPE"           = ""
      "TSUSERID"         = "UserPrincipalName"
    }
    
    $params = @{
      "LDAPFilter" = "(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(extensionAttribute3=colaborador)(extensionAttribute3=terceiro)))"
      "Properties" = $attributes.Values | Where-Object { $_ } | Sort-Object -Unique
    }
    Get-ADUser @params | ForEach-Object {
      $user = $_
      $output = New-Object PSObject
      $attributes.Keys | ForEach-Object {
        switch ( $_ ) {
          "TSGROUPUSER" {
            $output | Add-Member $_ "IBMENDUSER"
          }
          "TSJOBCODE" {
            $output | Add-Member $_ ""
          }
          "TSLANGUAGE" {
            $output | Add-Member $_ "BR"
          }
          "TSLOCALE" {
            $output | Add-Member $_ "pt_BR"
          }
          "TSLOCATIONSTATE" {
            $output | Add-Member $_ "FLEURY"
          }
          "TSPLUSCISTVENDOR" {
            $output | Add-Member $_ "FLY-00"
          }
          "TSTIMEZONE" {
            $output | Add-Member $_ "BRAZIL/EAST"
          }
          "TSTYPE" {
            $output | Add-Member $_ "TYPE 1"
          }
          default {
            $output | Add-Member $_ $user.$($attributes[$_])
          }
        }
      }
      $output
    }
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 12, 2016 4:14 PM
    Moderator
  • I will use the output file in service desk system.
    The system don't read the csv file if it have quotation marks and DistinguishedName separated by commas.

    With your help, I made this script.

    $params = @{
      "LDAPFilter" = "(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(|(extensionAttribute3=colaborador)(extensionAttribute3=terceiro)))" }
    
    Get-ADUser @params | Select @{ expression={$_.DistinguishedName}; label='DistinguishedName' },@{ expression={$_.Surname}; label='TSLASTNAME' },@{ expression={$_.GivenName}; label='TSFIRSTNAME'},@{ expression={$_.DisplayName}; label='TSDISPLAYNAME'},@{ expression={$_.PostalCode}; label='TSPOSTALCODE'},@{ expression={$_.Country}; label='TSCOUNTRY'},@{ expression={$_.StreetAddress}; label='TSADDRESSLINE1'},@{ expression={$_.State}; label='TSSTATEPROVINCE'},@{ expression={$_.OfficePhone}; label='TSPRIMARYPHONE'},@{ expression={$_.UserPrincipalName}; label='TSPRIMARYEMAIL'},@{ expression={$_.Title}; label='TSTITLE'},@{ expression={$_.Department}; label='TSDEPARTMENT'},@{ expression={$_.City}; label='TSCITY'},@{ expression={$_.UserPrincipalName}; label='TSLOGINID'},@{ expression={$_.UserPrincipalName}; label='TSUSERID'},@{expression={$_.UserPrincipalName}; label='TSPERSONID'},@{expression={$_.POBox}; label='TSCLIENTUSERID'} | ForEach-Object {
    
    $data1 = ''
    $data2 = 'FLEURY'
    $data3 = 'Brazil/East'
    $data4 = 'pt_BR'
    $data5 = 'BR'
    $data6 = 'FLY-00'
    $data7 = 'TSTYPE 1'
    $data8 = 'IBMENDUSER'
    
    
    $_ | 
    Add-Member -MemberType NoteProperty -Name 'TSJOBCODE' -Value $data1 -PassThru |
    Add-Member -MemberType NoteProperty -Name 'TSLOCATIONSITE' -Value $data2 -PassThru |
    Add-Member -MemberType NoteProperty -Name 'TSTIMEZONE' -Value $data3 -PassThru |
    Add-Member -MemberType NoteProperty -Name 'TSLOCALE' -Value $data4 -PassThru |
    Add-Member -MemberType NoteProperty -Name 'TSLANGUAGE' -Value $data5 -PassThru |
    Add-Member -MemberType NoteProperty -Name 'TSPLUSPCUSTVENDOR' -Value $data6 -PassThru |
    Add-Member -MemberType NoteProperty -Name 'TSTYPE' -Value $data7 -PassThru |
    Add-Member -MemberType NoteProperty -Name 'TSGROUP' -Value $data8 -PassThru } | Export-Csv "C:\Scripts\Matricula\teste.csv" -NoTypeInformation -Encoding unicode -Force
    '99999999 LASTRECORD'| Out-File  "C:\Scripts\Matricula\teste.csv" -Append

    Now I'll find a way to remove the quotation marks and commas of DistinguishedName.

    Thank you very much.


    David Soares MCTS:MBS - MCTS - MCITP

    Tuesday, January 12, 2016 5:30 PM