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