0

I have the following commands in a powershell script (within 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

How can I run the SqlPackage.exe as another user?

PS: It is within Jenkins so I can't run the ps1 file or SqlPackage.exe using runas windows dialog.

EDIT:

I think I am very close, so far I have the following script.

   $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 

I am getting the following error:

*** Argument 'Action' has an invalid value: 'Script /sf:DB.dacpac /Profile:publish.xml /TargetServerName:localdb /op:Publish.sql'

developer
  • 1,401
  • 4
  • 28
  • 73
  • 2
    Does this answer your question? [Running PowerShell as another user, and launching a script](https://stackoverflow.com/questions/28989750/running-powershell-as-another-user-and-launching-a-script) – gravity Feb 26 '20 at 18:23
  • Thanks @gravity. It is similar but does not answer how can I pass parameters. I am new to powershell so perhaps it is not so clear to me. – developer Feb 27 '20 at 10:16
  • 1
    For parameter help, look here.. but this is a *different question* than your original one, technically - https://stackoverflow.com/questions/16347214/pass-arguments-to-a-scriptblock-in-powershell – gravity Feb 27 '20 at 13:55
  • Thanks @gravity. – developer Feb 28 '20 at 16:42

1 Answers1

0

instead of creating session, use start-process. I would recommend providing complete path to the dacpac file with SourceFile switch along with the Profile switch

$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
  • 11,028
  • 3
  • 24
  • 37