ForEach ($datas in $data)

I've a CSV file

Name Private Approval
ARoom115 $false $false

Running the below script

$datas = Import-Csv C:\MININT\asif.csv
ForEach ($datas in $data) {

$Name=$_.Name
$Private=[bool]$_.Private
$Approval=[bool]$_.Approval

"Name={0} Private={1} Approval={2}" -f $Name, $Private, $Approval

}

when I run script I get Name is empty (see below). Why I am getting Name is equal to empty? it should return ARoom115

Name= Private=False Approval=False





  • Edited by asif300 17 hours 30 minutes ago
November 5th, 2013 1:02pm

#Changed $datas to $DataInCSV
$DataInCSV= Import-Csv C:\MININT\asif.csv

#Changed $datas to $row
ForEach ($row in $DataInCSV) {
 
$Name=$row.Name
 $Private=[bool]$row.Private
 $Approval=[bool]$row.Approval
 
"Name={0} Private={1} Approval={2}" -f $Name, $Private, $Approval
}

You'd gotten two things wrong. Firstly you'd tried to do a foreach on the wrong object, your $data object was empty and your $datas object (which should be the collection of rows in the CSV) would have been overridden by the loop. Only the fact that your $data object was empty stopped it overwriting it.

Secondly you were using $_ which is valid syntax when you're doing a piplined for each operation  but not when you're doing a ForEach. It's complicated/confusing but they aren't really the same thing.

The reason you were getting anything for the Private and Approval values is that PowerShell was converting your null values to 'false'.

I renamed your variables and it should now work.



Free Windows Admin Tool Kit Click here and download it now
November 5th, 2013 1:31pm

$_ is used in the ForEach-Object cmdlet, not in the foreach statement.  Also, when you use the foreach statement, the name of the collection comes after the "in" keyword; you have them reversed.  Also, any non-empty string becomes True when you cast it to a boolean.  If you change your CSV so the Private and Approval fields are "True" and "False" instead of "$True" and "$False", you can use the [Convert]::ToBoolean() method, like this:

$datas = Import-Csv C:\MININT\asif.csv
foreach ($data in $datas) {
    $Name = $data.Name
    $Private = [Convert]::ToBoolean($data.Private)
    $Approval = [Convert]::ToBoolean($data.Approval)

    "Name={0} Private={1} Approval={2}" -f $Name, $Private, $Approval
}

November 5th, 2013 1:38pm

$datas = Import-Csv C:\MININT\asif.csv
ForEach ($data in $datas) {
	$Name=$data.Name
	$Private=[bool]$data.Private
 	$Approval=[bool]$data.Approval
	"Name={0} Private={1} Approval={2}" -f $Name, $Private, $Approval
}

Try the above. You had your ForEach($Data in $Datas) backwards and referencing $_ is for when you are sending data down the pipeline.

I hope this helps,
Mark

... apparently Alex was faster !


  • Edited by - Mark - 17 hours 10 minutes ago
Free Windows Admin Tool Kit Click here and download it now
November 5th, 2013 1:40pm

I've a CSV file

Name Private Approval
ARoom115 $false $false

Running the below script

$datas = Import-Csv C:\MININT\asif.csv
ForEach ($datas in $data) {

$Name=$_.Name
$Private=[bool]$_.Private
$Approval=[bool]$_.Approval

"Name={0} Private={1} Approval={2}" -f $Name, $Private, $Approval

}

when I run script I get Name is empty (see below). Why I am getting Name is equal to empty? it should return ARoom115

Name= Private=False Approval=False





Lots of correct answers here, but I thought I would ask why you chose to represent the boolean values of true and false in your .csv file as the string values "$true" and "$false"? There are two other conventions you might consider:

  1. use 'true' and 'false': these are the values that will be used when the boolean values "$true" and "$false" are written to a file, whether with set-content or export-csv. This fact can simplify things considerably if the csv is created by powershell code.
  2. use '1' and '': import-csv reads in strings. without casting to [bool], you could test these values in an if statement and find that a null value is interpreted as false, while any non-null value is interpreted as true.

I'd recommend option 1 if the .csv file is read or handled manually, as the presence of a null field could cause confusion for some readers. If the actual content of the csv file is hidden from the users, and managed only by a powershell script, you might consider option 2.

November 5th, 2013 4:16pm

looks like [convert]::ToBoolean will still work, even if your file contains "$false" instead of "False".

Just keep in mind that [bool]"False" is True. 

Free Windows Admin Tool Kit Click here and download it now
November 5th, 2013 4:22pm

looks like [convert]::ToBoolean will still work, even if your file contains "$false" instead of "False".

Just keep in mind that [bool]"False" is True. 

Yes, boolean operations can get overly complicated if you try hard enough to complicate it with things like double-negatives and weird casts. Casting to [bool] does not work intuitively, as it appears to interpret any string other than "" as true.

Not sure about [convert]::ToBoolean, though:

PS M:\> [convert]::ToBoolean('$false')
Exception calling "ToBoolean" with "1" argument(s): "String was not recognized as a valid Boolean."
At line:1 char:1
+ [convert]::ToBoolean('$false')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

PS M:\> [convert]::ToBoolean("$false")
False
PS M:\> [convert]::ToBoolean($false)
False
PS M:\>

I'd try to avoid casting altogether and change this:

$Private=[bool]$_.Private

to something like this:

$Private = ( $_.Private -eq 'true' )
and then indicate the boolean values in the csv file as 'true' and 'false'. This would be much less likely to throw an error in the event of processing data that has been corrupted. You could be even more paranoid about that possibility by testing for true and false and throwing an error if it is not one or the other...
November 5th, 2013 5:06pm

Oops, you're right.  When I tested with ToBoolean, I was using a double-quoted string.  So back to my original suggestion, get rid of those dollar signs in the CSV file (or have the script remove them before passing to ToBoolean).
Free Windows Admin Tool Kit Click here and download it now
November 5th, 2013 5:15pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics