1

My PowerShell code is tripping some strange error codes, which I have thoroughly searched for to no avail.

I am trying to create a script that calculates some basic statistics for a number of CSV files, which seems to work until I try to create and populate new columns.

The error code I'm getting is:

Import-Csv: Cannot open file "C:\zMFM\Microsoft.Powershell.Commands.GenericMeasureInfo". At C:\zMFM\StatsDebug.ps1:46 Char:21

+$STATS2 = Import-CSV <<< $STATS

+CategoryInfo : OpenError (:) [Import-Csv], FileNotFoundException

+FullyQualifiedErrorId : FileOpenFailure.Microsoft.Powershell.Commands.ImportCsvCommand at C:\zMFM\statsdebug.ps1:55 char:20

That's followed by an error using a null expression, but I assume fixing this problem with Import-Csv will in turn fix that error. Here's my code, any help would be great, thanks!

$i = 1
#$colSTDDEV = New-Object System.Data.DataColumn StdDev,([double])
$colVZA = New-Object System.Data.DataColumn VZA,([double])
#$colVAZ = New-Object System.Data.DataColumn VAZ,([double])

While ($i -le 211) {

#Set the variable to the filename with the iteration number
$filename = "c:\zMFM\z550Output\20dSummer\fixed20dSum550Output$i.csv"


#Check to see if that a file with $filename exists. If not, skip to the next iteration of $i. If so, run the code to collect the 

statistics for each variable and output them each to a different file
If (Test-Path $filename) {


#Calculate the Standard Deviation
#First get the average of the values in the column
$STDEVInputFile = Import-CSV $filename

#Find the average and count for column 'td'
$STDEVAVG = $STDEVInputFile | Measure-Object td -Average | Select Count, Average
$DevMath = 0

# Sum the squares of the differences between the mean and each value in the array
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($Y.Average - $STDEVAVG.Average), 2)

#Divide by the number of samples minus one
$STDEV = [Math]::sqrt($DevMath / ($STDEVAVG.Count-1))

}

#Calculate the basic statistics for column 'td' with the MEASURE-OBJECT cmdlet
$STATS = Import-CSV $Filename |
Measure-Object td -ave -max -min

#Append the standard deviation variable to the statistics array and add the value

$STATS2 = Import-CSV $Stats
$VZA = $filename.VZA
#$VAZ = $filename.VAZ #COMMENTED FOR DEBUGING


#$STATS2.Columns.Add($colSTDDEV) #COMMENTED FOR DEBUGING
#$STATS2[0].StdDev = $STDEV #COMMENTED FOR DEBUGING


$STATS2.Columns.Add($colVZA)
$STATS2[0].VZA = $VZA


#$STATS2.Columns.Add($colVAZ) #COMMENTED FOR DEBUGING
#$STATS2[0].VZA = $VAZ #COMMENTED FOR DEBUGGING

#Export the $STATS file containing everything you need in the correct folder


Export-CSV -notype "c:\zMFM\z550Output\20dSummer\20dSum550Statistics.csv"

}
$i++
}
0

1 Answer 1

3
$STDEVInputFile = Import-CSV $filename
...
$STATS = Import-CSV $Filename |
  Measure-Object td -ave -max -min
# $STATS here will be type [GenericMeasureInfo], so you cannot use this as a filename.

$STATS2 = Import-CSV $Stats
# Are you trying to import $filename a third time here? You can't use $Stats because it's a [GenericMeasureInfo] object, not a [string].

Based on your code, it looks like you're trying to import a CSV with a filename of type [Microsoft.PowerShell.Commands.GenericMeasureInfo]. Filenames are strings. Also, why are you importing $filename twice? Would recommend importing it once, then operating off of the variable you saved it to.

Would also recommend changing your while loop to 1..211 | ForEach-Object. That way you won't have to worry about whether $i is less than or equal to your static number. Not a huge deal, but would make the code a little more readable.

TL;DR

Import-CSV $Stats is the problem. $Stats is not a valid filename, so you can't open/import it.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh, so changing the word 'STATS' to something else should fix the problem? I'll try it out in a few minutes when I'm back at my desk and report if that fixed it or not.
No, the problem is that you are storing Measure-Object td -ave -max -min into variable $STATS, and then you immediately try to open a CSV using $STATS as a filename when it is not a filename at all.
When you pipe something to Measure-Object, the result is an object of type [GenericMeasureInfo], which you can't use as a filename.
Okay, I think I understand now. Instead of immediately trying to view $STATS as a CSV, I first exported it as a CSV file and then re-imported it so the filetype will allow for columns to be added. It seems to have worked. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.