我的情况相当具体,所以我希望有人有一些见解可以帮助我。
代码
$id = Get-Credential "REMEDY\$($env:USERNAME)"
$userId = $id.UserName.split("\")[1].tolower()
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($id.password)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$cn = New-Object -comobject ADODB.Connection
$rs = New-Object -comobject ADODB.Recordset
$b = "Group5"
$c = "DSN=AR System ODBC Data Source;ARServer=remedyServer;ARServerPort=7130;UID=$userId;PWD=$password"
$cn.Open($c)
$q = "SELECT AuditTrail FROM HPD_HelpDesk WHERE AssignedToGroup='$b'"
$rs.Open($q, $cn)
while (!$rs.EOF) {
$rs.Fields.item(0).value
$rs.MoveNext()
}
$rs.close()
$cn.close()
司机:
Name: AR System ODBC Driver
Version: 7.06.04.30000
File: ARODBC7604_BUILD02.DLL
问题
在 Windows 7 32bit、powershell v4 下,一切都 100% 正常运行,没有错误。在 Windows 10 64bit 下,它大约有 5% 的时间工作(强制 powershell x86 或者它根本看不到 ODBC 驱动程序,但这比问题更烦人)
错误
我什至很难得到错误,因为 Powershell 在发生这种情况时会立即完全关闭,但是从 CMD 运行它可以让我在它关闭之前看到输出。这是我得到的错误,同样的代码偶尔也会起作用。实际崩溃的行是 $rs.fields.item(0).value 行
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at IDispatchInvoke(IntPtr , Int32 , INVOKEKIND , DISPPARAMS& , Variant& , ExcepInfo& , UInt32& )
at System.Management.Automation.ComInterop.UnsafeMethods.IDispatchInvoke(IntPtr dispatchPointer, Int32 memberDispId, INVOKEKIND flags, DISPPARAMS& dispParams, Variant& result, ExcepInfo& excepInfo, UInt32& argErr)
at CallSite.Target(Closure , CallSite , ComObject )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
at System.Management.Automation.DlrScriptCommandProcessor.Complete()
at System.Management.Automation.CommandProcessorBase.DoComplete()
at System.Management.Automation.Internal.PipelineProcessor.DoCompleteCore(CommandProcessorBase commandRequestingUpstreamCommandsToStop)
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
at System.Management.Automation.DlrScriptCommandProcessor.Complete()
at System.Management.Automation.CommandProcessorBase.DoComplete()
at System.Management.Automation.Internal.PipelineProcessor.DoCompleteCore(CommandProcessorBase commandRequestingUpstreamCommandsToStop)
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
at System.Management.Automation.Runspaces.PipelineThread.WorkerProc()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()