繁体   English   中英

Powershell以不同的用户身份运行exe

[英]Powershell run exe as different user

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

使用 start-process 代替创建会话。 我建议使用 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM