powershell is missing the terminator: \"

2019-01-19 10:20发布

问题:

I have the following script code

    #[string]$password = $( Read-Host "Input password, please" )
    param (
        [string]$ReleaseFile = $(throw "-ReleaseFile is required"),
        [string]$Destination = $(throw "-Destination is required")
    )

    function unzipRelease($src, $dst)
    {
        $shell = new-object -com shell.application
        $zip = $shell.NameSpace($src)
        foreach($item in $zip.items())
        {
            $shell.Namespace($dst).copyhere($item)
        }
    }

    #  .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination "."

    unzipRelease –Src '$ReleaseFile' -Dst '$Destination'

I run the script with: .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination "."

But I keep getting this:

    PS C:\Users\Administrator\Documents\Tools> .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination
    The string starting:
    At C:\Users\Administrator\Documents\Tools\deployrelease.ps1:19 char:16
    + unzipRelease â? <<<< "Src '$ReleaseFile' -Dst '$Destination'
    is missing the terminator: ".
    At C:\Users\Administrator\Documents\Tools\deployrelease.ps1:19 char:55
    + unzipRelease â?"Src '$ReleaseFile' -Dst '$Destination' <<<<
        + CategoryInfo          : ParserError: (Src `'$ReleaseF...'$Destination`':String) [], ParseException
        + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

I couldn't find the fix as I do not see any problem.

Any help?

回答1:

Look closely at the two dashes in

unzipRelease –Src '$ReleaseFile' -Dst '$Destination'

This first one is not a normal dash but an en-dash (&ndash; in HTML). Replace that with the dash found before Dst.



回答2:

In your script, why are you using single quotes around the variables? These will not be expanded. Use double quotes for variable expansion or just the variable names themselves.

unzipRelease –Src '$ReleaseFile' -Dst '$Destination'

to

unzipRelease –Src "$ReleaseFile" -Dst "$Destination"


回答3:

This error will also occur if you call .ps1 file from a .bat file and file path has spaces.

The fix is to make sure there are no spaces in the path of .ps1 file.