10

Incredibly simple powershell script...

#Server side storage copy

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

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

fails with the error

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

The strangest part is if I copy and paste the contents of the ps1 file into a PowerShell command line, it works fine !? What's going on?

Obviously I have removed my storage container name and key, you will need to assign your own Azure storage account name and api key to replicate it.

EDIT: A screen shot of the script edited in Notepad++ with all characters visible...

A screen shot of the script edited in Notepad++ with all characters visible

Mick
  • 6,527
  • 4
  • 52
  • 67
  • 2
    Enable seeing hidden character, this usually means you have some funny characters in the commands line or the variable names. – MBen May 04 '17 at 07:06
  • @MBen done but the issue remains. I've also tried deleting and recreating the quotes a couple of times – Mick May 04 '17 at 07:18
  • It is similar with this [question](http://stackoverflow.com/questions/20706869/powershell-is-missing-the-terminator). – Shui shengbao May 04 '17 at 07:22
  • @Walter-MSFT correct... i've copied and pasted code from a web site there is a long dash.... not a short one after New-AzureStorageContext and immediately before StorageAccountName – Mick May 04 '17 at 07:24
  • @Mick Do you solve it? – Shui shengbao May 04 '17 at 07:25
  • Yes. I think it's helpful to use cat filename.ps1 in the terminal you can see determine which characters are bad, it's not obvious in the editor – Mick May 04 '17 at 07:28

2 Answers2

13

It's similar with this question.

When you copy cmdlets from Web, maybe there are some special characters. I suggest you could copy the cmdlets to Notepad then copy to PowerShell.

It's helpful to use cat filename.ps1 in the terminal you can see determine which characters are bad, it's not obvious in the editor.

Community
  • 1
  • 1
Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
  • In my case I had to change in Notepad++ the encoding to Windows-1252 to spot these special characters. From menu Encoding -> Character sets -> Western European -> Windows-1252. I spent hours to find the problem with my PowerShell script because of this. – Alexandru Dicu May 20 '21 at 22:06
4

So parts of the code are copied and pasted from websites which have non-standard dash characters. I found it easier to see the characters when you use the cat command from the powershell command line to list the file

Here's a screenshot of what it looks like listed in the powershell:- enter image description here

Mick
  • 6,527
  • 4
  • 52
  • 67