如何在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()

其他回答

这招对我很管用:

& 'D:\Server\PSTools\PsExec.exe' @('\\1.1.1.1', '-accepteula', '-d', '-i', $id, '-h', '-u', 'domain\user', '-p', 'password', '-w', 'C:\path\to\the\app', 'java', '-jar', 'app.jar')

只需将路径或连接字符串放在一个数组项中,并将其他内容分别分割到一个数组项中。

这里还有很多其他选项:https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

微软应该让这种方法更简单,并与命令提示符语法兼容。

我在命令和参数中都有空格,这对我来说很有用:

$Command = "E:\X64\Xendesktop Setup\XenDesktopServerSetup.exe"
$Parms = "/COMPONENTS CONTROLLER,DESKTOPSTUDIO,DESKTOPDIRECTOR,LICENSESERVER,STOREFRONT /PASSIVE /NOREBOOT /CONFIGURE_FIREWALL /NOSQL"

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

这基本上与Akira的答案相同,但如果您动态地构建命令参数并将它们放入变量中,则可以工作。

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

…> \ file.exe。

or

……> \路径\ \ file.exe

所以,我遇到了一个类似的问题,我选择用这种方式来解决它:

用反撇号(')转义引号(")字符 用引号包围新表达式(") 使用调用操作符(&),对新字符串发出invoke-expression命令

示例解决方案:

&{调用-表达式"C:\程序文件\IIS\Microsoft Web Deploy\msdeploy.exe -verb:sync -source:dbfullsql= ' "数据源=mysource;集成安全=false;用户ID=sa;Pwd=sapass!;数据库=mydb; ' " -dest:dbfullsql= ' "数据源=.\mydestsource;集成安全=false;用户ID=sa;Pwd=sapass!;数据库=mydb; ' ",计算机名=10.10.10.10,用户名=管理员,密码=adminpass ' "}

您可以在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的编译方式或创建者的制作方式。