PowerShell如何使用-和Where-Object

时间:2019-05-24 17:25:45

标签: powershell

我想使用两个条件来过滤命令结果。这是我的命令

 $list=Get-PnpDevice | Sort-Object -Property Name | Where-Object -Property FriendlyName -like '*touch screen*' | ft Name, InstanceId -AutoSize

下一个过滤器是

and

它们都可以单独工作,但是我想使用-AND命令加入它们。我尝试将 Get-PnpDevice | Sort-Object -Property Name | Where-Object{ ( ConfigurationFlags -NotLike '*DISABLED*') -and ( FriendlyName -like '*touch screen*' ) }| ft Name, InstanceId -AutoSize 用作以下命令,但它不断出现错误

// Handler
public Optional<Response> handle(HandlerInput input) {
   //I tried with url.openStream () but it doesn't work
}

// Servlet
 public class AlexaServlet extends HttpServlet {


    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        out.println("Hello Alexa");
        out.close();
    }

2 个答案:

答案 0 :(得分:1)

只需使用$_中的Where-Object automatic variable来引用属性名称:

Get-PnpDevice | Sort-Object -Property Name | Where-Object{
      ( $_.ConfigurationFlags -NotLike '*DISABLED*') -and 
      ( $_.FriendlyName -like '*touch screen*' ) }| ft Name, InstanceId -AutoSize

答案 1 :(得分:1)

您可以将'Where'子句通过管道连接在一起...语法更简单,更易于阅读

Get-PnpDevice | Sort-Object -Property Name | Where ConfigurationFlags -NotLike '*DISABLED*' | Where FriendlyName -like '*touch screen*' | ft Name, InstanceId -AutoSize