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

当前回答

飞溅法与计算

如果您选择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

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

其他回答

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

示例字符串:

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

拖尾反撇号字符,即:

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

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

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

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

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

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

$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