Share via


Creating a conection string with powershell throws an error

Question

Tuesday, April 9, 2019 5:24 PM

Hi am currently trying to execute this code using powershell

Function Verify-License(){

    $dbName = "site1000"
    $serverName = 'josuevm\SQL2016'
    $validation1 = $false                         
    $finalValidation  = $false    

    try{
        #Create a Connection and open it
        $connection = [System.Data.SQLClient.SQLConnection]::new()
        $connection.ConnectionString = "Server=($serverName);database=($dbName);User ID=dailycheckupvm\manager;trusted_connection=true;"
        $connection.Open();
   
        #Create a Command Object and the query to be executed
        $command = [System.Data.SQLClient.SQLCommand]::new()
        $command.Connection = $connection
        $command.CommandText = 'Select name from ICE.syslicense'
        
       
         #This variable will store the name of the license
        [string]$result = $Command.ExecuteScalar()
        
        #Read the Query
        $reader = $command.ExecuteReader()    

        #Read the path where the License physical is
        $licenseFilePath = 'F:\Epicor\ERP10\ISV\site1\SetupEnvironment'
        
        #Store all the files located in the directory
        $filesInLicenseFilePath = Get-ChildItem -Path $LicenseFilePath

        #Verify the License File match with the record in the DB
        foreach($file in $filesInLicenseFilePath){
        
          if($file.name -eq $result){
            $validation1 = $true
          }
        }
       
        #Validate if there is a record on the ICE.syslicense table
        if(($reader.FieldCount -gt 0) -and ($validation1 -eq $true)) {
          $finalvalidation = $true;
        }
        
        else {
          
          $finalvalidation = $false;
        }

     }
     catch {
          
          $errorMessage = $_.Exception.Message
          $failedItem = $_.Exception.ItemName     
          write-host $errorMessage
          write-host $failedItem
     }

     finally{
          #$command.Dispose()
          #$reader.Dispose()
          $Connection.Close()
     }

      $finalvalidation

} #End of Method Verify-License

But am getting this error

ontext Basic 

                    Validation of the ISV Installation\nException calling "Open" with "0" argument(s): "Cannot open 

                    database "site1000" requested by the login. The login failed.\nLogin failed for user 'NT 

                    AUTHORITY\SYSTEM'."\n\n   

All replies (3)

Tuesday, April 9, 2019 8:18 PM ✅Answered

I found the answer, it was removing the Trusted Connection and also use another user.

Regards


Tuesday, April 9, 2019 5:29 PM

You're setting trusted connection to true, so it's impersonating the user running the process.  Remove this and add a password for the user.


Tuesday, April 9, 2019 5:43 PM

I remove the trusted connection, add a user and a password. Now i get this error

Context Basic 

                    Validation of the ISV Installation\nException calling "Open" with "0" argument(s): "Instance 

                    failure."\n\n