سؤال

I'm trying to create a simple performance counter with powershell using an example code found here. In the last line i replaced the variables with strings:

[System.Diagnostics.PerformanceCounterCategory]::Create("catname", "catinfo", [Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $CounterCollection); 

When i'm run the script, i get the following error:

Exception calling "Create" with "4" argument(s): "Input string was not in a correct format."
At C:\...\setup_test.ps1:9 char:56
+ [System.Diagnostics.PerformanceCounterCategory]::Create <<<< ("catname", "catfaszom",     [Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $CounterCollection); 
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

I printed some extra info with

$error[0]|format-list -force

The additional result is:

Exception             : System.Management.Automation.MethodInvocationException: Exception calling "Create" with "4" argument(s): "Input string was not in a correct format." ---> System.FormatException: Input string was not in a correct format.
                       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
                       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
                       at System.Diagnostics.PerformanceCounterLib.GetStringTable(Boolean isHelp)
                       at System.Diagnostics.PerformanceCounterLib.get_NameTable()
                       at System.Diagnostics.PerformanceCounterLib.get_CategoryTable()
                       at System.Diagnostics.PerformanceCounterLib.CategoryExists(String machine, String category)
                       at System.Diagnostics.PerformanceCounterCategory.Create(String categoryName, String categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)
                       at Create(Object , Object[] )
                       at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] originalArguments)
                       --- End of inner exception stack trace ---
                       at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] originalArguments)
                       at System.Management.Automation.ParserOps.CallMethod(Token token, Object target, String methodName, Object[] paramArray, Boolean callStatic, Object valueToSet)
                       at System.Management.Automation.MethodCallNode.InvokeMethod(Object target, Object[] arguments, Object value)
                       at System.Management.Automation.MethodCallNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
                       at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
                       at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : DotNetMethodException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {}
PSMessageDetails      : 

What is wrong whit my code?

Update:

The whole code:

$ccdTypeName = 'System.Diagnostics.CounterCreationData'
$CounterCollection = New-Object System.Diagnostics.CounterCreationDataCollection

#create as many counters as we'd like, and add them to the collection. here's just one:
$CounterCollection.Add( (New-Object $ccdTypeName "TestCtr1", "Test counter 1 description", NumberOfItems32) )

#create the category with the counter collection
[System.Diagnostics.PerformanceCounterCategory]::Create("asd", "dasd", [Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $CounterCollection);
$error[0]|format-list -force

I tried to run it on an another pc, and it worked without any errors.

هل كانت مفيدة؟

المحلول

I found the solution. It had nothing to do with Input string. First, i updated powershell from 2.0 to 3.0, then i got the right exception:

InvalidOperationException: Cannot load Counter Name data because an invalid index '' was read from the registry.

The first result in google (this) solved my problem. After opening an administrator command prompt and typed lodctr /r, my script worked without any problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top