Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.IndexOutOfRangeException When Using PowerShell on Windows Server 2012R2 #303

Closed
geneh opened this issue Sep 6, 2016 · 13 comments
Closed

Comments

@geneh
Copy link
Member

geneh commented Sep 6, 2016

When using AppInsights in PowerShell as described at https://vnextengineer.azurewebsites.net/powershell-application-insights/, everything works fine on Windows 10 but not on Windows Server 2012R2:

PS C:\Users\ghazan\Documents> [Reflection.Assembly]::LoadFile("C:\Users\ghazan\Documents\2.1.0-4.5\Microsoft.Application
Insights.dll")


GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\Users\ghazan\Documents\2.1.0-4.5\Microsoft.ApplicationInsights.dll


PS C:\Users\ghazan\Documents> $telClient = New-Object "Microsoft.ApplicationInsights.TelemetryClient"
PS C:\Users\ghazan\Documents> $telClient.InstrumentationKey = "secret"
PS C:\Users\ghazan\Documents> $telClient.trackEvent("test")
Index was outside the bounds of the array.
At line:1 char:1
+ $telClient.trackEvent("test")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException

Full stack using https://blogs.msdn.microsoft.com/powershell/2006/12/07/resolve-error/:

Message        : Index was outside the bounds of the array.
Data           : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException :
TargetSite     : Int32 CompareOverloadCandidates(OverloadCandidate, OverloadCandidate, System.Object[])
StackTrace     :    at System.Management.Automation.Adapter.CompareOverloadCandidates(OverloadCandidate candidate1,
                 OverloadCandidate candidate2, Object[] arguments)
                    at System.Management.Automation.Adapter.FindBestCandidate(IEnumerable`1 candidates, Object[]
                 arguments)
                    at System.Management.Automation.Adapter.FindBestCandidate(IEnumerable`1 candidates, Object[]
                 arguments, PSMethodInvocationConstraints invocationConstraints)
                    at System.Management.Automation.Adapter.FindBestMethod(MethodInformation[] methods,
                 PSMethodInvocationConstraints invocationConstraints, Object[] arguments, String& errorId, String&
                 errorMsg, Boolean& expandParamsOnBest)
                    at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeDotNetMethod(DynamicMetaObject
                 target, DynamicMetaObject[] args, BindingRestrictions restrictions, MethodInformation[] mi, Type
                 errorExceptionType)
                    at
                 System.Management.Automation.Language.PSInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject
                 target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
                    at System.Management.Automation.PSObject.PSDynamicMetaObject.BindInvokeMember(InvokeMemberBinder
                 binder, DynamicMetaObject[] args)
                    at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters,
                 LabelTarget returnLabel)
                    at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
                    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
                    at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
                    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
                 frame)
HelpLink       :
Source         : System.Management.Automation
HResult        : -2146233080

I’ve tried both https://www.nuget.org/api/v2/package/Microsoft.ApplicationInsights/1.2.3 and https://www.nuget.org/api/v2/package/Microsoft.ApplicationInsights/2.1.0 with versions 4.0 and 4.5.

Windows Server 2012R2 (not working):

$PSVersionTable
Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34209
BuildVersion                   6.3.9600.17400
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

Windows 10 (working):

$PSVersionTable
Name                           Value                                                                                                                                                           
----                           -----                                                                                                                  
PSVersion                    5.0.10586.494                                                                                                                                         
PSCompatibleVersions      {1.0, 2.0, 3.0, 4.0... }                                                           
BuildVersion                  10.0.10586.494                                                                                                                                                  
CLRVersion                    4.0.30319.42000                                                                                                                                                 
WSManStackVersion             3.0                                                                                                                                                             
PSRemotingProtocolVersion     2.3                                                                                                                                                             
SerializationVersion           1.1.0.1

Could you please advise how this issue can be resolved?

@SergeyKanzhelev
Copy link
Contributor

Oh, judging by the name CompareOverloadCandidates I'd guess that you just need to do TrackEvent rather than 'trackEvent' (note upper case).

@geneh
Copy link
Member Author

geneh commented Sep 6, 2016

Unfortunately I am getting the same error:

$telClient.TrackEvent("test")

Message        : Index was outside the bounds of the array.
Data           : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException :
TargetSite     : Int32 CompareOverloadCandidates(OverloadCandidate, OverloadCandidate, System.Object[])
StackTrace     :    at System.Management.Automation.Adapter.CompareOverloadCandidates(OverloadCandidate candidate1,
                 OverloadCandidate candidate2, Object[] arguments)
                    at System.Management.Automation.Adapter.FindBestCandidate(IEnumerable`1 candidates, Object[]
                 arguments)
                    at System.Management.Automation.Adapter.FindBestCandidate(IEnumerable`1 candidates, Object[]
                 arguments, PSMethodInvocationConstraints invocationConstraints)
                    at System.Management.Automation.Adapter.FindBestMethod(MethodInformation[] methods,
                 PSMethodInvocationConstraints invocationConstraints, Object[] arguments, String& errorId, String&
                 errorMsg, Boolean& expandParamsOnBest)
                    at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeDotNetMethod(DynamicMetaObject
                 target, DynamicMetaObject[] args, BindingRestrictions restrictions, MethodInformation[] mi, Type
                 errorExceptionType)
                    at
                 System.Management.Automation.Language.PSInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject
                 target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
                    at System.Management.Automation.PSObject.PSDynamicMetaObject.BindInvokeMember(InvokeMemberBinder
                 binder, DynamicMetaObject[] args)
                    at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters,
                 LabelTarget returnLabel)
                    at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
                    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
                    at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
                    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
                 frame)
HelpLink       :
Source         : System.Management.Automation
HResult        : -2146233080

@SergeyKanzhelev
Copy link
Contributor

The only mention of this issue I found so far is in this PR: microsoft/azure-pipelines-extensions@e563501

Can you please try TrackEvent("test", $nil, $nil) or whatever null is in powershell.

I suspect it found this override and failed. Maybe a hint to use a different override will help.

I'm speculating here

@geneh
Copy link
Member Author

geneh commented Sep 7, 2016

I've upgraded .NET to 4.6, ran the script as an administrator, added nulls to the method's signature and modified the script as following:

$AppInsightsInstrumentationKey = "<secret>"
$ES = "$PSScriptRoot\2.1.0-4.5\Microsoft.Diagnostics.Tracing.EventSource.dll"
[Reflection.Assembly]::LoadFile($ES)
$AI = "$PSScriptRoot\2.1.0-4.5\Microsoft.ApplicationInsights.dll"
[Reflection.Assembly]::LoadFile($AI)
[Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]::Active.InstrumentationKey = $AppInsightsInstrumentationKey
$TelClient = New-Object "Microsoft.ApplicationInsights.TelemetryClient"
$TelClient.InstrumentationKey = $AppInsightsInstrumentationKey
$TelClient
$TelClient.TrackEvent("hello", $null, $null)

There are no errors now and the output looks right:

GAC    Version        Location                                                                                                               
---    -------        --------                                                                                                               
False  v4.0.30319     C:\Users\ghazan\Documents\2.1.0-4.5\Microsoft.Diagnostics.Tracing.EventSource.dll                                      
False  v4.0.30319     C:\Users\ghazan\Documents\2.1.0-4.5\Microsoft.ApplicationInsights.dll                                                  

Context            : Microsoft.ApplicationInsights.DataContracts.TelemetryContext
InstrumentationKey : <secret>

The problem now is that the event does not show up in App Insights.
If I run the same code from a Windows 10 machine, the event does show up.
Any ideas what may be going wrong here?

@SergeyKanzhelev
Copy link
Contributor

Try to run: $TelClient.Flush() Maybe powershell wouldn't wait for event to be send and kill some threads we are running

@SergeyKanzhelev
Copy link
Contributor

BTW, did just passing of argument helped or another framework and EventSoruce fixed the issue?

@geneh
Copy link
Member Author

geneh commented Sep 7, 2016

This is getting interesting. If I run $TelClient.Flush() and interrogate the last exception, it reveals that more dll's need to be loaded. I'll try that next.

Message        : Could not load file or assembly 'Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, 
                 PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
FileName       : Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
FusionLog      : WRN: Assembly binding logging is turned OFF.
                 To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
                 Note: There is some performance penalty associated with assembly bind failure logging.
                 To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Data           : {}
InnerException : 
TargetSite     : Void DequeueAndSend()
StackTrace     :    at Microsoft.ApplicationInsights.Channel.InMemoryTransmitter.DequeueAndSend()
                    at Microsoft.ApplicationInsights.Channel.InMemoryChannel.Flush()
                    at CallSite.Target(Closure , CallSite , Object )
HelpLink       : 
Source         : Microsoft.ApplicationInsights
HResult        : -2147024894

Loading the EventSource and passing additional arguments helped with the previous problem.

@SergeyKanzhelev
Copy link
Contributor

Are you sure you are using 4.5 version of Application Insights dll?

@geneh
Copy link
Member Author

geneh commented Sep 7, 2016

I think so, although anything is possible. I am not a .NET developer.

@SergeyKanzhelev
Copy link
Contributor

My question is where did you take this assembly from:

$AI = "$PSScriptRoot\2.1.0-4.5\Microsoft.ApplicationInsights.dll"
[Reflection.Assembly]::LoadFile($AI)

@geneh
Copy link
Member Author

geneh commented Sep 7, 2016

@geneh
Copy link
Member Author

geneh commented Sep 7, 2016

Finally, it looks like it started working for me. I've started using dll from lib\net46.

@geneh
Copy link
Member Author

geneh commented Sep 7, 2016

Thanks for help, Sergey!

@geneh geneh closed this as completed Sep 7, 2016
TimothyMothra added a commit that referenced this issue Oct 25, 2019
* new cert

* update
TimothyMothra added a commit that referenced this issue Oct 25, 2019
* fix signing (#301)

* nupkg and symbols (#300)

* Nuget.props and new symbols format

* fixing compile issues

* fix doc file

* ApplicationInsights.NLogTarget - Updated docs with NLog.config example (#294)

* new cert (#303)

* new cert

* update

* bump version 2.11-beta2 (#309)

* bump version 2.11-beta2

* Update CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants