0
votes

Following up from this question (Cannot set SCOM maintenance mode remotely)

We're trying to put servers in maintenance mode remotely. I'm using SCOM functions created by Tom Schumacher (https://gallery.technet.microsoft.com/scriptcenter/Put-server-in-Maintenance-0a23e1fe)

I'm using PSSession to try and put the server in maintenance mode from a server other than the SCOM server:

. 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'}

When I get to the function, I get the below errors:

Cannot validate argument on parameter 'DisplayName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. + CategoryInfo : InvalidData: (:) [Get-SCOMClassInstance], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SystemCenter.OperationsManagerV10.Commands.GetSCClassInstanceCommand + PSComputerName : scomserver

The term 'Start-serverScommaintenance' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (Start-serverScommaintenance:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException + PSComputerName : scomserver

Here is a copy of the function being used for reference:

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

1 Answers

0
votes

I believe that you need pass things with argumentlist in the when you use function, rather than call the function again with the args:

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