【发布时间】:2015-08-11 07:53:31
【问题描述】:
我在一个名为 BS.Logon 的 COM 对象中有一个名为 GoLogon 的方法,它需要 5 个参数:
- 用户名
- 密码
- 应用程序ID
- XMLRoot
- IP 地址
此方法使用用户名、密码和其他详细信息登录 Web 服务器,并返回会话 ID。
我已经编写了 Powershell 脚本来调用方法GoLogon,如下所示。
$obj=New-Object -ComObject BS.Logon
$sessionid=$obj.GoLogon([ref]$bUsername,$bPassword,$appid,$xmlroot,[ref]$ipad)
Write-Host $sessionid
当脚本执行时,该方法使登录成功。我可以在数据库中看到会话 ID 详细信息,但无法通过脚本获取会话 ID 详细信息。变量 $sessionid 返回 null。脚本也会抛出异常
Exception calling "GoLogon" with "5" argument(s): "Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))"
At E:\Logon.ps1:18 char:1
+ $obj.Login([ref]$bUsername,$bPassword,$appid,$xmlroot,[ref]$ipad)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation`
堆栈跟踪是:
Exception : System.Management.Automation.MethodInvocationException: Exception calling "GoLogon" with "5" argument(s): "Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))" ---> System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))
--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
--- End of inner exception stack trace ---
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)
at System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod method, PSMethodInvocationConstraints invocationConstraints, Object[] arguments)
at System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
at System.Management.Automation.Interpreter.DynamicInstruction 7.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject :
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : ComMethodTargetInvocation
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, E:\Logon.ps1: line 18
PipelineIterationInfo : {}
PSMessageDetails :
请建议我如何从方法中获取会话 ID 值。
【问题讨论】:
-
您的问题很可能是您在参数中使用了 [ref]。 “COM 或 RPC 使用 [ref] 来声明指针属性。.NET 中的 [ref] 用于参数重定向。这些绝不兼容。”
-
@Jan Chrbolka 实际上这两个作为方法参数的 [ref] 参数是指 COM 中的指针变量。
-
这一直是我在 PowerShell 中的一个问题。看看这里 ...RE: "Invalid callee" calling a com object 这描述了如何使用 Variant 包装器。它可能适用于您的情况。
$variable = new-object object $wrapper = New-Object Runtime.InteropServices.VariantWrapper($variable) ## now call the method with the wrapped object $object.method([ref] $wrapper ) -
@Jan Chrbolka 即使我尝试使用包装器,但脚本会引发另一个异常。您能否提供我的脚本的包装器用法?另外忘了提,该方法返回一个指针值。
-
抱歉,这有点远,因为我绝对不是 .COM 方面的专家。你有 .COM 对象的源代码吗?你能把它添加到问题中吗?或者至少显示 ByRef 参数的定义?对于代码中的包装器,
$wrapbUservame = New-Object Runtime.InteropServices.VariantWrapper($bUsername) $sessionid=$obj.GoLogon([ref]$wrapbUservame ....和 IP 相同
标签: powershell com powershell-3.0