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

其他回答

如果你有一个函数:

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

另一种更简洁的参数传递方法是飞溅。

将参数和值定义为一个散列表,如下所示:

$params = @{ 'class' = 'Win32_BIOS';
             'computername'='SERVER-R2';
             'filter'='drivetype=3';
             'credential'='Administrator' }

然后像这样调用你的commandlet:

Get-WmiObject @params

微软文档:关于喷溅

TechNet杂志2011:Windows PowerShell:飞溅

看起来它适用于Powershell 2.0及以上版本

拖尾反撇号字符,即:

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

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

你可以使用反勾运算符:

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