validation with function
This commit is contained in:
@@ -1,15 +1,33 @@
|
|||||||
$sqlConn = New-Object System.Data.SqlClient.SqlConnection
|
function Test-SqlConnection {
|
||||||
$sqlConn.ConnectionString = "Server=$env:computername\SQL2019;Integrated Security=false;User ID=sa; Password=P@ssword!!"
|
param(
|
||||||
$sqlConn.Open()
|
[Parameter(Mandatory)]
|
||||||
$str1=""
|
[string]$ServerName,
|
||||||
$sqlConn.State
|
|
||||||
IF (Compare-Object $sqlConn.State $str1)
|
[Parameter(Mandatory)]
|
||||||
{
|
[string]$IntegratedSecurity,
|
||||||
Write-Host "Failed attempt"
|
|
||||||
exit 1
|
[Parameter(Mandatory)]
|
||||||
|
[string]$UserName,
|
||||||
|
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[string]$Password
|
||||||
|
)
|
||||||
|
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
|
try {
|
||||||
|
$connectionString = 'Server={0};Integrated Security={1};User ID={2};Password={3}' -f $ServerName,$IntegratedSecurity,$UserName,$Password
|
||||||
|
Write-Host -Object "Trying to connect to SQL Express instance: $ServerName"
|
||||||
|
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString
|
||||||
|
$sqlConnection.Open()
|
||||||
|
Write-Host -Object "Connection to SQL Express was successful."
|
||||||
|
} catch {
|
||||||
|
Write-Host -Object "Connection to SQL Express cannot be established."
|
||||||
|
exit 1
|
||||||
|
} finally {
|
||||||
|
## Close the connection when we're done
|
||||||
|
$sqlConnection.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ELSE
|
$instanceName = "$env:computername\SQL2019"
|
||||||
{
|
Test-SqlConnection -ServerName $instanceName -IntegratedSecurity "false" -UserName "sa" -Password "P@ssword!!"
|
||||||
Write-Host "Success"
|
|
||||||
exit 0
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user