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


当前回答

请看本页: 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中更容易处理。详见这篇博客文章。

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

$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的答案相同,但如果您动态地构建命令参数并将它们放入变量中,则可以工作。

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

用反撇号(')转义引号(")字符 用引号包围新表达式(") 使用调用操作符(&),对新字符串发出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 ' "}

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

我在数组中放置参数,每行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";
  

请看本页: 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: