powershell – 从COM方法中检索会话ID

我在一个名为BS.Logon的COM对象中有一个名为GoLogon的方法,它需要5个参数:

>用户名
>密码
> ApplicationID
> XMLRoot
> IPAddress

此方法使用用户名,密码和其他详细信息登录到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值.

最佳答案 PowerShell似乎存在定义为VARIANT的COM参数的问题.

这就是这种情况,因为GoLogon的C签名表明了这一点.

STDMETHODIMP CLOG::GOLogon(VARIANT *pvEmailId, BSTR bsPassword, BSTR bsIPAddress, BSTR bsSoloBuildVer, VARIANT *pvXML, BSTR *bsSessionId)

在这篇文章“Invalid callee” calling a com object中建议的答案是使用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)
点赞