ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Powershell Import-CSV issue

    Scheduled Pinned Locked Moved IT Discussion
    powershellimport-csvcsv
    14 Posts 4 Posters 3.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nadnerBN
      nadnerB @coliver
      last edited by nadnerB

      @coliver said:

      I am trying to import a CSV file with powershell, iterate through it and run a command on each entry.

      $importcsv = import-csv "filename"
      foreach($guid in $importcsv)
          {
          $string1 = "(gPLink=*" + $guid + "*)"
          echo $string1
          }
      

      I'm able to do the first two things. However the third isn't working as expected. When I interate through it just by echoing the variable I get what I am expecting.

      31346975-2114-40b6-a423-8cbc9adc76fd
      

      However when I run it through $string1 I get the following

      (gPLink=*@{guid=31346975-2114-40b6-a423-8cbc9adc76fd}*)
      

      For some reason it adds @{guid= and then a } to the string.

      Anyone have any ideas how I can remove those extra characters?

      Try

      $importcsv = import-csv "filename"
      foreach($guid in $importcsv) 
         { 
         $string1 = "(gPLink=*" + $guid + "*)"``
         $noondles = $string1.Trim("@","{guid=","}")
         echo $noodles
        }
      
      1 Reply Last reply Reply Quote 0
      • nadnerBN
        nadnerB
        last edited by

        If you look in the csv file before the import, are the @{guid= and } in there?
        If so, how are you obtaining and exporting the GUIDs?

        coliverC 1 Reply Last reply Reply Quote 0
        • C
          Carnival Boy
          last edited by

          I'm no expert, but I reckon it is treating $guid as an array when you use "$string1 =", so the output is displaying as an array value, hence the @{ and } characters.

          coliverC 1 Reply Last reply Reply Quote 0
          • coliverC
            coliver @Dashrender
            last edited by

            @Dashrender said:

            I'm not a scriptor, but to me it looks like you are adding that stuff to your value

                $string1 = "(gPLink=*" + $guid + "*)"
            

            what if you just use

               $string1 = "*"
            

            I just get a "*"

            1 Reply Last reply Reply Quote 0
            • coliverC
              coliver @nadnerB
              last edited by

              @nadnerB said:

              If you look in the csv file before the import, are the @{guid= and } in there?
              If so, how are you obtaining and exporting the GUIDs?

              No, the CSV file doesn't contain the @(guid=...}. The header of the column is guid and that changes when I name the header something else.

              So it looks like when pulling it through string concatenation (which is what the + signs are doing) it pulls in both the header and the value. I just need the value.

              1 Reply Last reply Reply Quote 0
              • coliverC
                coliver @Carnival Boy
                last edited by

                @Carnival-Boy said:

                I'm no expert, but I reckon it is treating $guid as an array when you use "$string1 =", so the output is displaying as an array value, hence the @{ and } characters.

                I've tried to call out the first element in the array and it gives me an invalid operation and cannontIndex error. Which from searching seems to indicate it isn't an array.

                1 Reply Last reply Reply Quote 0
                • coliverC
                  coliver
                  last edited by

                  When I do

                  $guid.gettype()
                  

                  It returns this

                  IsPublic IsSerial Name                                     BaseType                                                         
                  -------- -------- ----                                     --------                                                         
                  True     False    PSCustomObject                           System.Object
                  
                  1 Reply Last reply Reply Quote 0
                  • nadnerBN
                    nadnerB
                    last edited by

                    Did you try the trim() thing?

                    coliverC 1 Reply Last reply Reply Quote 0
                    • coliverC
                      coliver @nadnerB
                      last edited by coliver

                      @nadnerB said:

                      Did you try the trim() thing?

                      Yes,

                      It gives me the following error

                      Cannot convert argument "1", with value: "{guid=", for "Trim" to type "System.Char": "Cannot convert value "{guid=" to type "System.Char". Error: "String must be exactly one character long.""
                      

                      I'm guessing that is because it isn't a string?

                      1 Reply Last reply Reply Quote 0
                      • C
                        Carnival Boy
                        last edited by

                        Instead of import-csv could you use Get-Content

                        ie
                        $importcsv = Get-Content "filename"
                        foreach($guid in $importcsv)
                        {
                        $string1 = "(gPLink=" + $guid + ")"
                        echo $string1
                        }

                        I've tested this and it works, assuming you only have one column in your CSV file.

                        coliverC 1 Reply Last reply Reply Quote 3
                        • coliverC
                          coliver @Carnival Boy
                          last edited by

                          @Carnival-Boy said:

                          Instead of import-csv could you use Get-Content

                          ie
                          $importcsv = Get-Content "filename"
                          foreach($guid in $importcsv)
                          {
                          $string1 = "(gPLink=" + $guid + ")"
                          echo $string1
                          }

                          I've tested this and it works, assuming you only have one column in your CSV file.

                          Perfect that does work. Thank you.

                          1 Reply Last reply Reply Quote 0
                          • coliverC
                            coliver
                            last edited by

                            I found an even easier way.

                            $guid.id
                            

                            Since the $guid is a Powershell custom object you can call individual elements of it. This is the first time I've really dug into Powershell but this is a good thing to know.

                            1 Reply Last reply Reply Quote 2
                            • 1 / 1
                            • First post
                              Last post