Thanks for your help. I didn't see your response until just now, but I have been playing around with this all since my last response. I got it to work. I wish I could pinpoint what exactly I changed to get it to work, but I
feel like I've been changing so much stuff that I'm not sure any more.
Just in case anyone finds it useful, here is the code.
[CmdletBinding(SupportsShouldProcess=$true)]
param
(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the server you want to put in Maintenance Mode?')]
[Alias("Server")]
[string[]]$ServerName,
[Parameter(Mandatory=$True,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Specifies the time the maintenance will end. The minimum amount of time a resource can be in maintenance mode is 5 minutes. This is a required parameter. Format is 1/29/2014 8:59:26 AM')]
[Datetime]$end,
[Parameter(Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='PlannedOther, UnplannedOther, PlannedHardwareMaintenance, UnplannedHardwareMaintenance, PlannedHardwareInstallation, UnplannedHardwareInstallation, PlannedOperatingSystemReconfiguration, UnplannedOperatingSystemReconfiguration, PlannedApplicationMaintenance, ApplicationInstallation, ApplicationUnresponsive, ApplicationUnstable, SecurityIssue, LossOfNetworkConnectivity')]
[string]$Reason,
[Parameter(Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Allows you to type a comment about the maintenance activity.')]
[string]$Comment,
[switch]$EventLog
)
set-strictmode -version latest
#$start=Get-Date
#$currentlog = $start.ToString()
Write-Verbose "Starting $($myinvocation.mycommand)"
Write-Verbose "Ready to put ServerName $ServerName in Maintenance Mode"
Function Start-SCOMMaintenanceModeForServer
{
[CmdletBinding(SupportsShouldProcess=$true)]
param
(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the server you want to put in Maintenance Mode?')]
[Alias("Server")]
[string[]]$ServerName,
[Parameter(Mandatory=$True,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Specifies the time the maintenance will end. The minimum amount of time a resource can be in maintenance mode is 5 minutes. This is a required parameter. Format is 1/29/2014 8:59:26 AM')]
[Datetime]$end,
[Parameter(Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='PlannedOther, UnplannedOther, PlannedHardwareMaintenance, UnplannedHardwareMaintenance, PlannedHardwareInstallation, UnplannedHardwareInstallation, PlannedOperatingSystemReconfiguration, UnplannedOperatingSystemReconfiguration, PlannedApplicationMaintenance, ApplicationInstallation, ApplicationUnresponsive, ApplicationUnstable, SecurityIssue, LossOfNetworkConnectivity')]
[string]$Reason,
[Parameter(Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Allows you to type a comment about the maintenance activity.')]
[string]$Comment,
[switch]$EventLog
)
Begin
{
Write-Verbose "Starting Function Start-SCOMMaintenanceModeForServer"
#Check for minumum Maintenance mode period of 5 mins.
$start = Get-Date
$5MinFromNowTime = $start.AddMinutes(5)
#$end = [datetime]$end
$end = $end.AddSeconds(5)
if($end -lt $5MinFromNowTime)
{
Write-Error "The time span for the maintenance mode should be at least 5 minutes." -ErrorAction Stop
}
Write-Verbose "Following server will be put in Maintenance Mode: $ServerName"
$MSs = Get-SCOMManagementServer
} #End Begin
Process
{
Write-Verbose "Checking if server $ServerName is a Management Server"
#Write-Verbose ($MSs | Select DisplayName)
if(($MSs | Select DisplayName) -eq $ServerName)
{
Write-Verbose "We don't want to put a Management Server in Maintenance Mode. Skipping"
}
else
{
Write-Verbose "Let's put server $ServerName in Maintenance Mode"
$Instance = Get-SCOMClassInstance -Name $ServerName
if ($PSCmdlet.ShouldProcess("Putting $ServerName in Maintenance Mode until $($end).") )
{
Write-Verbose ("Start-SCOMMaintenanceMode -Instance " + $Instance + " -EndTime " + $end + " -Reason " + $Reason + " -Comment " + $Comment)
Start-SCOMMaintenanceMode -Instance $Instance -end $end -Reason $Reason -Comment $Comment
}#End of whatif
}#End of else
if ($PSBoundParameters['EventLog'])
{
write-eventlog -LogName "Operations Manager" -Source "OpsMgr SDK Service" -EventID 999 -message "The following Objects are put into in Maintenance Mode until $($end) : $($ServerName)"
}#End if
} #End Process
End
{
Write-Verbose "Finished Function Start-SCOMMaintenanceModeForServer Function"
}
}
#Main
try
{
if ($PSBoundParameters['EventLog'])
{
write-eventlog -LogName "Operations Manager" -Source "OpsMgr SDK Service" -EventID 998 -message "The $($myinvocation.mycommand) is used to put Objects in Maintenance Mode"
}
Write-Verbose "Checking if OperationsManager Module is loaded"
#Check if OperationsManager Module is loaded.
if(!(Get-Module OperationsManager))
{
Write-Verbose "Importing OperationsManager Module"
Import-Module OperationsManager -ErrorAction Stop
}
Write-Verbose "Checking for OM2012 environment"
#Check if OM2012 is being used.
if(!(Get-Module OperationsManager).Description -eq "Operations Manager OperationsManagerV10 Module")
{
Write-Error "This script is only for OM2012"
}
#Call Function
if ($PSBoundParameters['EventLog'])
{
Start-SCOMMaintenanceModeForServer -ServerName $ServerName -end $end -Reason $Reason -Comment $Comment -EventLog
}
else
{
Start-SCOMMaintenanceModeForServer -ServerName $ServerName -end $end -Reason $Reason -Comment $Comment
}
} #End Try
catch [System.IO.FileNotFoundException]
{
"OperationsManager Module not found"
$_.Exception.Message
}
catch
{
Write-Warning "Oops something went wrong"
$_.Exception.Message
}
$end=Get-Date
Write-Debug ("Total processing time {0}" -f ($end-$start).ToString())
Write-Verbose "Ending $($myinvocation.mycommand)"
There is one remaining problem with this script. It does not correctly check to see if something is a management server. We have two management servers. These are the applicable lines, which I still haven't gotten to work yet. First, this retrieves
the list of my Management servers:
$MSs=Get-SCOMManagementServer
Next, these lines are supposed to check if the server I specified is a management server:
if(($MSs | Select DisplayName) -eq $ServerName)
{
Write-Verbose "We don't want to put a Management Server in Maintenance Mode. Skipping"
}
Thanks to the Intellisense pop-up deal, I can see that $MSs does get my two management servers. And I can clearly see that there is a DisplayName column. And I can also see that the $ServerName does match what I put in my command line. But it doesn't
seem to catch them if they are actually equal to each other. I don't know if it's because there's two Management Servers, and it doesn't know how to compare like that? Any idea? Is there some sort of loop I need to write so that it compares is to the DisplayName
for EACH Management Server it finds? Any help would be greatly appreciated.