无法远程设置SCOM维护模式(第2部分)

时间:2016-09-23 13:52:26

标签: powershell scom

跟进此问题(Cannot set SCOM maintenance mode remotely

我们正试图远程将服务器置于维护模式。我正在使用由Tom Schumacher(https://gallery.technet.microsoft.com/scriptcenter/Put-server-in-Maintenance-0a23e1fe)创建的SCOM函数

我正在使用PSSession尝试从SCOM服务器以外的服务器将服务器置于维护模式:

. C:\Scripts\Start-serverScommaintenance.ps1
$s = New-PSSession -ComputerName scomserver
Invoke-Command -Session $s -Scriptblock {Import-Module OperationsManager}
Invoke-Command -Session $s -Scriptblock ${function:Start-
serverScommaintenance} 
Invoke-Command -Session $s -Scriptblock {Start-serverScommaintenance -
servername testserver -message "test" -maintmodeinMinutes '6'}

当我进入该功能时,我收到以下错误:

无法验证参数' DisplayName'的参数。参数为null或空。提供非null或空的参数,然后再次尝试该命令。     + CategoryInfo:InvalidData:(:) [Get-SCOMClassInstance],ParameterBindingValidationException     + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.SystemCenter.OperationsManagerV10.Commands.GetSCClassInstanceCommand     + PSComputerName:scomserver

术语' Start-serverScommaintenance'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或路径是否正确 包含,验证路径是否正确,然后重试。     + CategoryInfo:ObjectNotFound:(Start-serverScommaintenance:String)[],CommandNotFoundException     + FullyQualifiedErrorId:CommandNotFoundException     + PSComputerName:scomserver

以下是用于参考的函数的副本:

function Start-ServerScommaintenance
{
param([string] $servername = 'yourServerGoeshere',
   [string]$message = "Putting: $servername into Maintenance Mode via     automation",
   [int]$maintModeinMinutes = '60')

$funcName = 'func - Start-ServerScommaintenance:'
if(get-command -Name 'Get-SCOMClassInstance')
{
    $server = (Get-SCOMClassInstance -DisplayName "$servername*") | select -first 1 | select -ExpandProperty Displayname
    $scommanagementServers = (Get-SCOMManagementServer).displayName
    if($scommanagementServers -ccontains $server)
    {
        Write-Warning "$funcname contains a Management Server $server.. You cannot put a management server in Maintenance Mode!!!"
    }
    else
    {
        $time = ((get-date).AddMinutes($maintModeinMinutes))
        $serverClassIds = Get-SCOMClassInstance -DisplayName $server
        foreach($classid in $serverClassIds)
        {
            $server1 = Get-SCOMClassInstance -id ($classid.id) | Where-Object{$_.DisplayName -match $server}
            write-host "$funcName putting " ($server1.id) ' in maintenance Mode Servername -->' ($Server1.DisplayName)
            if(!(Get-SCOMMaintenanceMode -Instance $classid))
            {
                Start-SCOMMaintenanceMode -Instance $server1 -EndTime $time -reason PlannedOther -Comment $message
            }
            else
            { Write-host "$funcname " $classid.id " has already been placed in Maintenance Mode"}
        }
    }
}
    else
    { Write-host "$funcname doesn't have the Operationsmanager module imported for this session"}
}

function Stop-ServerScommaintenance
{
param([string] $servername = 'yourServerGoeshere',
   [string]$message = "Removing Maintenance Mode from: $servername via     automation"
   )
$funcName = 'func - Stop-ServerScommaintenance:'
if(get-command -Name 'Get-SCOMClassInstance')
{
    $server = (Get-SCOMClassInstance -DisplayName "$servername*") | select -    first 1 | select -ExpandProperty Displayname
    $scommanagementServers = (Get-SCOMManagementServer).displayName
    if($scommanagementServers -ccontains $server)
    {
        Write-Warning "$funcname contains a Management Server $server.. You     cannot put a management server in Maintenance Mode!!!"
    }
    else
    {
        $serverClassIds = Get-SCOMClassInstance -DisplayName $server
        foreach($classid in $serverClassIds)
        {
            $server1 = Get-SCOMClassInstance -id ($classid.id) | Where-    Object{$_.DisplayName -match $server}
            write-host "$funcName removing " ($server1.id) ' in maintenance     Mode Servername -->' ($Server1.DisplayName)
            $result = (Get-SCOMClassInstance -id ($classid.id)|Where-Object{$_.Displayname -like $servername}).StopMaintenanceMode((get-date).ToUniversalTime())
        }
    }
}
else
{ Write-host "$funcname doesn't have the Operationsmanager module imported for this session"}
}
#Start-serverScommaintenance -servername Server1 -message "my message" -maintmodeinMinutes '30' 
#Stop-ServerScommaintenance -servername Server1 -message "my message"  

1 个答案:

答案 0 :(得分:0)

我相信你需要在使用函数时传递带有参数列表的东西,而不是用args再次调用函数:

Invoke-Command -Session $s -Scriptblock ${function:Start-serverScommaintenance} -argumentlist #argshere