Welcome Forums General PowerShell Q&A what is difference in between 2 variable

Viewing 2 reply threads
  • Author
    Posts
    • #172444
      Participant
      Topics: 4
      Replies: 5
      Points: 38
      Rank: Member

      Hello,

      Can you tell what is difference between on 2 variable: $data and $res.
      I have script what is reading csv file and generate graph from them.
      $data is not working when I try to genarate graps but $res is working.

      $DataFileName = "c:\temp\sample.file"
      
      $data = Import-CSV $DataFileName -Delimiter "`t"  
      
      $data = $data | 	
          Select-Object @{Name="DateTime"; Expression={[datetime]::parseexact($_."DateTime","dd.MM.yyyy HH:mm:ss",$null)}} ,
                        @{Name="Delay"; Expression={[int]$_."RoundtripTime"}} 
      			  
      $res = foreach ( $i in $Data ) {
              [PSCustomObject]@{
                  DateTime = ([datetime]$i."DateTime")
                  Delay = ([int]$i."Delay")	
              }
          }		
      ...
      ...
      ...
      # Graph Creation
      # Not Working
      $chart.Series["Delay"].Points.DataBindXY($data.DateTime, $data.Delay)
      
      # Working
      $chart.Series["Delay"].Points.DataBindXY($res.DateTime, $res.Delay)
      
    • #172447
      Participant
      Topics: 4
      Replies: 5
      Points: 38
      Rank: Member

      Error Message what I get when I use $data variable is

      Exception calling "DataBindXY" with "2" argument(s): "Series data points do not support values of type System.Managemen
      t.Automation.PSObject only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String,
      DateTime, short, ushort."
      At C:\UserData\Work\Powershell Scripts\Ping\Graph_Maker_Agrecate.ps1:225 char:2
      + $chart.Series["Delay"].Points.DataBindXY($Data.datetime, $Data. ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ArgumentException
      

      when I use $data or $res I get same kind variable info

      $data[0].datetime.gettype()
      $data[0].Delay.gettype()
      
      IsPublic IsSerial Name                                     BaseType
      -------- -------- ----                                     --------
      True     True     DateTime                                 System.ValueType
      True     True     Int32                                    System.ValueType
      
    • #172567
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      Lake,

      Without having access to some sample data it’s going to be hard to tell you the cause. I would also note that $Data is considered for one-liners and $res is proper coding practice when building a script. I don’t know if I would waste to much time trying to figure out why the non-best practice method doesn’t work.

      I suspect the overall object type may be causing an issue, example below of just importing a basic csv file in test1.

      PS C:\> $Test.gettype()
      
      IsPublic IsSerial Name BaseType
      -------- -------- ---- --------
      True True Object[] System.Array
      
      
      PS C:\> $Test2.gettype()
      
      IsPublic IsSerial Name BaseType
      -------- -------- ---- --------
      True False PSCustomObject System.Object
Viewing 2 reply threads
  • The topic ‘what is difference in between 2 variable’ is closed to new replies.