Powershell以其他用户身份运行exe

时间:2020-02-26 17:32:50

标签: powershell

我在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'

1 个答案:

答案 0 :(得分:0)

使用启动过程代替创建会话。我建议通过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