如何在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"


当前回答

我使用这种简单、干净、有效的方法。

我在数组中放置参数,每行1个。这样就很容易阅读和编辑了。 然后我使用一个简单的技巧,将所有参数都放在双引号内传递给一个只有一个形参的函数。这将它们(包括数组)压扁为一个字符串,然后我使用PS的“Invoke-Expression”执行。此指令专门用于将字符串转换为可运行命令。 适用:

# function with one argument will flatten 
# all passed-in entries into 1 single string line
Function Execute($command) {
    # execute:
    Invoke-Expression $command;
    # if you have trouble try:
    # Invoke-Expression "& $command";
    # or if you need also output to a variable
    # Invoke-Expression $command | Tee-Object -Variable cmdOutput;
}

#  ... your main code here ...

# The name of your executable app
$app = 'my_app.exe';
# List of arguments:
# Notice the type of quotes - important !
# Those in single quotes are normal strings, like 'Peter'
$args = 'arg1',
        'arg2',
        $some_variable,
        'arg4',
        "arg5='with quotes'",
        'arg6',
        "arg7 \ with \ $other_variable",
        'etc...';
    
# pass all arguments inside double quotes
Execute "$app $args";
  

其他回答

这招对我很管用:

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看到以字符串开头的命令时,它只计算字符串,也就是说,它通常会将其回显到屏幕上,例如:

PS> "Hello World"
Hello World

如果你想让PowerShell将字符串解释为命令名,那么使用调用操作符(&),如下所示:

PS> & 'C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe'

After that you probably only need to quote parameter/argument pairs that contain spaces and/or quotation chars. When you invoke an EXE file like this with complex command line arguments it is usually very helpful to have a tool that will show you how PowerShell sends the arguments to the EXE file. The PowerShell Community Extensions has such a tool. It is called echoargs. You just replace the EXE file with echoargs - leaving all the arguments in place, and it will show you how the EXE file will receive the arguments, for example:

PS> echoargs -verb:sync -source:dbfullsql="Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=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

Arg 0 is <-verb:sync>
Arg 1 is <-source:dbfullsql=Data>
Arg 2 is <Source=mysource;Integrated>
Arg 3 is <Security=false;User>
Arg 4 is <ID=sa;Pwd=sapass!;Database=mydb;>
Arg 5 is <-dest:dbfullsql=Data>
Arg 6 is <Source=.\mydestsource;Integrated>
Arg 7 is <Security=false;User>
Arg 8 is <ID=sa;Pwd=sapass!;Database=mydb; computername=10.10.10.10 username=administrator password=adminpass>

使用回声,你可以不断实验,直到你得到正确的答案,例如:

PS> echoargs -verb:sync "-source:dbfullsql=Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;"
Arg 0 is <-verb:sync>
Arg 1 is <-source:dbfullsql=Data Source=mysource;Integrated Security=false;User ID=sa;Pwd=sapass!;Database=mydb;>

事实证明,我以前在连接字符串周围保持双引号时太过努力了。显然,这是不必要的,因为即使cmd.exe也会删除这些文件。

顺便说一句,向PowerShell团队致敬。他们在向我展示单引号和双引号的具体咒语以获得所需的结果方面非常有帮助-如果你需要保持内部双引号到位。:-)他们也意识到这是一个痛苦的领域,但他们是由受特定问题影响的人数所驱动的。如果这对你来说是一个痛苦的领域,那么请为这个PowerShell错误提交投票。

有关PowerShell如何解析的更多信息,请查看我的Effective PowerShell博客系列——特别是第10项——“理解PowerShell解析模式”

2012年4月4日更新:这种情况在PowerShell V3中更容易处理。详见这篇博客文章。

我有以下代码在我的笔记本电脑上完美地工作:

& $msdeploy `
-source:package="$publishFile" `
-dest:auto,computerName="$server",includeAcls="False",UserName="$username",Password="$password",AuthType="$auth" `
-allowUntrusted  `
-verb:sync  `
-enableRule:DoNotDeleteRule `
-disableLink:AppPoolExtension  `
-disableLink:ContentExtension  `
-disableLink:CertificateExtension  `
-skip:objectName=filePath,absolutePath="^(.*Web\.config|.*Environment\.config)$" `
-setParam:name=`"IIS Web Application Name`",value="$appName"

然后,当我试图直接在一台服务器上运行时,我开始得到这些错误“无法识别的参数……等....所有参数必须以“-”开头。

在尝试了所有可能的解决方案(没有成功)后,我发现服务器(Windows 2008 R2)上的Powershell是3.0版本,而我的笔记本电脑是5.0版本。(你可以使用“$PSVersionTable”查看版本)。

升级Powershell到最新版本后,它又开始工作了。

只需在.exe名称之前添加&操作符。 下面是沉默模式下安装SQL Server Express的命令:

$fileExe = "T:\SQLEXPRADV_x64_ENU.exe"
$CONFIGURATIONFILE = "T:\ConfSetupSql2008Express.ini"

& $fileExe  /CONFIGURATIONFILE=$CONFIGURATIONFILE