[Resolved] export-csv

This topic contains 3 replies, has 3 voices, and was last updated by  beben 2 weeks, 1 day ago.

  • Author
    Posts
  • #101734

    beben
    Participant

    Hello

    I have a little problem with a script and export-cvs
    no result with this scrip

    get-content -Path 'Sw_Ala-PileBureaux.cfg' | foreach-object {
      $UneLigne = $_
        if ($UneLigne.Contains("interface port1.") -or $UneLigne.Contains("interface port2."))
            {
            $interface = $UneLigne
            $interface = $interface.Substring(14)
            Write-Host "Port :" $interface.Trim()
            }
    
        if ($UneLigne.Contains("description"))
            {
            $description = $UneLigne
            $description = $description.Substring(12)
            Write-Host "Description" : $description.Trim()
            }
    
        if ($UneLigne.Contains("switchport") -and $UneLigne.Contains("vlan"))
            {
            $vlan = $UneLigne
            $vlandebut = $vlan.LastIndexOfAny("add")
            $vlan = $vlan.Substring($vlandebut + 1)
            Write-Host "Vlan :" $vlan.Trim()
            }
    
    } | export-csv 'Results.csv' -notype
    

    I don't understand because I use export-csv in other scrip and no problem.

    Do you have any idea ?

  • #101739

    Will Anderson
    Keymaster

    Write-Host executes direct to the console. Even using Write-Output would just write the string length using Export-Csv (at least in my tests it did). I'd use PSCustomObject to create the table to export.

    get-content -Path 'Sw_Ala-PileBureaux.cfg' | foreach-object {
        $UneLigne = $_
          if ($UneLigne.Contains("interface port1.") -or $UneLigne.Contains("interface port2."))
              {
              $interface = $UneLigne
              $interface = $interface.Substring(14)
              }
      
          if ($UneLigne.Contains("description"))
              {
              $description = $UneLigne
              $description = $description.Substring(12)
    
              }
      
          if ($UneLigne.Contains("switchport") -and $UneLigne.Contains("vlan"))
              {
              $vlan = $UneLigne
              $vlandebut = $vlan.LastIndexOfAny("add")
              $vlan = $vlan.Substring($vlandebut + 1)
              }
      
              [PSCustomObject]@{
                Port = $interface.Trim()
                Description = $description.Trim()
                Vlan = $vlan.Trim()
              }
    
      } | export-csv 'Results.csv' -notype
    
    • #101791

      beben
      Participant

      That work fine.

      Thank you very much

  • #101746

    Rob Simmers
    Participant

    The Contains operator or Method (as your using) is to determine if something exists in an array:

    PS C:\Users\Rob> "one","two" -contains "two"
    True
    

    You should be using the -Like operator with a wildcard:

    $UneLigne -like "*interface port1.*" -or $UneLigne -like "*interface port2.*"
    

You must be logged in to reply to this topic.