Powershell to pass fixed Varibles (SCOM)

saiyad rahim 21 Reputation points
2020-09-12T02:14:51.93+00:00

Hi all,

Totally new to Powershell and would like some help.
i have script to put all Agents behind a SCOM Gateway into Maintenance Mode:

Import-Module OperationsManager

$Time = ((Get-Date).AddMinutes(10))
$ErrorActionPreference = 'SilentlyContinue'
$InstanceClass = Get-SCOMClass -Name "Microsoft.Windows.Computer"
$Agents = Get-SCOMAgent | where {$_.PrimaryManagementServerName -eq 'Gateway server name'} | Select DisplayName

Foreach($Agent in $Agents){
$Instance = Get-SCOMClassInstance -Class $InstanceClass | Where-Object {$_.DisplayName -match $Agent.DisplayName}
Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Comment "Server Migration." -Reason "PlannedOther"

   }

(Thanks to Leon Laude for his help).

However, I need to modify this where I want to let the user to choose which Gateway/Mgmt Server to target.

If i put my values in a variable like this:

$NthGateway="Server1"
$SthGateway="Server2"
$PrimaryMgtServer="Server2"
$SecondaryMgtServer="Server4"

Have the script ask:

"Select SCOM Mgmt/Gateway"

Press '1' for Server1"
Press '2' for Server2"
Press '3' for Server3"
Press '4' for Server4"

and from there the script will run as normal.

Can someone help me out with this?

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,421 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points
    2020-09-12T14:39:15.523+00:00

    This is clumsy and probably not easy to maintain, but it'll give you an idea of how to accomplish what you asked:

    $Choice = ""
    do {
        Write-Host "Select SCOM Mgmt/Gateway"
        Write-Host "Press '1' for Server1"
        Write-Host "Press '2' for Server2"
        Write-Host "Press '3' for Server3"
        Write-Host "Press '4' for Server4"
        Write-Host "Press 'Q to quit"
        $Choice = Read-Host "Enter your choice and press 'Enter'"
    } until (1,2,3,4,'Q' -contains $Choice)
    

    If the result is 'Q' I'd guess you'd exit the script? How your script should react to that choice is up to you.

    0 comments No comments

  2. saiyad rahim 21 Reputation points
    2020-09-15T00:41:13.677+00:00

    Thanks Rich, I will give this a shot.

    0 comments No comments