小编coo*_*lRV的帖子

使用powershell检查字符串列表中是否存在字符串

我的环境中有 Powershell 版本 3,4 和 5。当我编写下面的代码时,它不断给我错误,尽管 $CompatiableOS 包含 $OSverions 的输出。

   [string] $CompatiableOS = '2016','2012','2008'
   $OSVersion=[regex]::Matches(((Get-WmiObject -class Win32_OperatingSystem).caption), "([0-9]{4})")

   if ( $CompatiableOS -contains  $OSVersion)
   {
      return $TRUE
   }
   else
   {
      return $FALSE
   }
Run Code Online (Sandbox Code Playgroud)

但是当我将上面的代码更改为下面时,它起作用了。可能是什么问题?

 [string] $CompatiableOS = '2016','2012','2008'
 $OSVersion=[regex]::Matches(((Get-WmiObject -class Win32_OperatingSystem).caption), "([0-9]{4})")

 if ( $CompatiableOS.contains($OSVersion))
 {
    return $TRUE
 }
 else
 {
      return $FALSE
 }
Run Code Online (Sandbox Code Playgroud)

powershell

6
推荐指数
2
解决办法
3万
查看次数

标签 统计

powershell ×1