get-operationsmanagercommand
#定义RMS服务器名称(SCOM管理控制台所在服务器名称)
#Connect to the RMS server and initialize the command shell
$rmsServerName=”scomw.testj.com”
#向当前会话添加scom管理单元
add-pssnapin “Microsoft.EnterpriseManagement.OperationsManager.Client”
#将当前工作会话设置为SCOM
Set-Location “OperationsManagerMonitoring::”
#测试管理组连接是否正常,管理组可以更改
$mgConn = New-ManagementGroupConnection -connectionString:$rmsServerName
if($mgConn -eq $null)
{
[String]::Format(“Failed to connect to RMS on ‘{0}’”,$rmsServerName)
return;
}
#设置当前工作会话到scom管理控制台服务器
Set-Location $rmsServerName
#######通过路径导航到每台计算机下对其进行设置维护模式#############################
#获取监视对象
get-monitoringobject
#导航到所需要的路径下
cd Microsoft.SystemCenter.AllComputersGroupwin08r2.testj.com
#对当前路径新建维护模式
$time = [DateTime]::Now
New-MaintenanceWindow -starttime: ($time) -endTime: ($time.AddDays(1)) -reas
on: "ApplicationInstallation" -comment: "Testing DateTime.Now"
##################################################################################
##################################################################################
##################可以单独对每台计算机设置维护模式#################
#定义需要设置维护模式的计算机名称
$servername_maintenance = "win08r2.testj.com"
$time = [DateTime]::Now
#通过agent获取Monitoringobject
$agent = get-agent|where {$_.name -eq $servername_maintenance}
$server = $agent.hostcomputer
#也可以对组直接设置
#$MonitoringClassCG = get-monitoringclass | where {$_.DisplayName -eq $groupName}
#$MonitoringGUID = get-monitoringobject $MonitoringClassCG.Id
#设置具体结束时间
New-MaintenanceWindow -Monitoringobject $server -Comment "maintenance test" -Starttime $time -Endtime "3/2/2013 12:00"
#设置维护模式时长
New-MaintenanceWindow -Monitoringobject $server -Comment "maintenance test" -Starttime $time -Endtime $time.addminutes(30)
#获取计算机的维护模式状态,两种方法均可
get-maintenancewindow -monitoringobject $server |get-member
$? #如果未处于维护模式,则会报错
#$server|get-maintenancewindow
#结束维护模式
$time1=get-date
取消维护模式,两种方法均可
Set-MaintenanceWindow -Monitoringobject $server -Endtime $time1
#$server|Set-MaintenanceWindow -Endtime $time1