none
How to change headers in Export-CSV file?

    Question

  • I have one question about the original post here:

    http://social.technet.microsoft.com/Forums/da-DK/winserverpowershell/thread/e8498dd3-5744-4d90-bd87-72ad7259c3dd

    I am trying to read the headers from a .TXT file on a server which is being exported from Oracle as the following:

    SAMACCOUNTNAME, Pager

    I realize that "SAMACCOUNTNAME" must be changed to "sAMAccountName" in order for a different PowerShell script to run properly.

    I am having trouble with specifying the correct path in the first line.  I want to read the .TXT from its original location, change "SAMACCOUNTNAME" to "sAMAccountName" and then export the two headers "sAMAccountName,Pager" to a .CSV file on local C: drive.

    Here is what I have for my script:

    $path = "\\bow\appshare\EDR\edrtst\edroutput\ricoh\ricoh_extract.txt-$((get-date).tostring("C:\PowerShell Scripts\ricoh_extract")).csv"
    get-adgroupmember nameofgroup |
     get-aduser -properties sAMAccountName,Pager |
      foreach {
                new-object psobject -Property @{
                                                 sAMAccountName = $_.SAMACCOUNTNAME
                                                 Pager = $_.Pager
                                                 }
               } | Select sAMAccountName,Pager |
                   Export-Csv -delimiter "," $path

    Please help.  Thanks.

    Tuesday, October 9, 2012 3:18 PM

Answers

  • So, I gave up on PowerShell and wrote a VB script to find/replace the "SAMACCOUNTNAME", "sAMAccountName" and rewrite the TXT file in question.  It worked perfectly.

    Thanks, all.


    Rich Baird

    • Marked as answer by rb42000 Tuesday, October 9, 2012 4:30 PM
    Tuesday, October 9, 2012 4:29 PM

All replies

  • Why must the field SAMACCOUNTNAME be changed?  The script you posted should work.  If not, what's wrong?

    Grant Ward, a.k.a. Bigteddy


    • Edited by Bigteddy Tuesday, October 9, 2012 3:23 PM
    Tuesday, October 9, 2012 3:23 PM
  • Transcript (none of the users have pagers):

    PS C:\scripts> get-aduser -properties sAMAccountName,Pager |
       foreach {
                 new-object psobject -Property @{
                                                  sAMAccountName = $_.SAMACCOUNTNAME
                                                  Pager = $_.Pager
                                                  }
                } | Select sAMAccountName,Pager
    cmdlet Get-ADUser at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    Filter: *
    
    sAMAccountName                                                                       Pager                                                                               
    --------------                                                                       -----                                                                               
    Administrator                                                                                                                                                            
    Guest                                                                                                                                                                    
    Grant                                                                                                                                                                    
    krbtgt                                                                                                                                                                   
    samw                                                                                                                                                                     
    susang                                                                                                                                                                   
    Test1                                                                                                                                                                    
    Test2                                                                                                                                                                    
    A1                                                                                                                                                                       
    A2                                                                                                                                                                       
    A3                                                                                                                                                                       
    A4   


    Grant Ward, a.k.a. Bigteddy

    Tuesday, October 9, 2012 3:28 PM
  • When originally testing my other script, PowerShell spits out an error when trying to use SAMACCOUNTNAME versus sAMAccountName.  When I changed it, the script works fine.

    When running the above script, I get the following error:

    The term 'get-adgroupmember' is not recognized as the name of a cmdlet, function, script file, or operable
    ck the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\PowerShell Scripts\changecsvheaders.ps1:4 char:18
    + get-adgroupmember <<<<  nameofgroup |
        + CategoryInfo          : ObjectNotFound: (get-adgroupmember:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException


    Rich Baird

    Tuesday, October 9, 2012 3:34 PM
  • The code that I posted was just an excerpt from your script.  It was just meant to demonstrate that (at least on my system), Get-ADUser is not case-sensitive about its properties.  [ADSI] and [ADSISearcher] are.

    Grant Ward, a.k.a. Bigteddy

    Tuesday, October 9, 2012 3:38 PM
  • Perhaps the problem is because this is running on a Windows 2003 DC?  When I type in get-aduser it does not resolve when hitting the tab key.  Do you have any other alternative commands to pull the AD data?

    Rich Baird

    Tuesday, October 9, 2012 3:50 PM
  • Are you running these PowerShell commands from a Windows 2003 Server, because if that is the case you will not have access to the AD-Cmdlets. The Cmdlets are included in Server 2008 R2 and up.

    You can pull the data from AD by either using the [adsisearcher] accelerator or the Quest AD Cmdlets.


    Jaap Brasser
    http://www.jaapbrasser.com

    Tuesday, October 9, 2012 3:58 PM
  • Yes, the script is being run on a Windows 2003 Server DC.

    Rich Baird

    Tuesday, October 9, 2012 3:59 PM
  • I can then assume that you got the script from the Net.  Using ADSI to do this is a lot more complex.

    Grant Ward, a.k.a. Bigteddy

    Tuesday, October 9, 2012 4:24 PM
  • You may need to use the directory searching function and pull the results from that instead...

    $Search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
    $Search.filter = "(&(objectClass=user)(Pager="*"))"
    $objPager = ($Search.FindOne().GetDirectoryEntry()).path

    Im going to be PERFECTLY honest, this was not tested before posting as I have no user with pagers, but it "SHOULD" work...

    you get the idea though, this is for searching the information on pulling it down though, not formatting in to a table etc.

    Regards,
    Bertand1


    • Edited by Bertand1 Tuesday, October 9, 2012 4:26 PM
    Tuesday, October 9, 2012 4:25 PM
  • So, I gave up on PowerShell and wrote a VB script to find/replace the "SAMACCOUNTNAME", "sAMAccountName" and rewrite the TXT file in question.  It worked perfectly.

    Thanks, all.


    Rich Baird

    • Marked as answer by rb42000 Tuesday, October 9, 2012 4:30 PM
    Tuesday, October 9, 2012 4:29 PM