locked
Help with formatting RRS feed

  • Question

  • Hello 

    I am quite new to power shell and i am working on ye olde get-weather function. I am trying to find a way to take this function and create a single table instead of the function creating 10 separate tables. Is this possible or is the for each command really preventing me from doing that? The only instruction is that it must display an object and not just plain text.

    Function Get-Weather  {
    
      Param (
    
      [parameter()]
    
      [string]$City = "Lacey",
    
      [parameter()]
    
      [string]$State = 'WA'
    
      )
    
    $URI = "https://query.yahooapis.com/v1/public/yql?q=select  * from weather.forecast where woeid in (select woeid from geo.places(1) where  text='{0}, {1}')&format=json&env=store://datatables.org/alltableswithkeys" -f $City,$State
    
      
    
      $weather  = (Invoke-RestMethod  -Uri $URI).query.results.channel
    
    
      foreach ($i in $weather.item.forecast) 
      {
    
       $weatherobject  = [pscustomobject]@{
    
      DateName = $i.day
    
      Date = $i.date
    
      Hi = $i.high
    
      Low = $i.low
    
      Text = $i.text
    
      }
     Write-Output $weatherobject | FT -AutoSize
    
    
    
      } 
    
    }  

    Wednesday, June 20, 2018 1:01 AM

All replies

  • Homework problem? 


    \_(ツ)_/

    Wednesday, June 20, 2018 1:53 AM
  • First you need to learn how to correctly format your code so that it is readable.  You also need to work with objects and formatting commands do not create objects.  THey create text output.

    This will create objects.

    Function Get-Weather{
        Param (
            [parameter()]
            [string]$City = 'Lacey',
            [parameter()]
            [string]$State = 'WA'
        )
    
        $URI = "https://query.yahooapis.com/v1/public/yql?q=select  * from weather.forecast where woeid in (select woeid from geo.places(1) where  text='{0}, {1}')&format=json&env=store://datatables.org/alltableswithkeys" -f $City,$State
        (Invoke-RestMethod  -Uri $URI).query.results.channel.Item.forecast
    }
    
    # output the objects
    Get-Weather
    
    # format outlput as a table
    Get-Weather | Format-Table -AutoSize
    


    \_(ツ)_/

    Wednesday, June 20, 2018 2:03 AM
    • Edited by jrv Wednesday, June 20, 2018 2:05 AM
    Wednesday, June 20, 2018 2:04 AM
  • Ah thank you i was confused first get-weather creates the object and then i format the table which is text. Thank you for the information and appreciate the corrections.
    Wednesday, June 20, 2018 3:27 AM
  • Thank you for the links this will be a great help

    Wednesday, June 20, 2018 3:28 AM
  • Format-* commands create formatter objects which cause the output to look like a table in the console and can be output to a file as text.  It does not retain the original objects.

    The result from the Invoke is a collection of objects.  There is no real need to convert them.

    This is how PowerShell works:

    Function Get-Weather{
        Param (
            [parameter(ValueFromPipeline)]
            [string]$City = 'Princeton',
            [parameter()]
            [string]$State = 'NJ'
        )
    
        Process{
            $URI = "https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where  text='{0}, {1}')&format=json&env=store://datatables.org/alltableswithkeys" -f $City,$State
            $results = Invoke-RestMethod  -Uri $URI
            $results.query.results.channel.Item.forecast |
                ForEach-Object{
                    [pscustomobject]@{
                        State = $State
                        City  = $City
                        Code  = $_.code
                        Date  = $_.date
                        High  = $_.high
                        Low   = $_.low
                        Conditions = $_.text.Trim(20)
                    }
                }
        }
    }
    'Princeton','Montclair','Newark' | Get-Weather
    'Princeton','Montclair','Newark' | Get-Weather | Format-Table -AutoSize
    
    

    Try to never use any line of code until you understand what it does and how it does it.


    \_(ツ)_/

    Wednesday, June 20, 2018 3:36 AM