PowerShell script error: the string is missing the terminator:

PowerShell script error: the string is missing the terminator:

非常简单的 powershell 脚本...

#Server side storage copy

$SourceStorageAccount = "myStorageAccount"
$SourceStorageKey = "myKey"

$SourceStorageContext = New-AzureStorageContext –StorageAccountName $SourceStorageAccount -StorageAccountKey $SourceStorageKey

失败并出现错误

At E:\DeploymentScripts\Storage\Test.ps1:6 char:51 + ... geContext –StorageAccountName $SourceStorageAccount -StorageAccount ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The string is missing the terminator: ". + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

最奇怪的是,如果我将 ps1 文件的内容复制并粘贴到 PowerShell 命令行中,它可以正常工作!?怎么回事?

显然我已经删除了我的存储容器名称和密钥,您需要分配自己的 Azure 存储帐户名称和 api 密钥才能复制它。

编辑:在 Notepad++ 中编辑的脚本的屏幕截图,所有字符都可见...

与此类似question

当您从 Web 复制 cmdlet 时,可能会有一些特殊字符。我建议您可以将 cmdlet 复制到记事本,然后复制到 PowerShell。

使用 cat 文件名很有帮助。ps1 在终端中你可以看到确定哪些字符是错误的,在编辑器中并不明显。

因此,部分代码是从具有非标准短划线字符的网站复制和粘贴的。我发现当您从 powershell 命令行使用 cat 命令列出文件时更容易看到字符

这是 powershell 中列出的内容的屏幕截图:-