Powershell 2.0调用命令在脚本中断,但在断点处起作用

时间:2014-07-23 13:39:20

标签: powershell powershell-v2.0

我正在编写一个powershell脚本,其中一部分需要检查另一个系统上的故障转移群集的状态。要做到这一点,我运行这行代码(通常是一行代码,但我在这里将其分解为多行以便于读取):

$result = @(invoke-command 
            -argumentlist ($test.test_arg_1) 
            -computername ($this.name) 
            -scriptblock { 
                  param($cluster) 
                  import-module failoverclusters; 
                  get-clustergroup "$($cluster)" 
             })

然后我检查结果。运行脚本没有用,所以我在它之后放了一个断点。这是奇怪的事情。在断点处,$ result为null。但是如果我在脚本暂停时将确切的行剪切并粘贴到shell中,使用相同的变量,它会返回正确的结果。

我试图用一个更简单的脚本重新创建它,我可以在这里发布,但是在一个带有本地定义变量的小脚本中运行相同的命令(如下),它运行正常。我能想到的唯一可能的范围差异是,在破碎的版本中,它作为我编写的对象脚本方法的一部分运行。但是在运行之后,所有变量都可以从挂起的断点处获得。

任何人都对如何让它发挥作用有任何想法?

工作版本(同样,为了可读性而格式化的最后一行):

$test="SQL Server (TEST)"
$comp="test-dbc1"
$result = @(invoke-command -computername $comp 
               -argumentlist $test 
               -scriptblock { 
                     param($sys) 
                     import-module failoverclusters
                     get-clustergroup "$($sys)" 
                })

仅供参考,来自系统的get-host:

Name             : ConsoleHost
Version          : 2.0
InstanceId       : 65fcc12c-4333-49d8-a340-92690d7aeb23
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

编辑(7月25日11:00) - @raf让我思考了一点,我做了一些测试。我注意到以下错误:

PSMessageDetails      :
OriginInfo            : test-dbc1
Exception             : System.Management.Automation.Remoting.PSRemotingTransportException: Processing data from remote
                         server failed with the following error message: The client cannot connect to the destination s
                        pecified in the request. Verify that the service on the destination is running and is accepting
                         requests. Consult the logs and documentation for the WS-Management service running on the dest
                        ination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following
                         command on the destination to analyze and configure the WinRM service: "winrm quickconfig". Fo
                        r more information, see the about_Remote_Troubleshooting Help topic.
TargetObject          : System.Management.Automation.PSInvokeExpressionSyncJob
CategoryInfo          : OperationStopped: (System.Manageme...pressionSyncJob:PSInvokeExpressionSyncJob) [], PSRemotingTransportException
FullyQualifiedErrorId : JobFailure
ErrorDetails          :
InvocationInfo        :
PipelineIterationInfo : {}

因此,出于某种原因,它似乎无法使用winRM进行连接,即使暂停脚本并手动运行该脚本也不会抛出该错误。任何人都有任何想法为什么在脚本中运行会影响winrm是否可以连接?

0 个答案:

没有答案