Powershell Import-CSV issue
-
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.