AnswerBun.com

Can I make this PowerShell Script accept commas?

Super User Asked by Mark Deven on October 8, 2020

The following powershell script outputs the results twice when there is a comma. It separates the strings as two entries. How can I make it treat it as one string instead of two?

Function Get-Weather {
    [Alias('Wttr')]
    [Cmdletbinding()]
    Param(
            [Parameter(
                Mandatory = $true,
                HelpMessage = 'Enter name of the City to get weather report',
                ValueFromPipeline = $true,
                Position = 0
            )]
            [ValidateNotNullOrEmpty()]
            [string[]] $City,
            [switch] $Tomorrow,
            [switch] $DayAfterTomorrow
    )

    Process
    {
        Foreach($Item in $City){
            try {

                # Check Operating System Version
                If((Get-WmiObject win32_operatingsystem).caption -like "*Windows 10*") {
                    $Weather = $(Invoke-WebRequest "http://wttr.in/$City" -UserAgent curl -UseBasicParsing).content -split "`n"
                }
                else {
                    $Weather = (Invoke-WebRequest "http://wttr.in/$City" -UseBasicParsing).ParsedHtml.body.outerText  -split "`n"
                }

                If($Weather)
                {
                    $Weather[0..16]
                    If($Tomorrow){ $Weather[17..26] }
                    If($DayAfterTomorrow){ $Weather[27..36] }
                }
            }
            catch {
                $_.exception.Message
            }
        }            
    }

}

Get-Weather Shrewsbury,MA?n1

One Answer

Also remember that when you use single quotes "single-quotation marks (a single-quoted string), the string is passed to the command exactly as you type it. No substitution is performed." Versus when you use double quotes "double quotation marks (a double-quoted string), variable names that are preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing."

This is from: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6 and is for PowerShell 6, but the same rules apply to PowerShell 1 through 6.

So unless you need to use a variable inside the string, the single quotes will force PowerShell to use the string exactly how you have it written.

Correct answer by DBADon on October 8, 2020

Add your own answers!

Related Questions

Download a file via HTTP from a script in Windows

8  Asked on February 24, 2021 by jason-r-coombs

       

ZSH prints the command I ran before showing the output

1  Asked on February 24, 2021 by anish-sharma

         

Virtualization is locked in BIOS

1  Asked on February 23, 2021 by ben-r

     

Send email from gmail alias with ssmtp or other

0  Asked on February 23, 2021 by cclloyd

       

Unable to set or remove a login PIN on Windows 10

2  Asked on February 22, 2021 by scott-severance

     

Open PDFs in Adobe instead of in Google Chrome

5  Asked on February 22, 2021 by ramzi-kahil

   

command not found after switching users

0  Asked on February 22, 2021 by dor00012

     

PC starts, but no display from the monitor

1  Asked on February 20, 2021 by micjaakson

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP