Powershell // foreach

时间:2016-03-17 14:47:53

标签: powershell-v4.0 scom

我正在使用SCOM(SystemCenterOperationsManager)并为计划任务编写了一个小脚本。

它应收集所有NEW ( ResolutionState = 0 )提醒并使用我的通配符规则对其进行过滤。

它会将所有收集的警报的解决状态更改为特定的解决状态。

我的问题:

要获取所有选择的警报的报告,我想生成“文件外” 我应该把它放在哪里?

这是我的代码:

if ($LastHourAlerts)
{
   foreach($Alert in $LastHourAlerts)
   {      
   $newState = $null

   switch -wildcard ($Alert.Name) 
      {
      "Operations Manager failed to start a process" { $newState = 255 } 
      "Operations Manager failed to check the state of a service" { $newState = 255 }
      "Operations Manager failed to run a WMI query" { $newstate =255 }
      "Interface Utilization Alert" { $newState = 15 }
      "Veeam VMware: Datastore Provisioning Analysis" { $newState = 17 }
      "Veeam VMware Collector: VMware connection data processing errors" { $newState = 17 }
      "Veeam VMware Collector: VMware connection is unavailable" { $newState = 17 }
      "Veeam VMware: Virtual Machine Memory Usage Analysis" { $newState = 17 }
      "Veeam VMware: Virtual Machine CPU Usage Analysis" { $newState = 17 }
      "Veeam VMware: Virtual Machine Memory Ballooning and Swapping Analysis" { $newState = 17 }
      "Veeam VMware: Virtual Machine Storage Latency Analysis" { $newState = 17 }
      "Veeam VMware: Virtual Machine Consolidation Needed status Alarm" { $newState = 17 }
      "Veeam VMware: Maintenance Mode Tracker" { $newState = 17 }
      "Veeam VMware: vSphere Host Storage Commands Failed Analysis" { $newState = 17 }
      "Veeam VMware: vSphere Host Network Packet Loss Analysis" { $newState = 17 }
      "Veeam VMware: vSphere Host Memory Swapping Analysis" { $newState = 17 }
      "Veeam VMware: vSphere Host Memory Usage Analysis" { $newState = 17 }
      "Veeam VMware: vSphere Host Kernel Latency Analysis" { $newState = 17 }
      "Veeam VMware: vSphere Cluster Memory Overcommit Analysis" { $newState = 17 }
      "Veeam VMware: VM customization failed" { $newState = 17 }

      }


      if($newState -ne $null)
         {
         $Alert.ResolutionState = $newState
         $Alert.Update(“Resolution State changed automatically by Automation”)              
         }
     }    
}

我在if循环结束时发现了类似的东西:

$Alert.Name | Out-File ("$path\" + "Report.csv") -Append

虽然它采用了名称,但只在所有已处理的警报数中写入第一个警报对象的名称。例如:

5种不同的警报:

Operations Manager failed to start a process
Interface Utilization Alert
Veeam VMware: VM customization failed
Veeam VMware: Virtual Machine CPU Usage Analysis
Veeam VMware Collector: VMware connection is unavailable

这就是.csv:

Operations Manager failed to start a process
Operations Manager failed to start a process
Operations Manager failed to start a process
Operations Manager failed to start a process
Operations Manager failed to start a process

有更好的方法来获取数据吗?另外,我不仅希望拥有$Alert.Name,而且还希望拥有$Alert.Name, $Alert.TimeRaised, $ResolutionState AngularJS

1 个答案:

答案 0 :(得分:0)

在你的循环结束时加上这样的东西:

if ($Alert.ResolutionState -eq 0) {
  $Alert.Name | Out-File 'C:\path\to\your.txt' -Append
}