如何在PowerShell中使用如下命令并将其拆分为多行?

&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" -verb:sync -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" -dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"

当前回答

另一种跨多行的方法是在字符串中间放置一个空表达式,并将其跨行分割:

示例字符串:

"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"

断线的:

"stackoverflow stackoverflow $(
)stackoverflow stack$(
)overflow stackoverflow"

其他回答

拖尾反撇号字符,即:

&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
-verb:sync `
-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
-dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"

留白很重要。要求格式为空格。

如果你有一个函数:

$function:foo | % Invoke @(
  'bar'
  'directory'
  $true
)

如果你有一个cmdlet:

[PSCustomObject] @{
  Path  = 'bar'
  Type  = 'directory'
  Force = $true
} | New-Item

如果你有申请:

{foo.exe @Args} | % Invoke @(
  'bar'
  'directory'
  $true
)

Or

icm {foo.exe @Args} -Args @(
  'bar'
  'directory'
  $true
)

在PowerShell 5和PowerShell 5 ISE中,也可以只使用Shift + Enter进行多行编辑(而不是在每行末尾使用标准的反引号):

PS> &"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" # Shift+Enter
>>> -verb:sync # Shift+Enter
>>> -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" # Shift+Enter
>>> -dest:contentPath="c:\websites\xxx\wwwroot,computerName=192.168.1.1,username=administrator,password=xxx"

你可以使用反勾运算符:

& "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
    -verb:sync `
    -source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web" `
    -dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"

这对我来说还是有点太长了,所以我将使用一些命名良好的变量:

$msdeployPath = "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"
$verbArg = '-verb:sync'
$sourceArg = '-source:contentPath="c:\workspace\xxx\master\Build\_PublishedWebsites\xxx.Web"'
$destArg = '-dest:contentPath="c:\websites\xxx\wwwroot\,computerName=192.168.1.1,username=administrator,password=xxx"'

& $msdeployPath $verbArg $sourceArg $destArg

另一种跨多行的方法是在字符串中间放置一个空表达式,并将其跨行分割:

示例字符串:

"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"

断线的:

"stackoverflow stackoverflow $(
)stackoverflow stack$(
)overflow stackoverflow"