4
我有一個在名爲BS.Logon
一個COM對象命名GoLogon
方法,該方法需要5個參數:檢索會話ID
- 用戶名
- 密碼
- 的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值。
您的問題很可能是您在參數中使用了[ref]。 *「[ref]被COM或RPC用來聲明指針屬性,.NET中的[ref]是 ,用於參數重定向。這些都不兼容。」* –
@Jan Chrbolka其實這兩個[ref]參數是方法的參數,指的是COM中的指針變量。 –
這在PowerShell中對我來說一直是個問題。看看這裏... [RE:「無效的被調用者」調用COM對象](http://www.archivum.info/microsoft.public.windows.powershell/2010-01/00361/RE--quot-Invalid -callee-quot - calling-a-com-object.html)這將描述如何使用Variant包裝器。它可能適用於你的情況。 '$ variable = new-object object $ wrapper = New-Object Runtime.InteropServices.VariantWrapper($ variable) ##現在使用包裝對象調用方法 $ object.method([ref] $ wrapper)' –