远程会话

时间:2016-03-03 21:15:52

标签: powershell powershell-remoting scom

我们已将系统警报移至SCOM 2012,并在服务器脱机时收到心跳警报。目前,我管理的vDC中大约有750台服务器。 SCOM 2012服务器位于不同的不受信任的域中。

我有一个工作脚本,它将服务器置于维护模式,但是它以串行方式运行,大约需要40分钟才能将近400台服务器置于维护模式。这是一个可行的解决方案,但我想使用foreach -parallel命令来加速它。

我的工作流程(使用foreach -parallel命令)已创建并放置在源计算机和目标计算机上的某个默认PowerShell模块位置。我已经在SCOM服务器上测试了工作流之外的命令集,并且它成功运行。当我尝试通过Invoke命令远程运行命令时,SCOM命令返回为无法识别。

#Get Date for usage in Connection Name
$Date = Get-Date -Format HHmmsss
#combine Name with Date to uniquify
$name = "ScomMM" + $Date

#Collect Servers from WSUS Server 
[reflection.assembly]::LoadWithPartialName("Microsoft.Updateservices.Administration") | out-null
$WSUS = [Microsoft.updateservices.administration.adminproxy]::Getupdateserver("ServerName",$false,8530);
$TS = $wsus.getcomputertargetgroups() | ? {($_.name -eq "Group1") -or ($_.Name -eq "Group2")}
$computers = $TS.getcomputertargets() | Select-Object FullDomainName

#Setup Trusted host 
invoke-Command -ScriptBlock {(winrm.cmd s winrm/config/client '@{TrustedHosts="*PublicIPAddress*"}')}
Enable-PSRemoting -Force
#Credentials stored in a file
$username = "username"
$password = get-content 'Path' | convertto-securestring
$creds = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,$password
$session = new-PSSession -ComputerName "IPAddress" -Credential $creds -Name $name

#SCOM Commands Module
Import-Module OperationsManager

#Workflow
Import-Module Set-MM

#Run the command remotely
Invoke-Command -Session $session -ScriptBlock {
    Import-Module -Name Set-MM
    Set-MM -computers $using:computers
}

#Workflow - Stored at C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Set-MM\Set-MM.psm1
Workflow Set-MM
{
    Param($computers)
    Foreach -Parallel($computer in $computers)
    {
        Get-SCOMClassInstance -Name $computers.FullDomainName | Start-SCOMMaintenanceMode -EndTime (Get-Date).AddMinutes(6) -Reason PlannedOperatingSystemReconfiguration
    }
}

At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Set-MM\Set-MM.psm1:6 char:3
+         Get-SCOMClassInstance -Name $computers.FullDomainName | Start ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cannot find the 'Get-SCOMClassInstance' command. If this command is defined as a workflow, ensure it is 
defined before the workflow that calls it. If it is a command intended to run directly within Windows 
PowerShell (or is not available on this system), place it in an InlineScript: 'InlineScript { 
Get-SCOMClassInstance }'
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : CommandNotFound
    + PSComputerName        : IPAddress

The term 'Set-MM' 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: (Set-MM:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : IPAddress    

        Get-PSSession | Remove-PSSession

如果我在foreach -Parallel

中的脚本上使用Inlinescript
InlineScript{Get-SCOMClassInstance -Name $Using:computers.FullDomainName | Start-SCOMMaintenanceMode -EndTime (Get-Date).AddMinutes(6) -Reason PlannedOperatingSystemReconfiguration}

我为试图在工作流程中处理的每台计算机都得到了这个:

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 the path is 
correct and try again.
    + CategoryInfo          : ObjectNotFound: (Get-SCOMClassInstance:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : ServerName

0 个答案:

没有答案