Powershell Multiline Mode

引自:http://www.windowsitpro.com/article/windows-powershell/how-to-handle-long-powershell-statements

Extracted from :http://www.windowsitpro.com/article/windows-powershell/how-to-handle-long-powershell-statements

文中讲述的是如何编辑多行powershell script,困扰我的一个现象是如何结束多行的powershell script,如今找到答案了,就是在完成你编辑的时候,按2次Enter键。

意外的收获是,可以使用 ·(也就是~键的另外一个字符)来提示powershell还有下行需要处理,原本这个符号应该是转义符号。

As a PowerShell statement grows larger, it’s not always practical to enter it on a single line in the PowerShell console window. You can enter a long statement on several lines, but you must take into account how PowerShell treats new lines. When PowerShell determines that a line is incomplete, it continues to the next line when processing the statement. For example, when the first line in a statement ends with the pipe operator, as in

Get-Service |
where {$_.status -eq ‘running’} |
select displayname

PowerShell knows that the statement continues to the next line. This statement returns results similar to those shown in Figure 3. Notice the multiline prompt (>>) that precedes each line after the first line. When PowerShell expects a line to continue to a second line, it uses a multiline prompt for that line. You then type the next line of code at that prompt. Once PowerShell enters this multiline mode, it will continue in this mode and always prompt you with the multiline prompt. When you finish entering the last line, press Enter a second time to execute the command and return to the normal prompt.

Now suppose you break the statement before the pipe operator:

 Get-Service
  | where {$_.status -eq ‘running’}
  | select displayname

PowerShell now interprets the first line as complete and processes it as an entire statement. PowerShell then tries to process the second line, which results in the error message: An empty pipe element is not permitted.

You can remedy this situation by adding a back tick (`) to the end of the lines:

 Get-Service `
  | where {$_.status -eq ‘running’} `
  | select displayname

The back tick tells PowerShell that the statement continues to the next line. The statement now returns the same information shown in Figure 3.

PowerShell processes any line that it thinks is a complete statement. In other words, it automatically terminates a statement when it reaches a new line unless it thinks that the statement continues. However, you can also manually terminate a statement by adding a semi-colon (;) at the end:

 Get-Service |
  where {$_.status -eq ‘running’} |
  select displayname;

This statement returns the same results as those shown in Figure 3.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值