title: trying to connect to SQL using Get-Credential in Winpe
body: <p>Does anyone know how to pass Get-Credentials to a SQL login using powershell?</p>
  
  <p>I have tried many different ways but no success. </p>
  
  <p>This needs to run in winpe - i can get the credentials using Get-Credential but i need to use those to connect to SQL DB - i keep getting.</p>
  
  <p>Login failed for 'NT AUTHORITY\ANONYMOUS LOGIN' The below code works fine in Windows - I am logged on as the user though so it must not be passing the credentials. if i remove the trusted_connection=true; i get the failed login and this is the best test for WINPE as no one is logged on. Is there a way to pass those Get-Credentials to SQL? 
  Either that or the same code does not work in WINPE - not sure why though?</p>
  
  <pre><code>$Cred = Get-Credential
  
  Function SQL_CONNECT ($Query){
  
      $ConnectionString = "server=VM855;database=LDMS2016;user   id=$Cred.Username;password=$Cred.Password;trusted_connection=true;"
      $SqlConnection = New-Object  System.Data.SQLClient.SQLConnection($ConnectionString)
      $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
      $SqlCmd.Connection = $SqlConnection
      $SqlCmd.CommandText = $Query
      $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
      $SqlAdapter.SelectCommand = $SqlCmd
      $DataSet = New-Object System.Data.DataSet
      $a = $SqlAdapter.Fill($DataSet)
      $SqlConnection.Close()
      $DataSet.Tables[0]
  }
  
  $Owners = SQL_CONNECT "Select Task_Name, Task_owner, first_action_query_date from PROV_HISTORY_TASK" | Select-Object Task_Owner, first_action_query_date
  
  $Owners
  </code></pre>