none
Export-Csv RRS feed

  • Question

  • $VPNUsers=Import-Csv "C:\Omar\Support_Tools\My_Scripts\My_Scripts\test.csv"
    $PrimaryDC = 'AD-PDC002.gotransit.local'
    $DomainName = 'gotransit'
    Clear-Host
    Connect-QADService -service $PrimaryDC

    Function RetrieveVPNUserBusiness
    {
    param(
    [string] $_UserName
    )
    Get-ADUser -Identity $_UserName -Properties Office | Export-Csv "c:\Omar\temp.csv"
    }

    FOREACH ($User in $VPNUsers) {
    RetrieveVPNUserBusiness $User.UserName
    }

    The export-csv is not appending records to temp.csv (file is blank).  Is there something I'm missing?

    Thanks,

    Omar

    Thursday, March 27, 2014 3:28 PM

Answers

  • Hi,

    Try it this way:

    Import-Csv 'C:\Omar\Support_Tools\My_Scripts\My_Scripts\test.csv' | ForEach {
    
        Get-ADUser -Identity $_.UserName -Properties Office
    
    } | Export-Csv 'C:\Omar\output.csv' -NoTypeInformation

    There's a lot of unneeded complexity in your original script.

    EDIT: In Tommy's example, -Append is being used (which requires PS V3), which will work but is slow. The code above builds the entire object, then exports it.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Edited by Mike Laughlin Thursday, March 27, 2014 4:20 PM
    • Proposed as answer by jrv Thursday, March 27, 2014 4:49 PM
    • Marked as answer by Bill_StewartModerator Monday, May 19, 2014 10:14 PM
    Thursday, March 27, 2014 4:19 PM

All replies

  • At one point you're appearing to use a Quest AD cmdlet and then inside your function you're using a cmdlet from the Windows ActiveDirectory module. Perhaps this is the problem. I have never used the Quest cmdlets but have plenty experience with the Windows cmdlets.

    The script below is working for me in PowerShell 3.0. Hope this is helpful.

    $VPNUsers = Import-Csv -Path 'c:\text.csv'
    
    If ($VPNUsers) {
        Function RetrieveVPNUserBusiness {
            param(
            [string] $_UserName
            )
    
            Get-ADUser -Identity $_UserName -Properties Office | Export-Csv "c:\temp.csv" -NoTypeInformation -Append
        }
    
        Foreach ($User in $VPNUsers) {
            RetrieveVPNUserBusiness $User.UserName
        }
    }

    Thursday, March 27, 2014 4:05 PM
  • Hi,

    Try it this way:

    Import-Csv 'C:\Omar\Support_Tools\My_Scripts\My_Scripts\test.csv' | ForEach {
    
        Get-ADUser -Identity $_.UserName -Properties Office
    
    } | Export-Csv 'C:\Omar\output.csv' -NoTypeInformation

    There's a lot of unneeded complexity in your original script.

    EDIT: In Tommy's example, -Append is being used (which requires PS V3), which will work but is slow. The code above builds the entire object, then exports it.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Edited by Mike Laughlin Thursday, March 27, 2014 4:20 PM
    • Proposed as answer by jrv Thursday, March 27, 2014 4:49 PM
    • Marked as answer by Bill_StewartModerator Monday, May 19, 2014 10:14 PM
    Thursday, March 27, 2014 4:19 PM
  • The whole script is a bit bogus:

    Connect-QADService -service $PrimaryDC

    Function RetrieveVPNUserBusiness
    {
    param(
    [string] $_UserName
    )
    Get-ADUser -Identity $_UserName -Properties Office | Export-Csv "c:\Omar\temp.csv"
    }

    Connect-QADService is not compatible with Get-AdUser.

    The "Param" statement is not useable. 

    This is just a bunch of scripts strung together which is not likely to ever work.


    ¯\_(ツ)_/¯

    Thursday, March 27, 2014 4:35 PM
  • Thank you, that greatly simplifies things
    Thursday, March 27, 2014 4:35 PM
  • I would also note that the function doesn't do anything more  than the command does.  It just adds more problems and noise to the script.

    Use Mike's solution.


    ¯\_(ツ)_/¯

    Thursday, March 27, 2014 4:51 PM