Arrays 如何检查PowerShell中的数组中是否存在变量?
我正在尝试在powershell中创建邮箱权限审核,并希望从powershell脚本的输出中删除特定帐户,而不是事后手动删除 为了做到这一点,我正在寻找一种方法,将数组的内容与powershell中的单个字符串进行比较 例如,如果我要声明一个数组:Arrays 如何检查PowerShell中的数组中是否存在变量?,arrays,powershell,if-statement,for-loop,compare,Arrays,Powershell,If Statement,For Loop,Compare,我正在尝试在powershell中创建邮箱权限审核,并希望从powershell脚本的输出中删除特定帐户,而不是事后手动删除 为了做到这一点,我正在寻找一种方法,将数组的内容与powershell中的单个字符串进行比较 例如,如果我要声明一个数组: $array = "1", "2", "3", "4" 然后,我想找到一种方法来执行以下操作: $a = "1" $b = "5" if ($a -ne *any string in $array*) {do something} #This s
$array = "1", "2", "3", "4"
然后,我想找到一种方法来执行以下操作:
$a = "1"
$b = "5"
if ($a -ne *any string in $array*) {do something} #This should return false and take no action
if ($b -ne *any string in $array*) {do something} #This should return true and take action
我对如何实现这一点感到茫然,非常感谢您的帮助您有几种不同的选择:
$array = "1", "2", "3", "4"
$a = "1"
$b = "5"
#method 1
if ($a -in $array)
{
Write-Host "'a' is in array'"
}
#method 2
if ($array -contains $a)
{
Write-Host "'a' is in array'"
}
#method 3
if ($array.Contains($a))
{
Write-Host "'a' is in array'"
}
#method 4
$array | where {$_ -eq $a} | select -First 1 | %{Write-Host "'a' is in array'"}
或者
(返回1、3和5)
-in
或-包含<代码>$a='g'$b=@('g','t')$a-在$b中
如果($array-notcontains$a){…}
[Int32[]] $data = @( 1, 2, 3, 4, 5, 6 );
[Int32[]] $remove_list = @( 2, 4, 6 );
$data | Where-Object { $remove_list -notcontains $_ }