New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with xExchMaintenanceMode #209

Closed
berchouinard opened this Issue Jan 12, 2018 · 0 comments

Comments

Projects
None yet
3 participants
@berchouinard

berchouinard commented Jan 12, 2018

Trying to put the server into maintenance mode it throws this error:
Server is not fully in maintenance mode after running through steps to enable maintenance mode
The send configuration apply function did not succeed

Need to add the switch -pauseclusternode on line 207 and 211 in MSFT_xExchMaintenanceMode.psm1

There is 2 Instance of the Set-Servicestate function in TransportMaintenance.psm1

Trying to put the server out of maintenance mode it throws this error:
Method invocation failed because […CimInstance] does not contain a method named ‘Change StartMode’

Need to change the Set-ServiceState
Here what worked for me, not sure if this is the best way to go fix this

function Set-ServiceState
{
    param(
        [Parameter(Mandatory = $true)]
        [string]$ServiceName,
        
        [Parameter(Mandatory = $false)]
        [string]$Server = $env:COMPUTERNAME,

        [Parameter(Mandatory = $false)]
        [ValidateSet("NoChange", "Stopped", "Running", "Paused")]
        [string]$State = "NoChange",
        
        [Parameter(Mandatory = $false)]
        [ValidateSet("NoChange", "Auto", "Automatic", "Manual", "Disabled")]
        [string]$StartMode = "NoChange",
        
        [Parameter(Mandatory = $false)]
        [TimeSpan] $WaitTime = (New-TimeSpan -Minutes 5),
        
        [Parameter(Mandatory = $false)]
        [string] $LoggingStage,
        
        [Parameter(Mandatory = $false)]
        [Switch] $ThrowOnFailure
    )
    
    $service = Get-CimInstance -ClassName win32_service -filter "name = '$ServiceName'" -ComputerName $Server
    
    if(-not $service)
    {
        if($LoggingStage)
        {
            Write-SkippedEvent -Source $Server -Stage $LoggingStage -Reason @{$ServiceName = 'NotFound'}
        }
        
        if($ThrowOnFailure)
        {
            throw "Service $ServiceName not found on $Server."
        }
        return
    }
    
    # Check and change the StartMode if necessary
    if($StartMode -eq "Auto")
    {
        $StartMode = "Automatic"
    }
    
    if(($StartMode -ne "NoChange") -and ($service.StartMode -ne $StartMode))
    {
        #$service.ChangeStartMode($StartMode) | Out-Null
        $service | Invoke-CimMethod -MethodName changeStartMode -Arguments @{startmode=$StartMode} | Out-null
        if($StartMode -eq "Disabled")
        {
            $State = "Stopped"
        }
    }
    
    # Determine if the start/stop/restart action is needed
    if($State -eq "NoChange" -or $service.State -eq $State)
    {
        return
    }
    
    if($LoggingStage)
    {
        $logEntry = New-LogEntry -Source $Server -Stage $LoggingStage
        Write-EventOfEntry -Event Start -Entry $logEntry
    }
    
    switch($State)
    {
        "Stopped" 
        {   
            #$service.StopService() | Out-Null
            $service | Invoke-CimMethod -Name stopService | Out-Null
        }
        
        "Running" 
        {
            if($service.State -eq "Paused")
            {
                #$service.ResumeService() | Out-Null
                $service | Invoke-CimMethod -Name resumeService | Out-Null
            }
            else
            {
                #$service.StartService()| Out-Null
                $service | Invoke-CimMethod -Name startService | Out-Null
            }
        }
        
        "Paused"
        {
            if($service.State -eq "Running")
            {
                #$service.PauseService() | Out-Null
                $service | Invoke-CimMethod -Name pauseService | Out-Null
            }
            else
            {
                # service is stopped, start it up first
                #$service.StartService() | Out-Null
                $service | Invoke-CimMethod -Name startService | Out-Null
                
                if($WaitTime -eq [TimeSpan]::Zero)
                {
                    $startupWaitTime = New-TimeSpan -Minutes 5
                }
                else
                {
                    $startupWaitTime = $WaitTime
                }
                
                Wait-ServiceState `
                    -ServiceName $ServiceName `
                    -Server $Server `
                    -State "Running" `
                    -WaitTime $startupWaitTime `
                    -ThrowOnFailure:$ThrowOnFailure
                
                # pause now
                #$service.PauseService() | Out-Null
                $service | Invoke-CimMethod -Name pauseService | Out-Null
            }
        }
    }
    
    if($WaitTime -gt [TimeSpan]::Zero)
    {
        Wait-ServiceState `
            -ServiceName $ServiceName `
            -Server $Server `
            -State $State `
            -WaitTime $WaitTime `
            -ThrowOnFailure:$ThrowOnFailure
    }
    
    if($LoggingStage)
    {
        Write-EventOfEntry -Event Completed -Entry $logEntry -reason @{'MaxWaitMinutes' = $WaitTime.TotalMinutes}
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment