Powershell Import-CSV issue



  • 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?



  • 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 = "*"


  • @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
      }


  • 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?



  • 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.



  • @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 "*"



  • @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.



  • @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.



  • When I do

    $guid.gettype()
    

    It returns this

    IsPublic IsSerial Name                                     BaseType                                                         
    -------- -------- ----                                     --------                                                         
    True     False    PSCustomObject                           System.Object


  • Did you try the trim() thing?



  • @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?



  • 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.



  • @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.



  • 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.