I have only one option to connect to SQL Server using Windows authentication mode. When I am trying to connect it through SQL Management studio it is working properly. Same thing I am trying to do using PowerShell using following code:
$Path = 'D:\'
write "Starting script --------" | Out-File $Path\output.txt
$SQLServer = 'server_name,1111'
$Database = 'db_name'
try {
$Connection = New-Object System.Data.SqlClient.SQLConnection
$Connection.ConnectionString = "Data Source=$SQLServer;Integrated Security=$true;Initial Catalog=$Database"
$Connection.Open()
Write-Host 'Open database connection'
write "Open database connection" | Out-File $Path\output.txt -Append
}
catch {
Write-Host 'An error occurs'
write "error opening connection: $_" | Out-File $Path\output.txt -Append
}
finally {
## Ensure closing the connection to release the resource / free memory
$Connection.Close()
Write-Host 'Close database connection'
write "Close database connection" | Out-File $Path\output.txt -Append
}
It is working properly when run it manually but it is showing following error when I schedule it in Windows Task Manager. In scheduler it is schedule with same user through which I have connect to server and same user is having rights to connect to DB.
error opening connection: Exception calling "Open" with "0" argument(s): "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'."
Security option. I have just marked some part of the username for security purposes
Can anyone help me what wrong I am doing here?
