调用Sqlcmd错误:运行powershell调用远程DB服务器时用户xxxx登录失败

调用Sqlcmd错误:运行powershell调用远程DB服务器时用户xxxx登录失败,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,执行powershell脚本时,我获取调用SqlCmd:用户'DIR\MISAccount'的登录失败。。powershell脚本将读取.sql文件和.sql文件中定义的已执行语句。我已经在我的本地机器上测试了powershell和.sql脚本(应用程序和数据库在同一个框中),脚本工作正常 现在我在服务器上执行脚本时遇到了错误,这里的区别是powershell需要调用远程db服务器。我在下面查过了 通过SSMO从应用服务器连接-已工作, 通过CMD SQLCMD从应用服务器连接-已工作, 在DB

执行powershell脚本时,我获取
调用SqlCmd:用户'DIR\MISAccount'的登录失败。
。powershell脚本将读取.sql文件和.sql文件中定义的已执行语句。我已经在我的本地机器上测试了powershell和.sql脚本(应用程序和数据库在同一个框中),脚本工作正常

现在我在服务器上执行脚本时遇到了错误,这里的区别是powershell需要调用远程db服务器。我在下面查过了

通过SSMO从应用服务器连接-已工作, 通过CMD SQLCMD从应用服务器连接-已工作, 在DB服务器上运行powershell脚本-已工作

基于上述测试,我认为问题在于Powershell(在app server中运行)到远程DB服务器的凭据,但我没有解决问题的线索。任何帮助都将不胜感激

这里是powershell脚本

$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$SqlFile = "E:\User\Script_CreateTable.sql"
$OutputFile = "E:\User\CreateTable.log"
$Path = "E:\User\Password.txt"
$uid = 'sa'
$ConnectionString = "Server=WINPRD212\PROD_INC;Database=AccountMEC;"

$pwd = Get-Content $Path | ConvertTo-SecureString
$pwd.MakeReadOnly()


$creds = New-Object System.Data.SqlClient.SqlCredential($uid,$pwd)

$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = $ConnectionString
$con.Credential = $creds
$con.Open()


Invoke-SqlCmd -InputFile $SqlFile -verbose

$con.Close()

通过以下方法解决问题:

$creds=New Object System.Management.Automation.PSCredential$uid,$pwd $Password=$creds.GetNetworkCredential().Password

调用SqlCmd-InputFile$SqlFile-Username'sa'-Password$Password-ServerInstance'WINPRD212\PROD_INC'-数据库'AccountMEC'