cli-microsoft365 icon indicating copy to clipboard operation
cli-microsoft365 copied to clipboard

Expand the user guide with info about escaping in PowerShell

Open martinlingstuyl opened this issue 3 years ago • 6 comments

The CLI documentation could use some information about escaping in PowerShell.

Escaping double quotes

There's an issue in PowerShell 5 and 7 (a fix is implemented for POSH 7.3, but that's not live yet.) around parsing command arguments: https://github.com/PowerShell/PowerShell/issues/1995

The idea is that command arguments are being parsed twice for tools like the CLI. Once by PowerShell and once by the executable that's being called. The result is that you need to escape quotes twice.

Example The following code

m365 spo listitem set --webUrl "https://contoso.sharepoint.com/sites/Project-x" --id 1 --listTitle somelist --SomeField "{ `"test1`": `"test2`" }"

Would result in the following being saved to sharepoint: { test1: test2 }. As you can see: the double quotes are missing.

There are two methods to work around this:

Method 1: Escaping twice: In this situation you escape the double quotes for powershell, using a backtick (`) AND you escape it for the executable using backslash ():

m365 spo listitem set --webUrl "https://contoso.sharepoint.com/sites/Project-x" --id 1 --listTitle somelist --SomeField "{ \`"test1\`": \`"test2\`" }"

Method 2: Using verbatim strings with single quotes: In this situation you surround your value with a single quote, meaning that you don't have to escape double quotes in powershell. So no backtick. You still need to use a backslash though, to take care of the parsing in the lower level executable:

m365 spo listitem set --webUrl "https://contoso.sharepoint.com/sites/Project-x" --id 1 --listTitle somelist --SomeField `{ \"test1\": \"test2\" }`

Escaping @ and $

Some extra information about escaping @ when using tokens and $, for example in URL's, would be handy as well.

Where to add the explanations

We could add the explanation below the existing section here: https://pnp.github.io/cli-microsoft365/user-guide/using-cli/#values-with-quotes

But also: I think we should add pointers to this central documentation at least in every spot where this is evidently used, like with sending adaptive cards and setting taxonomy. All places where double quotes or @ tokens or $ are included in examples.

martinlingstuyl avatar Aug 31 '22 11:08 martinlingstuyl

this is a great suggestion and currently I don't see anything we could add to this page 👍 I suggest we open this up and improve the docs asap 👍

Adam-it avatar Sep 01 '22 23:09 Adam-it

@martinlingstuyl could you elaborate what you're proposing and what's missing from our existing guide to make it clearer for the person that will work on this?

waldekmastykarz avatar Sep 02 '22 06:09 waldekmastykarz

I'll rewrite it a bit 👍

martinlingstuyl avatar Sep 07 '22 15:09 martinlingstuyl

Ok @waldekmastykarz, @Adam-it, I've done some extra research and rewrote the issue.

@Adam-it, you're a posh user, do you recognize the issue I'm describing?

martinlingstuyl avatar Sep 09 '22 05:09 martinlingstuyl

Great suggestion and it's clearer now, @martinlingstuyl. Let's do it!

waldekmastykarz avatar Sep 09 '22 06:09 waldekmastykarz

Ok @waldekmastykarz, @Adam-it, I've done some extra research and rewrote the issue.

@Adam-it, you're a posh user, do you recognize the issue I'm describing?

totally 💪 ... except 🤔.. te second method "Method 2: Using verbatim strings with single quotes:" I think that you should be using single quotes like ' not `, as the second left at the end will actually break line when enter is pressed

Adam-it avatar Sep 15 '22 22:09 Adam-it

... except 🤔.. te second method "Method 2: Using verbatim strings with single quotes:" I think that you should be using single quotes like ' not `, as the second left at the end will actually break line when enter is pressed

Ah, yes, sharp. typo by me 😂

martinlingstuyl avatar Oct 17 '22 19:10 martinlingstuyl