WindowsServerCore容器中PowerShell脚本报错:字符串缺少终止符'
Hey there, let's tackle that frustrating parser error you're seeing. The TerminatorExpectedAtEndOfString message is PowerShell's way of saying it hit an unclosed quote somewhere in your script—either a missing ' or " that leaves a string hanging open.
Looking at the snippet you shared:
$srcdir = "packages"; $destdir = "Web\Bin"; md $destdir; $fi...
The first three commands are totally fine, but the truncated $fi... part is almost certainly where the problem lives. Here's how to diagnose and fix it:
Common Causes & Fixes
- Unclosed quotes in file filtering/paths: If you were starting a
Get-ChildItemcommand (like$files = Get-ChildItem $srcdir -Filter '*.dll), forgetting the closing'would trigger this exact error. Double-check every string in your full script to make sure every opening quote has a matching closing quote. - Truncated script: It looks like your script got cut off at
$fi.... Make sure you're running the complete version. For example, a full working version of your migration script might look like this:
Note: I replaced$srcdir = "packages" $destdir = "Web\Bin" # Create destination directory (ignore if it already exists) New-Item -Path $destdir -ItemType Directory -Force # Recursively copy all DLLs from packages to Web/Bin Get-ChildItem -Path $srcdir -Filter "*.dll" -Recurse | Copy-Item -Destination $destdir -ForcemdwithNew-Item -Forcefor clarity (both work, but-Forceavoids errors if the directory already exists) and added proper closing quotes everywhere. - Escape characters gone wrong: If you have any special characters in paths (like spaces), make sure you're either wrapping the path in quotes or escaping correctly. For example, if your path was
packages\My Lib, you'd need"packages\My Lib"instead of an unquoted string.
How to Test
To narrow down the issue, run parts of your script step-by-step in the PowerShell session inside the windowsservercore:10.0.14393.206 image:
- Define your variables:
$srcdir = "packages"; $destdir = "Web\Bin" - Create the directory:
md $destdir - Run the next command piece by piece (starting with
$fi...expanded) to see exactly where the parser breaks.
That should help you track down the missing quote or truncated line causing the error.
内容的提问来源于stack exchange,提问作者Rigel Reyes

