none
Cannot display output for array RRS feed

  • Question

  • Hi All,
     
    I am stuck
     
    I was able to successfully create a foreach  loop as shown below and the output was as expected, each item was assigned to a new variable.
     
    NOTE: list.txt is used in both instances and is a CSV, contents looks like this 1,2,3,4,5,6,7
     
    This Works:

    $file = "D:\Code\list.txt" $DB = Get-Content $file foreach ($Data in $DB) { $NAME, $IP, $CPU, $MEMORY, $PGN, $GW, $SNM = $Data -split ',' -replace '^\s*|\s*$' write-host "Virtual Machine Name: "$NAME write-host "IP Address: "$IP write-host "CPU Count: "$CPU write-host "Memory: "$MEMORY write-host "Port Group: "$PGN write-host "Gateway: "$GW write-host "SubNet: "$SNM Write-Host "" }

    Output:

    Virtual Machine Name: 1
    IP Address: 2
    CPU Count: 3
    Memory: 4
    Port Group: 5
    Gateway: 6
    SubNet: 7

    So I then got a little tricker with the below.

    This Almost works:

    $FILEPATH = "D:\Code"
    $files = Get-ChildItem $FilePath\*.txt
    foreach ($file in $files)
    {
      (Get-Content $file)
        Foreach-Object { 
    foreach ($line in $file) {   
      $NAME, $IP, $CPU, $MEMORY, $PGN, $GW, $SNM = $Data -split ',' -replace '^\s*|\s*$' 
      write-host "Virtual Machine Name: "$NAME
      write-host "IP Address: "$IP
      write-host "CPU Count: "$CPU
      write-host "Memory: "$MEMORY
      write-host "Port Group: "$PGN
      write-host "Gateway: "$GW
      write-host "SubNet: "$SNM  
      Write-Host ""
                                    }
                    }
    }

    Output is now wrong:
     
    1,2,3,4,5,6,7
    Virtual Machine Name:
    IP Address:
    CPU Count:
    Memory:
    Port Group:
    Gateway:
    SubNet:
     
    Is anyone able to show me where I went wrong please?

     
    Tuesday, January 14, 2014 7:40 AM

Answers

  • Hi,

    jrv is right: You are doing things manually, when PowerShell has already powerful builtin Csv tools (Import-Csv most notably). The error on your second script however ought to lie here:

    # Wrong
    {
      (Get-Content $file)
        Foreach-Object {
    		
    # Correct
    {
    	Get-Content $file | Foreach-Object {
    
    # The whole script with Csv cmdlet:
    $FilePath = "D:\Code"
    $files = Get-ChildItem $FilePath\*.txt
    foreach ($file in $files)
    {
    	$Content = Import-Csv $file
    	$Content | FT
    }

    Cheers,
    Fred

    Edit: The Import-Csv example assumes you are using valid Csv that has a header naming the columns. If it does not, your first data entry will be assumed to be the header.


    There's no place like 127.0.0.1


    • Edited by FWN Tuesday, January 14, 2014 8:44 AM
    • Marked as answer by CyberIDentity Tuesday, January 14, 2014 9:34 AM
    Tuesday, January 14, 2014 8:42 AM
  • I only meant I have separated them with a "," the file format is still .txt does that change things?

    A CSV file is a text file.  If you have no header then Import-Csv has a -header parameter that allows us to add a header.

    help import-csv -full

    read all very carefully.


    ¯\_(ツ)_/¯

    • Marked as answer by CyberIDentity Tuesday, January 14, 2014 9:34 AM
    Tuesday, January 14, 2014 8:54 AM

All replies

  • For CSV files use Import-Csv

    Text only files cannot be decoded as CSV file.

    Rethink what you are trying to do adding the above information.


    ¯\_(ツ)_/¯

    Tuesday, January 14, 2014 8:31 AM
  • Hi,

    jrv is right: You are doing things manually, when PowerShell has already powerful builtin Csv tools (Import-Csv most notably). The error on your second script however ought to lie here:

    # Wrong
    {
      (Get-Content $file)
        Foreach-Object {
    		
    # Correct
    {
    	Get-Content $file | Foreach-Object {
    
    # The whole script with Csv cmdlet:
    $FilePath = "D:\Code"
    $files = Get-ChildItem $FilePath\*.txt
    foreach ($file in $files)
    {
    	$Content = Import-Csv $file
    	$Content | FT
    }

    Cheers,
    Fred

    Edit: The Import-Csv example assumes you are using valid Csv that has a header naming the columns. If it does not, your first data entry will be assumed to be the header.


    There's no place like 127.0.0.1


    • Edited by FWN Tuesday, January 14, 2014 8:44 AM
    • Marked as answer by CyberIDentity Tuesday, January 14, 2014 9:34 AM
    Tuesday, January 14, 2014 8:42 AM
  • I only meant I have separated them with a "," the file format is still .txt does that change things?
    Tuesday, January 14, 2014 8:51 AM
  • I only meant I have separated them with a "," the file format is still .txt does that change things?

    A CSV file is a text file.  If you have no header then Import-Csv has a -header parameter that allows us to add a header.

    help import-csv -full

    read all very carefully.


    ¯\_(ツ)_/¯

    • Marked as answer by CyberIDentity Tuesday, January 14, 2014 9:34 AM
    Tuesday, January 14, 2014 8:54 AM
  • Thanks Fred / jrv.

    I was making it much harder than it needed to be.

    Thank you both very much.

    Tuesday, January 14, 2014 9:36 AM
  • Thanks Fred / jrv.

    I was making it much harder than it needed to be.

    Thank you both very much.

    You are welcome.  Remember to always look at and read the help.  Also ty to spend some time looking into the bas CmdLets of PowerShell.  There is a huge amount of basic functionality that make PS very useful.


    ¯\_(ツ)_/¯

    Tuesday, January 14, 2014 10:05 AM