【问题标题】:Powershell run exe as different userPowershell以不同用户身份运行exe
【发布时间】:2020-02-26 17:32:50
【问题描述】:

我在 powershell 脚本中(在 Jenkins 中)有以下命令:

$Command = "C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe"
$Parms = "/Action:Script /sf:DB.dacpac /Profile:publish.xml"

$Prms = $Parms.Split(" ")
& "$Command" $Prms

如何以其他用户身份运行 SqlPackage.exe?

PS:它在 Jenkins 中,所以我无法使用 runas windows 对话框运行 ps1 文件或 SqlPackage.exe。

编辑:

我想我已经很接近了,到目前为止我有以下脚本。

   $sb = [scriptblock]::create("& ""SqlPackage.exe"" ""/Action:Script /sf:DB.dacpac /Profile:publish.xml /TargetServerName:localdb /op:Publish.sql""")
   $Secure_Password = ConvertTo-SecureString -String $parPassword -AsPlainText -Force
   $Credential = New-Object -Type PSCredential($parUserId,$Secure_Password)
   $Session    = New-PSSession -ComputerName ServerName -Credential $Credential
   Invoke-Command -Session $Session -ScriptBlock $sb 

我收到以下错误:

*** 参数 'Action' 的值无效:'Script /sf:DB.dacpac /Profile:publish.xml /TargetServerName:localdb /op:Publish.sql'

【问题讨论】:

标签: powershell


【解决方案1】:

不要创建会话,而是使用启动进程。我建议使用 SourceFile 开关和 Profile 开关提供 dacpac 文件的完整路径

$Command = "C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe"
$Parms = "/Action:Script /sf:DB.dacpac /Profile:publish.xml"

$Secure_Password = ConvertTo-SecureString -String $parPassword -AsPlainText -Force
$Credential = New-Object -Type PSCredential($parUserId,$Secure_Password)

Start-Process -Credentials $credential -FilePath $command -ArgumentList $Parms

【讨论】:

  • 感谢 Jawad,它解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多