Jonno Will
Jonno Will

Reputation: 11

Unexpected Token Error in expression or statement

I'm new to Powershell so I'll try my best to describe what i am doing. I'm trying to run a script that will clean out my WSUS server.

this is the part of the code where it gets stuck.

$Adamj `ServerAdminProxy = $Script:WSUSAdminProxy

The error returned

At line:1 char:8 + $Adamj ServerAdminProxy = $Script:WSUSAdminProxy + ~~~~~~~~~~~~~~~~ Unexpected token 'ServerAdminProxy' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

I though it may have had something to do with the ` to be honest I'm not sure.

Hope this is clear for you..

Upvotes: 1

Views: 9882

Answers (2)

Shadowzee
Shadowzee

Reputation: 547

` is the escape character. Anything directly after it will be treated as is. So `ServerAdminProxy is being treated as additional input or maybe a function call.

Also with the $Script:WSUSAdminProxy, the : will cause the $Script to be treated as a directory aka D:

I'm not sure how you are storing the values you want to retrieve, but you should probably stay away from special characters like ` : \ ' " in your variable names and stick to alphanumeric characters and underscore (_)

Upvotes: 0

Daniel Mann
Daniel Mann

Reputation: 59020

To create or display a variable name that includes spaces or special characters, enclose the variable name in braces. This directs PowerShell to interpret the characters in the variable name literally.

Ex:

${save`items} = "a", "b", "c"

Ref: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-5.1

Upvotes: 1

Related Questions