この PowerShell スクリプトでコンマを受け入れるようにできますか?

この PowerShell スクリプトでコンマを受け入れるようにできますか?

次の PowerShell スクリプトは、コンマがある場合に結果を 2 回出力します。文字列を 2 つのエントリとして区切ります。これを 2 つの文字列ではなく 1 つの文字列として処理するにはどうすればよいでしょうか。

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

答え1

また、一重引用符「一重引用符 (一重引用符で囲まれた文字列) を使用する場合、文字列は入力したとおりにコマンドに渡されます。置換は実行されません。」一方、二重引用符「二重引用符 (二重引用符で囲まれた文字列) を使用する場合、ドル記号 ($) が先頭にある変数名は、文字列が処理のためにコマンドに渡される前に変数の値に置き換えられます。」

これは次のものです:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6これは PowerShell 6 用ですが、PowerShell 1 から 6 にも同じルールが適用されます。

したがって、文字列内で変数を使用する必要がない限り、単一引用符により、PowerShell は記述したとおりに文字列を使用するようになります。

関連情報