A N

March 7, 2016

SCOM – Show and Start MaintenanceMode

Filed under: Tips — donrsh @ 2:49 pm
#REQUIRES -Version 4.0
<# .NOTES
	===========================================================================
	Created on:   	2016-03-07
	Created by:   	Arash Nabi - arash@nabi.nu
	===========================================================================
#>

<#
	.SYNOPSIS
		Invoke a command to SCOM Server
	
	.DESCRIPTION
		The Invoke-SCOM function uses SCOM Module to connect to SCOM Server and Show if a server is in maintenance mode,
		put a server in maintenance mode, show if a Web Watcher is in maintenance mode or put a Web Watcher in maintenance mode.
	
	.PARAMETER ShowInMaintenanceModeServer
		Selecting this parameter will show you Maintenance mode of a server or servers.
	
	.PARAMETER StartMaintenanceModeServer
		Selecting this parameter will start Maintenance mode of a server or serves, if the server is in maintenance mode
		it will update it with the new entries you entered.
	
	.PARAMETER ComputerName
		ComputerName should be entered either with .hm.com or without. The script is smart enough to check and add domain
		prefix if doesnĀ“t have.
	
	.PARAMETER Minutes
		Minutes should be entred as integer, minimum 10 minutes.
	
	.PARAMETER Reason
		The reason parameter is predefined values. Choose the one that fits.
	
	.PARAMETER Comment
		The comment parameter should be entered in quotation mark, otherwise you will get error.
	
	
	.EXAMPLE
		Invoke-SCOM -ShowInMaintenanceModeServer -ComputerName SERVER001,SERVER002
		
		Show if a server(s) is in Maintenance mode or not.
		
		HealthState     InMaintenanceMode  DisplayName
		-----------     -----------------  -----------
		Error                 False        SERVER001.DOMAIN.COM
		Success               False        SERVER002.DOMAIN.COM
	
	.EXAMPLE
		Invoke-SCOM -StartMaintenanceModeServer -ComputerName SERVER001,SERVER001 -Minutes 10 -Reason PlannedOther -Comment "Some text comments here..."
		Put a server(s) in Maintenance mode. If the server is in maintenance mode, the records will be updated.
		
		VERBOSE: SERVER001.DOMAIN.COM is not in maintenance mode. Starting maintenance mode...
		
		
		StartTime        : 2016-03-07 12:15:42
		ScheduledEndTime : 2016-03-07 12:25:32
		Reason           : PlannedOther
		Comments         : Some text comments here...
		User             : DOMAIN\USERNAME
		
		VERBOSE: SERVER002.DOMAIN.COM is not in maintenance mode. Starting maintenance mode...
		StartTime        : 2016-03-07 12:15:42
		ScheduledEndTime : 2016-03-07 12:25:32
		Reason           : PlannedOther
		Comments         : Some text comments here...
		User             : DOMAIN\USERNAME
	
	.NOTES
		Additional information about the function.
#>
function Invoke-SCOM
{
	[CmdletBinding(DefaultParameterSetName = 'MaintenanceModeServer')]
	[OutputType([int])]
	param
	(
		[Parameter(ParameterSetName = 'ShowOnlyMaintenanceModeServers',
				   Mandatory = $true,
				   ValueFromPipeline = $true,
				   ValueFromPipelineByPropertyName = $true,
				   Position = 0)]
		[switch]$ShowInMaintenanceModeServer,
		[Parameter(ParameterSetName = 'MaintenanceModeServer',
				   Mandatory = $false,
				   Position = 0)]
		[switch]$StartMaintenanceModeServer,
		[Parameter(ParameterSetName = 'MaintenanceModeServer',
				   Mandatory = $true)]
		[Parameter(ParameterSetName = 'ShowOnlyMaintenanceModeServers',
				   Mandatory = $true)]
		$ComputerName,
		[Parameter(ParameterSetName = 'MaintenanceModeServer')]
		[int]$Minutes,
		[Parameter(ParameterSetName = 'MaintenanceModeServer')]
		[ValidateSet('ApplicationInstallation', 'ApplicationUnresponsive', 'ApplicationUnstable', 'LossOfNetworkConnectivity', 'PlannedApplicationMaintenance', 'PlannedHardwareInstallation', 'PlannedHardwareMaintenance', 'PlannedOperatingSystemReconfiguration', 'PlannedOther', 'SecurityIssue', 'UnplannedApplicationMaintenance', 'UnplannedHardwareInstallation', 'UnplannedHardwareMaintenance', 'UnplannedOperatingSystemReconfiguration', 'UnplannedOther')]
		[string]$Reason,
		[Parameter(ParameterSetName = 'MaintenanceModeServer',
				   Mandatory = $true)]
		[string]$Comment
	)
	
	Begin
	{
		$VerbosePreference = 'Continue'
		$SCOMServer = "ScomServer"
		$GetModule = Get-Module -Name 'OperationsManager'
		
		if (-not $GetModule)
		{
			$ScomModule = Get-Module -ListAvailable | Where-Object { $_.Name -like 'OperationsManager' }
			if ($ScomModule)
			{
				Write-Verbose -Message "OperationsManager exist in $env:COMPUTERNAME"
				Import-Module -Name 'OperationsManager' -Verbose
				New-SCOMManagementGroupConnection -ComputerName $SCOMServer -Verbose
			}
			else
			{
				[void][System.Windows.Forms.MessageBox]::Show("OperationsManager module does not exist", "Warning!")
				Break
			}
			
		}
		
		
	}
	Process
	{
		try
		{
			$end = ((get-date).AddMinutes($minutes))
			
			# Show All Servers Maintenance Mode
			if ($ShowInMaintenanceModeServer)
			{
				$instanceclass = Get-SCOMClass -Name 'Microsoft.Windows.Computer' -ErrorAction SilentlyContinue | Get-SCOMClassInstance
				foreach ($Computer in $ComputerName)
				{
					if (-not ($Computer -match ".domain.com"))
					{
						$computer = $($computer + ".domain.com")
					}
					$instance = $instanceclass | Where-Object { $_.DisplayName -match $Computer }
					$isMaint = $instance | Get-SCOMMaintenanceMode -ErrorAction SilentlyContinue | Select-Object -Property StartTime, ScheduledEndTime, Reason, Comments, User
					if ($isMaint)
					{
						Write-Host " "
						Write-Verbose -Message "$Computer"
						$isMaint
					}
					else
					{
						$instance
					}
					
				}
				
			}
			
			
			# Start Server Maintenance
			if ($StartMaintenanceModeServer)
			{
				$instanceclass = Get-SCOMClass -Name 'Microsoft.Windows.Computer' -ErrorAction SilentlyContinue | Get-SCOMClassInstance
				foreach ($Computer in $ComputerName)
				{
					if (-not ($Computer -match ".domain.com"))
					{
						$computer = $($computer + ".domain.com")
					}
					$instance = $instanceclass | Where-Object { $_.DisplayName -match $Computer }
					if ($instance.InMaintenanceMode -eq $false)
					{
						Write-Verbose -Message "$computer is not in maintenance mode. Starting maintenance mode..."
						Start-SCOMMaintenanceMode -Instance $instance -EndTime $end -Reason $Reason -Comment $Comment -ErrorAction SilentlyContinue
					}
					else
					{
						Write-Warning "$Computer is in maintenance mode. Updating with new entries you entered... "
						$instance | Get-SCOMMaintenanceMode -ErrorAction SilentlyContinue | Set-SCOMMaintenanceMode -EndTime $end -Reason $Reason -Comment $Comment -ErrorAction SilentlyContinue
						
					}
					$instance | Get-SCOMMaintenanceMode -ErrorAction SilentlyContinue | Select-Object -Property StartTime, ScheduledEndTime, Reason, Comments, User
				}
				
				
			}
			
			
			
		}
		
		catch [System.Net.WebException], [System.Exception]
		{
			$errorMessage = $_.Exception.Message
			Write-Warning  $errorMessage
		}
		finally
		{
			
		}
		
		
	}
	End
	{
	}
}


Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: