简体   繁体   English

Powershell中的“处于”状态

[英]“in” condition in powershell

Is there any "in" condition in Powershell similar to sql. 在Powershell中是否有类似于sql的“处于”状态。 if available please let me know the syntax on how to use it. 如果有的话,请让我知道如何使用它的语法。 My senario is i need to get a list of ids and use it in another query. 我的问题是我需要获取ID列表并在另一个查询中使用它。

Looking for doing below 寻找下面的事情

//Below line i veried returns values more than 1
$desktopGroupIds = Get-BrokerDesktopGroup -AdminAddress 'IPAddressgoeshere' |  select uid

Get-BrokerPrivateDesktop  -AdminAddress 'IPAddressgoeshere' | Where { $desktopGroupIds.contains($_.DesktopGroupUid) } | select DesktopGroupUid

The 2nd query does not return value. 第二个查询不返回值。 i am sure the 2nd query can give results if i pass values one by one. 我敢肯定,如果我一个一个地传递值,第二个查询可以给出结果。 i want to use may be '-in' operator if avaliable or make the above contains work. 我想使用的话可能是“ -in”运算符,或者使上面包含的内容有效。

Thanks, 谢谢,

-in is a reverse -contains operator that's been added in PowerShell 3.0 (so dasari.. has to update to use it). -in是一个反向-contains运算符,已在PowerShell 3.0中添加(因此dasari ..必须更新才能使用它)。 With contains you use $array -contains -element , while in PS3.0 you can use $element -in $array 使用contains可以使用$array -contains -element -contains -element,而在PS3.0中可以使用$element -in $array

You seem to have a typo in $desktopGroupIdS -c contains $_ ..... . 您似乎在$desktopGroupIdS -c contains $_ .....有一个错字$desktopGroupIdS -c contains $_ ..... Try this: 尝试这个:

//Below line i veried returns values more than 1
$desktopGroupIds = @(Get-BrokerDesktopGroup -AdminAddress 'IPAddressgoeshere' |  select uid)

Get-BrokerPrivateDesktop  -AdminAddress 'IPAddressgoeshere' | Where { $desktopGroupIds -contains $_.DesktopGroupUid } | select DesktopGroupUid

Powershell's "in" keyword is normally used for selecting items individually in a collection object. Powershell的“ in”关键字通常用于在集合对象中单独选择项目。

eg: 例如:

foreach($item in $collection)
{
    #...Process some code...
}

If you want to use "Contains" like you have in your solution, you can cast everything in $desktopGroupIds into an array by placing the first line inside @(): 如果要像在解决方案中一样使用“包含”,则可以通过将第一行放在@()内,将$ desktopGroupIds中的所有内容强制转换为数组:

eg 例如

//Below line i veried returns values more than 1
$desktopGroupIds = @(Get-BrokerDesktopGroup -AdminAddress 'IPAddressgoeshere' |  select uid)
Get-BrokerPrivateDesktop  -AdminAddress 'IPAddressgoeshere' | Where { $desktopGroupIds.Contains($_.DesktopGroupUid) } | select DesktopGroupUid

edit 编辑

Another solution you might try is using the -match operator: 您可能尝试的另一种解决方案是使用-match运算符:

eg: 例如:

$desktopGroupIds = @(Get-BrokerDesktopGroup -AdminAddress 'IPAddressgoeshere' |  select uid)
Get-BrokerPrivateDesktop  -AdminAddress 'IPAddressgoeshere' | Where { $desktopGroupIds -match $_.DesktopGroupUid } | select DesktopGroupUid

The update code line to fix the issue is shown below 解决该问题的更新代码行如下所示

Get-BrokerPrivateDesktop  -AdminAddress $regionAddress | Where { $desktopGroupIds  -Contains $_.DesktopGroupUid } | select DesktopGroupUid

Note above the usage of "-Contains" 注意上面“-包含”的用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM