PowerShell 脚本错误:字符串缺少终止符:

Mic*_*ick 7 powershell azure azure-storage azure-storage-blobs azure-powershell

非常简单的powershell脚本...

#Server side storage copy

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

$SourceStorageContext = New-AzureStorageContext –StorageAccountName $SourceStorageAccount -StorageAccountKey $SourceStorageKey
Run Code Online (Sandbox Code Playgroud)

因错误而失败

在 E:\DeploymentScripts\Storage\Test.ps1:6 char:51 + ... geContext –StorageAccountName $SourceStorageAccount -StorageAccount ... + ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 字符串缺少终止符:". + CategoryInfo : ParserError: (:) [], ParseException +fullyQualifiedErrorId : TerminatorExpectedAtEndOfString

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

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

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

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

Shu*_*bao 10

这与this question类似。

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

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