0

我们正在尝试将服务器远程置于维护模式。我正在使用Tom Schumacher 创建的 SCOM 函数

如果我在 SCOM 服务器上执行以下操作,则该过程完成,我可以将服务器置于维护模式并再次停止它:

#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"

这一切都很好,但是我正在尝试从另一台服务器运行上述进程,该服务器将运行其他几个需要服务器首先处于维护模式的进程。

我的火车是从辅助服务器运行以下内容:

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

但是,在尝试在远程机器上运行该功能(第 3 步)后,我收到以下错误:

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
4

1 回答 1

2

通过Invoke-Command这样使用,

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

您创建一个会话,执行导入命令并结束会话。因此下一次调用没有加载模块并且脚本失败。

使用 session 来继续使用这样的上下文,

$s = New-PSSession -ComputerName foo
Invoke-Command -Session $s -Scriptblock { <whatever> }
于 2016-09-23T12:00:20.470 回答