在我的公司中,我们使用powershell脚本将对象置于维护模式。问题是它突然停止工作 - 没有错误,但脚本什么都不做
维护间隔由XML文件定义
例如
<MMWindow>
<Name>MMG 129OP SVC WEBSOA</Name> ---group containing objects
<Schedule>Mon 19:30-Mon 23:00</Schedule> ---monday 19:30 to 23:00
</MMWindow>
这是脚本
$ErrorActionPreference = "stop"
$oAPI = new-object -comObject "MOM.ScriptAPI"
$Error.Clear()
If ($Debug -ne "true")
{
$Debug = [bool]$false
}
else
{
$Debug = [bool]$true
}
$DateTime = Get-Date
$Interval = $IntervalSeconds / 60
If ($Debug)
{
$oAPI.LogScriptEvent("MaintenanceWindows.ps1",800,4,"The script 'MaintenanceWindows.ps1' is starting at $DateTime.")
}
$setupKey = Get-Item -Path "HKLM:\Software\Microsoft\Microsoft Operations Manager\3.0\Setup"
$installDirectory = $setupKey.GetValue("InstallDirectory") | Split-Path
$psmPath = $installdirectory + "\Powershell\OperationsManager\OperationsManager.psm1"
Import-Module $psmPath
#Import-Module "C:\Program Files\System Center 2012\Operations Manager\Console\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.dll"
New-SCOMManagementGroupConnection -ComputerName 123.ru
$XmlPath = "C:\Monitoring\Maintenance\MaintenanceWindows.xml"
[xml]$MMContent = Get-Content $XmlPath
If ($Debug -and $Error)
{
$oAPI.LogScriptEvent("MaintenanceWindows.ps1",802,4,"Error: $Error. Time: $DateTime.")
}
if ($MMContent.HasChildNodes)
{
foreach ($MMWindow in $MMContent.MMWindows.MMWindow)
{
$ScheduledDays = ($MMWindow.Schedule).Split(",")
foreach ($ScheduledDay in $ScheduledDays)
{
$StartDay = ($ScheduledDay.Split("-").GetValue(0)).Split(" ").GetValue(0)
Switch ($StartDay)
{
"Mon" {$EndDay = "Monday"
$EndDayCount = 1}
"Tue" {$EndDay = "Tuesday"
$EndDayCount = 2}
"Wed" {$EndDay = "Wednesday"
$EndDayCount = 3}
"Thu" {$EndDay = "Thursday"
$EndDayCount = 4}
"Fri" {$EndDay = "Friday"
$EndDayCount = 5}
"Sa" {$EndDay = "Saturday"
$EndDayCount = 6}
"Su" {$EndDay = "Sunday"
$EndDayCount = 7}
}
$EndDay = ($ScheduledDay.Split("-").GetValue(1)).Split(" ").GetValue(0)
Switch ($EndDay)
{
"Mon" {$EndDay = "Monday"
$EndDayCount = 1}
"Tue" {$EndDay = "Tuesday"
$EndDayCount = 2}
"Wed" {$EndDay = "Wednesday"
$EndDayCount = 3}
"Thu" {$EndDay = "Thursday"
$EndDayCount = 4}
"Fri" {$EndDay = "Friday"
$EndDayCount = 5}
"Sa" {$EndDay = "Saturday"
$EndDayCount = 6}
"Su" {$EndDay = "Sunday"
$EndDayCount = 7}
}
[DateTime]$StartTime = ($ScheduledDay.Split("-").GetValue(0)).Split(" ").GetValue(1)
if ($EndDayCount -lt $StartDayCount)
{
[DateTime]$EndTime = ([DateTime]($ScheduledDay.Split("-").GetValue(1)).Split(" ").GetValue(1)).AddDays(7 - ($StartDayCount - $EndDayCount))
}
else
{
[DateTime]$EndTime = ([DateTime]($ScheduledDay.Split("-").GetValue(1)).Split(" ").GetValue(1)).AddDays($EndDayCount - $StartDayCount)
}
If ($StartDay -eq (Get-Date).DayOfWeek -and (Get-Date) -ge $StartTime.AddMinutes(-($Interval + 10)) -and (Get-Date) -lt $StartTime.AddMinutes($Interval + 10))
{
$Group = Get-SCOMGroup -DisplayName $MMWindow.Name
If ($Debug -and $Error)
{
$oAPI.LogScriptEvent("MaintenanceWindows.ps1",803,4,"Error: $Error. Time: $DateTime.")
}
If($Group)
{
$GroupMembers = $Group.GetRelatedMonitoringObjects()
If($GroupMembers.Count -gt 0)
{
Foreach ($Instance in $GroupMembers)
{
if (!$Instance.InMaintenanceMode)
{
Start-SCOMMaintenanceMode -Instance $Instance -EndTime $EndTime.ToUniversalTime() -Reason "PlannedOther" -Comment "Planned Maintenance Mode by Script"
$oAPI.LogScriptEvent("MaintenanceWindows.ps1",805,4, "Start MM for object: " + $Instance.DisplayName + ". End MM: " + $EndTime)
If ($Debug -and $Error)
{
$oAPI.LogScriptEvent("MaintenanceWindows.ps1",804,4,"Error: $Error. Time: $DateTime.")
}
}
}
}
}
}
}
}
}
If ($Debug)
{
$oAPI.LogScriptEvent("MaintenanceWindows.ps1",801,4, "The script 'MaintenanceWindows.ps1' is finished.")
}
任何想法?
答案 0 :(得分:0)
正如安斯加所说,通过某种调试解决问题的好方法。
但是你问了想法,所以这里有一些: 1.我认为剧本“无所事”(假设它一直运行)的一种可能方式是$ GroupMembers为空。 2. $ GroupMembers依赖于$ Group评估为$ true,因此如果$ Group为$ null,则不会发生任何事情。 3.除非以下评估$ true:
,否则不会分配$ Group$StartDay -eq (Get-Date).DayOfWeek -and
(Get-Date) -ge $StartTime.AddMinutes(-($Interval + 10)) -and
(Get-Date) -lt $StartTime.AddMinutes($Interval + 10))
该表达式为$ true取决于$ Interval的值。 $ Interval基于$ IntervalSeconds定义 - 其定义未在OP中显示。因此,StackOverflow响应者无法帮助您使用该代码路径。
其他一些可能的失败是Get-SCOMGroup返回$ null或GetRelatedMonitoringObjects返回$ null。
既然最初的问题要求提出想法,而且我给了你几个看似合理的想法,我会说我回答了这个问题。
答案 1 :(得分:0)
几年前,我的公司为Microsoft编写了这些cmdlet。这个问题响起了SCOM爱好者朋友的钟声以及经常改变维护模式的必要性。我查了一下,我相信命令Start-SCOMMaintenanceMode和Update-SCOMMaintenanceMode可以解决这个问题(并为您节省大量代码)。
如果不是这样,请告诉我。