I'm trying to run a sql query from powershell as a scheduled job in SQL server (it's then getting emailed out by the same powershell script) and getting an error I can't sort out.
###
$SqlServer = “localhost”
$SqlCatalog = “myDatabase”
$SqlQuery = “select * from MY_VIEW″
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = “server=$SqlServer;Database=$SqlCatalog; user id = myUserid; password=myPassword”
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
This is the error I receive:
A job step received an error at line 7 in a PowerShell script. The corresponding line is '$SqlConnection.ConnectionString = "server=$SqlServer;Database=$SqlCatalog; user id = myUserid; password=myPassword"'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Unexpected token 'server=$SqlServer' in expression or statement. '.
What's wrong with my script?