Powershell 从COM方法检索会话ID

Powershell 从COM方法检索会话ID,powershell,com,powershell-3.0,Powershell,Com,Powershell 3.0,我在名为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,[r

我在名为
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      :
Exception:System.Management.Automation.MethodInvocationException:Exception调用带有“5”参数的“GoLogon”:“无效被调用方(来自HRESULT的异常:0x80020010(DISP_E_BADCALLEE))”-->System.Reflection.targetingexception:
调用的目标已引发异常。-->System.Runtime.InteropServices.ComeException:被调用方无效。(来自HRESULT的异常:0x80020010(DISP_E__BADCALLEE))
---内部异常堆栈跟踪的结束---
位于System.RuntimeType.InvokeDispMethod(字符串名称、BindingFlags invokeAttr、对象目标、对象[]参数、布尔[]byrefModifiers、Int32区域性、字符串[]namedParameters)
位于System.RuntimeType.InvokeMember(字符串名称、BindingFlags BindingFlags、绑定器绑定器、对象目标、对象[]提供的参数、参数修改器[]修饰符、CultureInfo区域性、字符串[]namedParams)
位于System.Management.Automation.CommMethod.InvokeMethod(PSMethod方法,对象[]参数)
---内部异常堆栈跟踪的结束---
位于System.Management.Automation.CommMethod.InvokeMethod(PSMethod方法,对象[]参数)
位于System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod方法、psMethodInvokationConstraints调用约束、Object[]参数)
在System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](调用站点,T0 arg0,T1 arg1,T2 arg2,T3 arg3,T4 arg4,T5 arg5)
在System.Management.Automation.Translator.DynamicInstruction 7.Run(解释器框架)在System.Management.Automation.Translator.EnterTryCatchFinallyInstruction.Run(解释器框架)运行
目标对象:
CategoryInfo:NotSpecified:(:)[],MethodInvocationException
FullyQualifiedErrorId:CommMethodTargetInvocation
错误详细信息:
调用信息:System.Management.Automation.InvocationInfo
ScriptStackTrace:at,E:\Logon.ps1:第18行
PipelineIterationInfo:{}
psmessage详情:

请建议我如何从该方法获取会话ID值。

PowerShell似乎对定义为VARIANT的COM参数有问题

这是GORCOON的C++签名建议的例子。

STDMETHODIMP CLOG::GOLogon(VARIANT *pvEmailId, BSTR bsPassword, BSTR bsIPAddress, BSTR bsSoloBuildVer, VARIANT *pvXML, BSTR *bsSessionId)
这篇文章建议的答案是使用VariantWrapper

在你的情况下,这应该可以做到

#define VariantWrapper for variables to be passesed as VARIANT
$wrapbUservame = New-Object Runtime.InteropServices.VariantWrapper($bUsername) 
$wrappvXML = New-Object Runtime.InteropServices.VariantWrapper($pvXML)

#pass a [ref]VariantWrapper to .COM method
$sessionid=$obj.GoLogon([ref]$wrapbUservame,$bPassword,$appid,[ref]$wrappvXML,$ipad)

您的问题很可能是在参数中使用了[ref]。“[ref]被COM或RPC用来声明指针属性。.NET中的[ref]被用于参数重定向。这些都不兼容。”@Jan Chrbolka实际上,作为方法参数的这两个[ref]参数引用COM中的指针变量。这一直是我在PowerShell中遇到的问题。看看这里。。。这描述了如何使用变体包装器。对你来说可能有用
$variable=new object object$wrapper=new object Runtime.InteropServices.VariantWrapper($variable)##现在使用包装对象$object.method([ref]$wrapper)
@Jan Chrbolka调用该方法,即使我尝试使用包装器,但脚本引发另一个异常。你能为我的脚本提供包装器的用法吗?还忘了提到,该方法返回一个指针值。对不起,这是一个长期的尝试,因为我绝对不是.COM的专家。你有.COM对象的源代码吗?你能把这个问题补充一下吗?或者至少显示ByRef参数的定义?对于代码中的包装器,
$wrapuservame=New Object Runtime.InteropServices.VariantWrapper($bUsername)$sessionid=$obj.GoLogon([ref]$wrapuservame…
对于IP也是如此