如何在PowerShell中将多个命令分割为多个命令

时间:2010-04-09 14:11:01

标签: powershell

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

8 个答案:

答案 0 :(得分:240)

尾随反引号字符,即

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

答案 1 :(得分:53)

清除参数传递的另一种方法是splatting

将参数和值定义为这样的哈希表:

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

然后像这样调用你的命令行开关:

Get-WmiObject @params

Microsoft文档:About Splatting

TechNet Magazine 2011:Windows PowerShell: Splatting

Looks like it works with Powershell 2.0 and up

答案 2 :(得分:36)

啊,如果你有一个非常长的字符串,你想要分解,比如HTML,你可以通过在@的外侧"的每一侧放置$mystring = @" Bob went to town to buy a fat pig. "@ 来实现这一点 - 就像这样:

Bob
went
to town
to buy
a fat
pig.

你得到了这个:

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

如果您使用Notepad++,它甚至会正确地突出显示为字符串块。

现在,如果您希望该字符串包含双引号,只需添加它们,如下所示:

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

你会得到这个:

"@

但是,如果你在@ -string中使用双引号,Notepad ++没有意识到这一点,并且会将语法着色切换为没有引用或引用,具体取决于具体情况。

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

注意!如果您没有将最终的{{1}}放在行的最开头,它将会失败。我花了一个小时才发现我无法在我的代码中缩进它!

以下是关于此主题的MSDN Using Windows PowerShell “Here-Strings”

答案 3 :(得分:15)

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

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

答案 4 :(得分:9)

如果你有一个功能:

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

答案 5 :(得分:1)

带有计算的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

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

答案 6 :(得分:0)

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

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

答案 7 :(得分:0)

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

示例字符串:

"stackoverflow stackoverflow stackoverflow stackoverflow stackoverflow"

断行:

"stackoverflow stackoverflow $(
)stackoverflow stack$(
)overflow stackoverflow"
相关问题