none
Powershell Function Export CSV RRS feed

  • Question

  • Started writing the powershell program and encountering an issue while extracting output in CSV using function. when we tried to extract out-file, the output is prefect , when we use export-csv the output is showing length.  

    Powershell Program:

    $out = "F:\active.csv"
    function ad () {
    $users = Get-Content "C:\dis1.txt" 
    foreach( $user in $users)
    {
    try{
    $memberof=Get-ADUser $user -Properties * -ErrorAction stop | select samaccountname,memberof
    foreach ($groups in $memberof)
    {
    if ($group=$groups.memberof -like "*mobile*")
    {
    foreach ($name in $group)
    {
    $gpnames += "$((get-adgroup $name).samaccountname);"
    $gpname= "$($memberof.samaccountname),Exist,$($gpnames)"
    }
    Write-output $gpname
    }
    else {
    Write-output "$($memberof.samaccountname),Exist"
    }}}
    catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
    {
    $usrname = "$($user),NotExist"
    Write-output $usrname
    }}}
    ad 


    Output in out-file

    nbdhandasi123,Exist,MobileUsers;MobileExchUsers;
    nbdhandaaaaaaaaa,NotExist
    ThokalS,Exist

    Output in Export-CSV

    #TYPE System.String
    "Length"
    "6"
    "25"
    "6"


    - Sivashankar. Please mark as answer/useful if my contribution is helpful





    Monday, January 16, 2017 2:05 PM

All replies

  • You need tom export objects.  Strings will not export.

    Try naming your variables in a less ambiguous way and format your code so it is readable.

    Look at some of the tutorials and example code in the Gallery to see how to write a usable script.


    \_(ツ)_/

    Monday, January 16, 2017 2:20 PM
    Moderator
  • Updated the code in readable format.

    I want to extract the output using export-csv cmd along the custom title name in the csv file


    - Sivashankar. Please mark as answer/useful if my contribution is helpful

    Monday, January 16, 2017 3:27 PM
  • Updated the code in readable format.

    I want to extract the output using export-csv cmd along the custom title name in the csv file


    - Sivashankar. Please mark as answer/useful if my contribution is helpful

    Please post the code using the posting tool on the edit bar and use indentation to make the code readable. 

    Here is an example:

    Get-ChildItem |
         ForEach-Object{
              Write-Host $_
         }
    
    # OR
    
    foreach($user in $users){
         Write-Host $user
         ... other code
    }
    

    If you use PowerShell ISE or other code editor it will help you do this.

    To use Export-Csv you must generate objects.  Search for blogs that show how to do this in various ways.  Search for "[pscustomobject}"


    \_(ツ)_/



    Monday, January 16, 2017 3:53 PM
    Moderator
  • update the code readable format, thanks for guidence.

    could you Please update me where i need to use the psobject 


    - Sivashankar. Please mark as answer/useful if my contribution is helpful

    Monday, January 16, 2017 4:07 PM
  • Why no formatting.  All code is just lines. Notice my example has correct indentation.

    You need to search for articles that will teach you how to use objects with PowerShell.


    \_(ツ)_/

    Monday, January 16, 2017 4:50 PM
    Moderator