5/04/2014

Sometimes you feel so dumb… PowerShell Mistake!

 

I'm trying to search SharePoint using PowerShell and REST web services…

PS C:\> Invoke-WebRequest http://spserver/sites/demo3/_api/search/query?querytext='sharepoint'  -UseDefaultCredentials

I keep getting:

Invoke-WebRequest : The remote server returned an error: (400) Bad Request.

Sad smile

I spent the last hour doing web searches for "400" errors and Invoke-WebRequest and SharePoint and search etc.

Talk about the forest and the trees… The problem was with basic PowerShell strings in parameters. The single quotes got me! Quotes around strings should be required on parameters, but PowerShell lets you be lazy and leave them off… most of the time. Here's the correct solution:

PS C:\> Invoke-WebRequest "http://spserver/sites/demo3/_api/search/query?querytext='sharepoint'"  -UseDefaultCredentials

The only change was the addition of double quotes. Everything now works as expected! A full hour wasted climbing the wrong trees.

Smile

In the forest of ASCII, watch out for the quote trees…

.

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.