如何在PowerShell中运行以下命令?

C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe -verb:sync -source:dbfullsql="数据源=mysource;集成安全=false;用户ID=sa;Pwd=sapass!;数据库=mydb;"-dest:dbfullsql="Data Source=.\mydestsource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;",computername=10.10.10.10,username=administrator,password=adminpass"


当前回答

我尝试了所有的建议,但仍然无法运行参数包含空格的msiexec.exe。所以我的解决方案最终使用System.Diagnostics.ProcessStartInfo:

# can have spaces here, no problems
$settings = @{
  CONNECTION_STRING = "... ..."
  ENTITY_CONTEXT = "... ..."
  URL = "..."
}

$settingsJoined = ($settings.Keys | % { "$_=""$($settings[$_])""" }) -join " "
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.WorkingDirectory = $ScriptDirectory
$pinfo.FileName = "msiexec.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "/l* install.log /i installer.msi $settingsJoined"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$stdout = $p.StandardOutput.ReadToEnd()
$p.WaitForExit()

其他回答

我可以使用以下方法获得类似的命令:

msdeploy.exe -verb=sync "-source=dbFullSql=Server=THESERVER;Database=myDB;UID=sa;Pwd=saPwd" -dest=dbFullSql=c:\temp\test.sql

对于你的命令(并不是说它现在有多大帮助),事情看起来像这样:

msdeploy.exe -verb=sync "-source=dbfullsql=Server=mysource;Trusted_Connection=false;UID=sa;Pwd=sapass!;Database=mydb;" "-dest=dbfullsql=Server=mydestsource;Trusted_Connection=false;UID=sa;Pwd=sapass!;Database=mydb;",computername=10.10.10.10,username=administrator,password=adminpass

重点是:

在源参数周围使用引号,并删除连接字符串周围的嵌入引号 在构建SQL连接字符串时使用不包含空格的替代键名。例如,使用“UID”而不是“User Id”,“Server”而不是“Data Source”,“Trusted_Connection”而不是“Integrated Security”,等等。我只能让它工作,一旦我从连接字符串中删除所有空格。

我没有尝试在命令行末尾添加“computername”部分,但希望这些信息将帮助其他人阅读这篇文章,现在更接近他们想要的结果。

如果有人想知道如何运行一个可执行文件:

…> \ file.exe。

or

……> \路径\ \ file.exe

这招对我很管用:

PowerShell.exe -Command "& ""C:\Some Script\Path With Spaces.ps1"""

关键似乎是整个命令都用外引号括起来,“&”&用于指定正在执行的另一个子命令文件,然后最后在路径/文件名周围转义(双-双-)引号,其中有空格。

这也是MS连接问题的唯一解决方案,即-File不传递非零返回码,而-Command是唯一的替代方案。但到目前为止,人们认为-Command的一个限制是它不支持空格。我也更新了反馈内容。

http://connect.microsoft.com/PowerShell/feedback/details/750653/powershell-exe-doesn-t-return-correct-exit-codes-when-using-the-file-option

请看本页: https://slai.github.io/posts/powershell-and-external-commands-done-right/

摘要使用vshadow作为外部可执行文件:

$exe = "H:\backup\scripts\vshadow.exe"
&$exe -p -script=H:\backup\scripts\vss.cmd E: M: P:

您可以在powershell中以不同的方式运行exe文件。例如,如果你想运行unar .exe并提取一个。rar文件,你可以简单地在powershell中这样写:

$extract_path = "C:\Program Files\Containing folder";
$rar_to_extract = "C:\Path_to_arch\file.rar"; #(or.exe if its a big file)  
C:\Path_here\Unrar.exe x -o+ -c- $rar_to_extract $extract_path;

但有时,这行不通,所以你必须使用&参数,如下所示: 例如,使用vboxmanage.exe(一个管理virtualbox虚拟机的工具),你必须像这样调用字符串之外的参数,不带引号:

> $vmname = "misae_unrtes_1234123"; #(name too long, we want to change this)
> & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' modifyvm $vmname --name UBUNTU;

如果你想简单地调用winrar归档文件作为.exe文件,你也可以用invoke命令cmdlet和一个Silent参数/S(它将在压缩的文件夹中解压缩自己)来解压缩它。

> Invoke-Command -ScriptBlock { C:\Your-path\archivefile.exe /S };

因此,在powershell中有几种方法来运行带有参数的.exe文件。

有时,人们必须找到一个变通方法来使其正常工作,这可能需要一些进一步的努力和痛苦:),这取决于.exe的编译方式或创建者的制作方式。