I am currently working on a PowerShell script that is supposed to connect every morning to my company database, checking for the pending Software Approval requests in SCCM, and sending emails to concerned people which allow them to accept or refuse the request with a link which executes a PowerShell script.
My question is currently about of the first step, which is connecting to the database.
I created a SQL Server account, with which I am able to connect to Microsoft SQL Server Management Studio on the server, but which doesn't connect with my script...
I found this code on this website, and people are apparently saying that it is working, But I always get a timeout error when I run it :
Function GetPendingRequests {
param (
[string]$SQLServer,
[string]$SQLDBName,
[string]$uid,
[string]$pwd
)
$SqlQuery = "select * from [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 the way, the [Database] contains the real database name, and variables are correct... I already checked the following points :
- The server accepts Windows and SQL Server authentication
- The account can access the database in Microsoft SQL Server Management Studio, and read the data the way I want
- The computer retrieves the server IP successfully
Does anyone have an idea of what I did wrong, and why this PowerShell function can't connect to the database?