zoukankan      html  css  js  c++  java
  • [转]How to Handle Long PowerShell Statements(在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.

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

  • 相关阅读:
    JAVA的学习日记15
    JAVA的学习日记14
    CIRD主站与DOPAS构建笔记#1
    信仰之题——Codeforces Round 259(附题面完整翻译)
    平面最近点对问题
    BZOJ4552 [Tjoi2016&Heoi2016]排序
    BZOJ1001 [Beijing2006]狼抓兔子
    (二)k8s编写资源清单
    linux常用搜索工具find/whereis/locate
    解决centos7 的/etc/rc.local不会开机执行
  • 原文地址:https://www.cnblogs.com/kxlf/p/2278930.html
Copyright © 2011-2022 走看看