我尝试使用找到here的示例代码,使用powershell创建一个简单的性能计数器。在最后一行,我用字符串替换变量:
[System.Diagnostics.PerformanceCounterCategory]::Create("catname", "catinfo", [Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $CounterCollection);
当我运行脚本时,我收到以下错误:
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
我用
打印了一些额外的信息$error[0]|format-list -force
其他结果是:
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 :
我的代码有什么问题?
更新
整个代码:
$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
我试图在另一台电脑上运行它,它没有任何错误。
答案 0 :(得分:1)
我找到了解决方案。它与输入字符串无关。首先,我将powershell从2.0更新到3.0,然后我得到了正确的例外:
InvalidOperationException: Cannot load Counter Name data because an invalid index '' was read from the registry.
谷歌(this)的第一个结果解决了我的问题。打开管理员命令提示符并键入lodctr /r
后,我的脚本没有任何问题。