0

We're trying to put servers in maintenance mode remotely. I'm using SCOM functions created by Tom Schumacher.

If I do the following on the SCOM server, the process completes and I'm able to place a server in maintenance mode and stop it again:

#load the function
. C:\temp\opsmanagerFunctions.ps1

#Import the OpsMgr module
Import-Module OperationsManager

#Set server into maintenance mode
Start-serverScommaintenance -servername testserver -message "test" -maintmodeinMinutes '6'

#Stop server maintenance mode
Stop-ServerScommaintenance -servername testserver -message "test complete"

This is all good, however I'm trying to run the above process from another server that will be running several other processes which require a server to be in maintenance mode first.

My though train was to run the following from the secondary server:

#1
C:\temp\opsmanagerFunctions.ps1

#2
Invoke-Command -ComputerName scomservername -ScriptBlock {Import-Module OperationsManager}

#3
Invoke-Command -ComputerName scomservername ${function:Start-serverScommaintenance} 

#4
Invoke-Command -ComputerName scomservername -ScriptBlock {Start-serverScommaintenance -servername testserver -message "test" -maintmodeinMinutes '3'}

#5
Invoke-Command -ComputerName scomservername -ScriptBlock {Stop-ServerScommaintenance -servername testserver -message "test complete"}

However, after trying to run the function on the remote machine (step 3), I get the below error:

The term 'Get-SCOMClassInstance' 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
path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Get-SCOMClassInstance:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
    + PSComputerName        : scomserver
Matt
  • 45,022
  • 8
  • 78
  • 119
Draaiboek
  • 3
  • 1

1 Answers1

2

By using Invoke-Command like so,

Invoke-Command -ComputerName scomservername -ScriptBlock {Import-Module OperationsManager}

you create a session, execute import command and end the session. Thus the next invoke doesn't have the module loaded and the script fails.

Use session to keep using the context like so,

$s = New-PSSession -ComputerName foo
Invoke-Command -Session $s -Scriptblock { <whatever> }
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • I've used the below, however now I'm getting a different error - did I script it incorrectly? . 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'} – Draaiboek Sep 23 '16 at 12:30
  • 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 – Draaiboek Sep 23 '16 at 12:31
  • 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 – Draaiboek Sep 23 '16 at 12:32
  • @Draaiboek Removing the already solved part from the question makes it hard to understand due missing context. Consider accepting this part as answer for the first problem (missing the module) and creating a new question that deals about the next problem(s). – vonPryz Sep 23 '16 at 12:37