如何在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中更容易处理。详见这篇博客文章。

你可以使用很多方法来做到这一点。

还有其他方法,如使用调用操作符(&),Invoke-Expression cmdlet等。但它们被认为是不安全的。微软建议使用Start-Process。

方法1

一个简单的例子

Start-Process -NoNewWindow -FilePath "C:\wamp64\bin\mysql\mysql5.7.19\bin\mysql" -ArgumentList "-u root","-proot","-h localhost"

在你的情况下

Start-Process -NoNewWindow -FilePath "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -ArgumentList "-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"

在这个方法中,你用逗号分隔ArgumentList中的每个参数。

方法2

简单的例子

Start-Process -NoNewWindow -FilePath "C:\wamp64\bin\mysql\mysql5.7.19\bin\mysql" -ArgumentList "-u root -proot -h localhost"

在你的情况下

Start-Process -NoNewWindow -FilePath "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -ArgumentList "-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"

这个方法更简单,因为它允许一次性输入参数。

注意,在powershell中,要在字符串中表示引号("),您应该插入重音(')(这是美国键盘中Tab键上方的键)。 -NoNewWindow 参数用于在当前控制台窗口中显示新进程。默认情况下,Windows PowerShell会打开一个新窗口。

参考:Powershell/Scripting/Start-Process

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

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

& $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