How to connect to a SQL Server database using PowerShell -


i working on powershell script supposed connect every morning company database, checking pending software approval requests in sccm, , sending emails concerned people allow them accept or refuse request link executes powershell script.

my question of first step, connecting database.

i created sql server account, able connect microsoft sql server management studio on server, doesn't connect script...

i found code on this website, , people apparently saying working, timeout error when run :

function getpendingrequests { param (     [string]$sqlserver,     [string]$sqldbname,     [string]$uid,     [string]$pwd ) $sqlquery = "select * [database].[dbo].[userapplicationrequests]"  $sqlconnection = new-object system.data.sqlclient.sqlconnection $sqlconnection.connectionstring = "server = $sqlserver; database = $sqldbname; user id = $uid; password = $pwd;"  $sqlcmd = new-object system.data.sqlclient.sqlcommand $sqlcmd.commandtext = $sqlquery $sqlcmd.connection = $sqlconnection  $sqladapter = new-object system.data.sqlclient.sqldataadapter $sqladapter.selectcommand = $sqlcmd  $dataset = new-object system.data.dataset $sqladapter.fill($dataset)  $sqlconnection.close()  return $dataset.tables[0]  } 

by way, [database] contains real database name, , variables correct... checked following points :

  • the server accepts windows , sql server authentication
  • the account can access database in microsoft sql server management studio, , read data way want
  • the computer retrieves server ip successfully

does have idea of did wrong, , why powershell function can't connect database?

are getting sort of error when run script?

it's possible need specify server database on (especially if it's on remote machine)

for example:

$sqlconnection.connectionstring = "server = localhost\mysqlserverinstance; database = $sqldbname; user id = $uid; password = $pwd;

this can of course done parameter, example.

take @ @ https://www.connectionstrings.com/sql-server/ , https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx more information on connection strings.

this guess since don't have lot of information on happening when run script (nor how invoking it), feel free respond additional details , we'll go there!


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -