none
Issue in reading a CSV file RRS feed

  • Question

  • Hi All,

    I've written a script as below to read contents of a text file into CSV and do some processing.

    $servers = Import-Csv "H:\Servers.txt"
    $servers
    foreach ($entry in $servers)
    {
     $ip = $entry.servername
     $user = $entry.userid
     $pwd = $entry.pwd
     write-host $ip
     write-host $user
     write-host $pwd
    # Do something
    }

    Servers.txt file has below format:

    ServerName UserID Pwd
    xxxxxxxxxx xx xxxxxxx
    xxxxxxxxxx xx xxxxxxx

    xxxxxxxxxx xx xxxxxxx

    The problem here is nothing is being read in for loop.
    The count of $servers is fine but I'm unable to read the contents in $servers object.
    All I get is blanks. 
    Any help??

    Regards,
    Pavan

    Friday, September 21, 2012 5:38 AM

Answers

  • This works for me like a charm:
    $CSVFile = 'C:\Temp\test.csv' 
    
    # Create Server CSV
    # Create Object array
    $ServerArr = @()
    $ServerArr += New-Object PSObject -Property @{Servername='Bill';UserID='BGates';Pwd='MakeMeRich'}
    $ServerArr += New-Object PSObject -Property @{Servername='Romney';UserID='MRomney';Pwd='AllVictims'}
    $ServerArr += New-Object PSObject -Property @{Servername='Obama';UserID='BObama';Pwd='VoteMe'}
    # Write the CSV with Object Array
    $ServerArr | Export-Csv -Path $CSVFile -NoTypeInformation # -InputObject $ServerArr
    
    # Read the CSV File
    $servers = Import-Csv $CSVFile
    $servers
    
    foreach ($entry in $servers)
    {
     $ip = $entry.servername
     $user = $entry.userid
     $pwd = $entry.pwd
     write-host "The IP is = $ip"
     write-host "The User is = $user"
     write-host "Teh Password is = $pwd"
    # Do something
    }


    Please click “Mark as Answer” if my post answers your question and click Vote as Help if my Post helps you.
    Bitte markiere hilfreiche Beiträge von mir als Hilfreich und Beiträge die deine Frage ganz oder teilweise beantwortet haben als Antwort.
    My PowerShell Blog http://www.admin-source.info
    [string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '

    Friday, September 21, 2012 6:13 AM

All replies

  • What is Delimiter in your file?

    (Import-Csv  has Delimiter switch)


    Сазонов Илья http://isazonov.wordpress.com/

    Friday, September 21, 2012 5:51 AM
  • This works for me like a charm:
    $CSVFile = 'C:\Temp\test.csv' 
    
    # Create Server CSV
    # Create Object array
    $ServerArr = @()
    $ServerArr += New-Object PSObject -Property @{Servername='Bill';UserID='BGates';Pwd='MakeMeRich'}
    $ServerArr += New-Object PSObject -Property @{Servername='Romney';UserID='MRomney';Pwd='AllVictims'}
    $ServerArr += New-Object PSObject -Property @{Servername='Obama';UserID='BObama';Pwd='VoteMe'}
    # Write the CSV with Object Array
    $ServerArr | Export-Csv -Path $CSVFile -NoTypeInformation # -InputObject $ServerArr
    
    # Read the CSV File
    $servers = Import-Csv $CSVFile
    $servers
    
    foreach ($entry in $servers)
    {
     $ip = $entry.servername
     $user = $entry.userid
     $pwd = $entry.pwd
     write-host "The IP is = $ip"
     write-host "The User is = $user"
     write-host "Teh Password is = $pwd"
    # Do something
    }


    Please click “Mark as Answer” if my post answers your question and click Vote as Help if my Post helps you.
    Bitte markiere hilfreiche Beiträge von mir als Hilfreich und Beiträge die deine Frage ganz oder teilweise beantwortet haben als Antwort.
    My PowerShell Blog http://www.admin-source.info
    [string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '

    Friday, September 21, 2012 6:13 AM
  • Thanks Peter. That did help. 

    Regards,
    Pavan

    Friday, September 21, 2012 6:34 AM