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

拖尾反撇号字符,即:

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

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

啊,如果你有一个很长的字符串,你想要分开,比如HTML,你可以在外层的每一边放一个@,就像这样:

$mystring = @"
Bob
went
to town
to buy
a fat
pig.
"@

你会得到这样的结果:

Bob
went
to town
to buy
a fat
pig.

如果你使用notepad++,它甚至会作为一个字符串块正确地高亮显示。

现在,如果你想让字符串也包含双引号,只需将它们添加进去,就像这样:

$myvar = "Site"
$mystring = @"
<a href="http://somewhere.com/somelocation">
Bob's $myvar
</a>
"@

你会得到这样的结果:

<a href="http://somewhere.com/somelocation">
Bob's Site
</a>

然而,如果你在@-string中像这样使用双引号,notepad++不会意识到这一点,并且会切换语法颜色,就好像它没有被引用或被引用一样,这取决于情况。

更好的是:在任何插入$变量的地方,它都会被解释!(如果你在文本中需要美元符号,你可以用这样的标记来转义:' ' $not-a-variable ')

注意!如果你不把最后一个“@”放在行首,它就会失败。我花了一个小时才弄清楚我不能在我的代码中缩进它!

下面是MSDN关于这个主题的:使用Windows PowerShell " Here- strings "

你可以使用反勾运算符:

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

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

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

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

然后像这样调用你的commandlet:

Get-WmiObject @params

微软文档:关于喷溅

TechNet杂志2011:Windows PowerShell:飞溅

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

如果你有一个函数:

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

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

示例字符串:

"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"

断线的:

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

飞溅法与计算

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

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