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
Get-Weather "Shrewsbury,MA?n1"
- But make sure you have some function to, perhaps, break the parameter into an array (delimited by the comma) and then use the array index to get whichever part of the parameter the rest of your script needs.