当前位置: 动力学知识库 > 问答 > 编程问答 >

Powershell 2.0 invoke-command breaks in the script, but works at the breakpoint

问题描述:

I am writing a powershell script, and part of it needs to check the state of a failover cluster on another system. To do that, I run this line of code (which is normally one line, but I've broken it up into multiple lines for legibility here):

$result = @(invoke-command

-argumentlist ($test.test_arg_1)

-computername ($this.name)

-scriptblock {

param($cluster)

import-module failoverclusters;

get-clustergroup "$($cluster)"

})

And then I check the results. Running the script didn't work right, so I put a breakpoint right after it. Here's the odd thing. At the breakpoint, $result was null. But if I cut and paste the exact line into the shell while the script is suspended, using the same variables, it returns the right results.

I tried to recreate it with a simpler script that I could post here, but running the same command from within a small script with locally defined variables (below), and it ran fine. The only possible scope difference I can think of is that in the broken version it's running as a part of an object scriptmethod that I wrote. But all the variables are available from the suspended breakpoint right after this runs.

Anyone have any thoughts about how I can get this to work?

Working version (again, last line formatted for readability):

$test="SQL Server (TEST)"

$comp="test-dbc1"

$result = @(invoke-command -computername $comp

-argumentlist $test

-scriptblock {

param($sys)

import-module failoverclusters

get-clustergroup "$($sys)"

})

FYI, get-host from the system:

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

EDIT (7/25 11:00EST) - @raf got me thinking a little bit, and I did a bit of testing. I noticed the following error:

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

So it appears that for some reason it can't connect using winRM, even though suspending the script and running the same manually doesn't throw that error. Anyone have any thoughts on why running within the script would affect whether winrm can connect?

分享给朋友:
您可能感兴趣的文章:
随机阅读: