0
votes

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
1

1 Answers

2
votes

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> }