0

I am new to PowerShell and I am getting powershell parsing error. My script is as:

$ServerName=".\SQL"

$ScriptLoc=$pwd

echo $pwd

$DrScript=$(scriptLoc)\main_file.sql

echo $DrScript

After executing the script I am getting:

Unexpected token '\main_file.sql' in expression or statement. At D:\Powershell-MyScripts\powerfile.ps1:8 char:45 + $DrScript=$(scriptLoc)\main_file.sql <<<< + CategoryInfo : ParserError: (\main_file.sql:String) [], ParseException + FullyQualifiedErrorId : UnexpectedToken

please help me to get over it.

PS: I am converting the batch script to Powershell script so I facing such error. Please advise if there is good tutorial which help me to get over this.

2 Answers 2

2

You're building the string in $DrScript=$(scriptLoc)\main_file.sql incorrectly. To construct that string, wrap the variable and the constant in double quotes; the variable will be expanded automatically.

$DrScript="$scriptLoc\main_file.sql"

Wrapping the variable in parens is only necessary if it's an object whose property you need to access directly within a quoted string. For example:

$DrScript="$($scriptLoc.propertyname)\main_file.sql"

But you don't need that here.

Also, PowerShell style note: echo is an alias for Write-Output. Aliases should be reserved for the command line; when you're writing a reusable script, use the full cmdlet name. Aliases can be redefined (or removed entirely) in another user's environment, so creating a dependency upon one that you aren't defining in your script can cause failure.

Sign up to request clarification or add additional context in comments.

Comments

1

You're missing quotes around your path. Also need to ensure that you have your variable $scriptLoc named properly with a dollar sign at the beginning.

$DrScript="$($scriptLoc)\main_file.sql" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.