使用匹配运算符时出现多个异常
More than one exception when using the match operator
我有这部分代码:
$exception = "VM"
ForEach($item in $list) {
IF ($item -match $exception) { $invalidlist += $item }
ELSE { $validlist += $item }
}
当仅将 1 项分配给变量 $exception
时,它作为意图工作。
问题是,我需要变量包含不止一项,例如:$exception = "VM","TM","TMP"
如何在 $exception
中搜索任何项目的匹配项?
提前致谢。
编辑:$list
是通过使用创建的:
$list = Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 548.00:00:00
关于您更新的评论,您的 $list
是 AD 对象数组:
假设您关心计算机名称,您应该将名称 属性 添加到 $item
-> $item.name
,如下例所示:
*如果您只关心 return 计算机名称而不是完整对象,请将 $item.name 也添加到无效变量中,例如$invalidlist += $item.name
$invalidlist = @()
$validlist = @()
$exception = "VM","TM","TMP"
$list = Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 548.00:00:00
ForEach($item in $list) {
## if the item in $exception are equal to the $item name you can use the contains operator
## otherwise you can use -match
IF ($exception -contains $item.name) { $invalidlist += $item }
ELSE { $validlist += $item }
}
我有这部分代码:
$exception = "VM"
ForEach($item in $list) {
IF ($item -match $exception) { $invalidlist += $item }
ELSE { $validlist += $item }
}
当仅将 1 项分配给变量 $exception
时,它作为意图工作。
问题是,我需要变量包含不止一项,例如:$exception = "VM","TM","TMP"
如何在 $exception
中搜索任何项目的匹配项?
提前致谢。
编辑:$list
是通过使用创建的:
$list = Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 548.00:00:00
关于您更新的评论,您的 $list
是 AD 对象数组:
假设您关心计算机名称,您应该将名称 属性 添加到 $item
-> $item.name
,如下例所示:
*如果您只关心 return 计算机名称而不是完整对象,请将 $item.name 也添加到无效变量中,例如$invalidlist += $item.name
$invalidlist = @()
$validlist = @()
$exception = "VM","TM","TMP"
$list = Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 548.00:00:00
ForEach($item in $list) {
## if the item in $exception are equal to the $item name you can use the contains operator
## otherwise you can use -match
IF ($exception -contains $item.name) { $invalidlist += $item }
ELSE { $validlist += $item }
}