SQL Powershell Querie that can be executed by windows schueduler

I am trying to create a PowerShell SQL query for example below :

USE users_db
Update users set name = 2 where category = '5'
Go

I then somehow would like be able to use Microsoft scheduler to execute this command / '.bat file' ?
John-S PretoriusTechnical Service Manager, Mid-AtlanticAsked:
Who is Participating?
 
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
Save the stuff between the *** in a PS1 file, and execute it
(remember to fill in $SQLServer = '<EnterSQLServerNameHere>'
and check that the DB is named users_db)

***********************************************************************
$SQLServer = '<EnterSQLServerNameHere>'
$SQLDB = 'users_db'

function QueryDB($DBServer, $Database, $Query, $BatchName) {
#write-host $DBServer, $BatchName
      $connection = new-object system.data.sqlclient.sqlconnection($connectionString)
      $connection.connectionString = $("Data Source=" + $DBServer + ";Initial Catalog=" + $Database + " ;Trusted_Connection=True; Connect Timeout=0; Application Name=" + $BatchName + ";" )
      $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
      $SqlCmd.CommandText = $query
      $SqlCmd.Connection = $connection
      $SqlCmd.CommandTimeout = 0
      $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
      $SqlAdapter.SelectCommand = $SqlCmd
      $DataSet = New-Object System.Data.DataSet
      $script:Supress = $SqlAdapter.Fill($DataSet)
      $connection.close()
      return ,$DataSet.Tables[0]
}

QueryDB $SQLServer $SQLDB  "Update users set name = 2 where category = '5'" "ApplicationName_GoodAsProfilerFilter"
***********************************************************************

Regards Marten
0
 
DcpKingCommented:
So you're using SQL Server 2008.

* With that comes a tool called SSIS.
* The interface for designing jobs like the one you want to create and then run them using SSIS is called BIDS, and you should be able to find it in your menu structure, somewhere under the SQL Server entries.
* You can use BIDS to create a new SSIS package and include in that an "Execute SQL" job.
* For the Execute SQL task you'll need to create the definition of a connection to the database you want to use: there's a wizard that will help you do that.
* Then, in the interface wizard for the Execute SQL task, you can type in your SQL:

Update users set name = 2 where category = '5'

(You'll already be using User_db, etc., and there'll be no need for the "Go".

* Finally you can run your new package in BIDS to test it.

* If all that (which isn't really very much, actually - it takes more to write about it than to do it!), you'll be able to create a SQL Server Agent Job that will execute your package and schedule that in the SQL Server Agent. Look here for a step-by-step instruction for deployment.here - they're much better at that than I am!

hope this helps

Mike
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
I am using SQL 2008, which is locked down to limited functionality in a parking environment.

Thank you for your input Mike, I am fully aware of using this approach but with the closed system I'm using in a parking environment that feature is not available (locked-down) indefinably. I do however have available Powershell and scheduler which is what I have been looking into but need a little assistance putting together a complete .bat script that will make the connection, process the sql query and close up again.

Also, I would be scheduling this on the same PC running SQL.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Just save the code as a SQL file and the use the SQLCMD utility to run it inside a schedule task.
Example of the SQLCMD utility (run form command prompt) to run a script in a SQL Server instance using trusted connection (-E):
sqlcmd -S ServerName\InstanceName -E -i C:\Path\SQLScript.sql

Open in new window

0
 
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
Save the file somwhere as <Filenamewhatever>.PS1
The in Windows scheduler, create a new basic task
copy and past this code into the execute part of windows scheduled task (don't have a English computer nearby, so do an educated guess), the code comes here:

Powershell -ExecutionPolicy bypass -file <FullPath>\<Filenamewhatever>.PS1

This will launch it, and bypass executionpolicy restricted bye the way. So its safer than fiddeling with Set-Executionpolicy unrestricted.

To make it a lot safer, let windows scheduled task run as a domain user (with low level rights) and the powershell file should be encrypted with this users EFS key. This means nobody else can alter the code that windows scheduled task executes. This would be considered a secure solution in most Companys.

Regards Marten
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Thank you for your input, I need direction/assistance creating this .PS1 file please.
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Thank you much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.