281

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

8 回答 8

421

尾随反引号字符,即

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

空白很重要。所需格式为Space`Enter.

于 2010-04-09T14:17:21.827 回答
81
于 2014-06-19T17:53:21.363 回答
44
于 2012-08-23T17:13:45.400 回答
21

您可以使用反引号运算符:

& "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
于 2013-08-01T17:44:49.187 回答
18

如果你有一个功能:

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

如果您有cmdlet

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

如果您有应用程序:

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

或者

icm {foo.exe @Args} -Args @(
  'bar'
  'directory'
  $true
)
于 2014-11-11T23:42:13.973 回答
4

在 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"
于 2016-02-03T15:05:45.223 回答
3

另一种将字符串拆分为多行的方法是将一个空表达式放在字符串的中间,然后将其拆分为多行:

示例字符串:

"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"

断线:

"stackoverflow stackoverflow $(
)stackoverflow stack$(
)overflow stackoverflow"
于 2019-11-18T16:48:19.347 回答
2

带计算的 Splat 方法

如果您选择 splat 方法,请注意使用其他参数进行的计算。在实践中,有时我必须先设置变量,然后再创建哈希表。此外,该格式不需要在键值或分号周围加上单引号(如上所述)。

Example of a call to a function that creates an Excel spreadsheet

$title = "Cut-off File Processing on $start_date_long_str"
$title_row = 1
$header_row = 2
$data_row_start = 3
$data_row_end = $($data_row_start + $($file_info_array.Count) - 1)

# use parameter hash table to make code more readable
$params = @{
    title = $title
    title_row = $title_row
    header_row = $header_row
    data_row_start = $data_row_start
    data_row_end = $data_row_end
}
$xl_wksht = Create-Excel-Spreadsheet @params

注意:文件数组包含将影响电子表格填充方式的信息。

于 2020-02-27T22:06:25.153 回答