0

I have a powershell script that I am calling from a Visual Studio web app (C#). I have an app pool set up, and the script executes. However, when I use any Invoke-DbaQuery command using the -SqlCredential, it works just fine from the powershell console, but it uses the app pool creds when called from the web app.

Here is some test code:

$mytest = Invoke-DbaQuery -SqlInstance $BackupInstance -Database "master" -SqlCredential $SourceCredential -Query "select suser_name()" -as SingleValue 

write-output "I am currently running as:  $mytest"

From powershell, this returns:

I am currently running as: MYDOMAIN\myAdUserName

When I run it through the web app I get:

I am currently running as: MYDOMAIN\AppPoolServerName$

I have changed my credentials to try a different user, and it returns that new user when ran from console, but still the machine name when ran from Web.

Any ideas why this is happening and how to fix?

Edit: $SourceCredential is being set as follows:

$SourceUsername = 'MYDOMAIN\MyAdUserName' $SecureIntPw = Get-Content "C:\Scripts\Security\MyAdUserName.txt" | ConvertTo-SecureString $SourceCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $SourceUserName, $SecureIntPw
alroc
  • 27,574
  • 6
  • 51
  • 97
M Rothwell
  • 45
  • 5
  • How are you specifying `$SourceCredential`? – HAL9256 May 31 '19 at 21:49
  • $SourceUsername = 'MYDOMAIN\MyAdUserName' $SecureIntPw = Get-Content "C:\Scripts\Security\MyAdUserName.txt" | ConvertTo-SecureString $SourceCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $SourceUserName, $SecureIntPw I have been using this for a long time in other environments with no issues. – M Rothwell May 31 '19 at 22:07
  • Hi @MRothwell , I know this is an old post, but have you got any luck on this issue ? I'm experiencing it now, have tried a number of stuff, including some DBATools commands to clear credential cache,etc... but it just sems to be a double-hop issue, like what you've mentioned in the Github forum. Hopefully you've arrived at a solution :) – Durairaj Veera Sinnaiah Oct 20 '20 at 05:54

0 Answers0