0

I'm new on this site and a newby in programming Powershell.

I've made a Powershell script with forms. In the script I've made a connection string to a SQL server with integrated security (also tried credentials(domain and sql). In ISE all working fine, no problem. When I make a batchfile, all working fine. When I make an .exe of the ps1 file with ps2exe the script is running partly fine. The only thing is when I push a button to start a query, the script gives a message I "Login failed for user x".

Also tried invoke-sqlcmd, same problem. I've got all the rights to the server and databases.

Anyone got any idea or know if this is a bug in ps2exe?

     1. Function F_SQL_Query
     2. {
     3. param($VSQLServernaam, $VSQLDatabasenaam, $VSQLQueryString)
     4. $VdataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
     5. $VconnString = "data source=$VSQLServernaam;initial catalog=$VSQLDatabasenaam;Integrated Security=sspi;trusted_connection=true;trustServerCertificate=True;"
     6. $VdataAdapter.SelectCommand = new-object System.Data.SqlClient.SqlCommand ($VSQLQueryString,$VconnString) 
     7. $VcommandBuilder = new-object System.Data.SqlClient.SqlCommandBuilder $VdataAdapter 
     8. $Vdt = New-Object System.Data.DataSet
     9. [void]$VdataAdapter.fill($Vdt)
    10. $Vdt.tables
    11. }
rayrol
  • 1
  • 1
  • How are you executing the .exe result? When using "Integrated Security=sspi" the SQL connection will be established using the Windows (or Domain) credentials of the process and that account needs to be listed in the target SQL Server's logins collection. i.e.: it won't work if the target SQL Server expects a Domain login and you're invoking the .exe using .\SYSTEM or .\LocalService. – AlwaysLearning Mar 10 '20 at 10:24
  • Hai, also tried "Integrated Security=true", same result. My account has all rights on the server/ database. Why is the script working fine in ISE Powershell with this code and fails with an .exe? I also tried a SQL account.... – rayrol Mar 10 '20 at 10:39
  • Sorry, forgot someting. The executable is executing with my own account. – rayrol Mar 10 '20 at 10:47
  • PS2EXE uses an abbreviated version of the PS/.NET host to compile into executables. Does it work if you supply username and password directly instead of using integrated authentication? I'd also use Try...Catch to trap errors and get more specific detail about why it's refusing the connection. – Scepticalist Mar 10 '20 at 19:44
  • I've updated powershell to the last version. Now the .ps1 and .exe working fine. Thnx for the help! – rayrol Mar 12 '20 at 12:03

0 Answers0