【问题标题】:powershell dll method not found找不到powershell dll方法
【发布时间】:2019-09-23 22:07:39
【问题描述】:

我有一个 PowerShell 脚本,我想在其中以某种方法设置一个值。 我使用了一个 dll,而我现在这些方法都存在于这个 dll 文件中。

但由于某种原因,我仍然收到方法未找到异常。

我在我的脚本中更新了 dll 并打印了文件名,所以我确信我的脚本使用了正确的 dll。 我检查了dll并且该方法存在。

在同一个文件中,我使用了另一种方法,效果很好。

我想也许我写错了,但没有找到。

$messagingDllPath = Join-Path -Path $PSScriptRoot -ChildPath 'dll/OneHIP.Messaging.dll' | Resolve-Path
Add-Type -Path $messagingDllPath # Add assembly

[OneHIP.Messaging.Configuration.ApplicationSettingsProvider]::SetValue("ClientId","idValue")

[OneHIP.Messaging.Configuration.ApplicationSettingsProvider]::SetValue("ClientSecret","secret")
<member name="M:OneHIP.Messaging.Configuration.ApplicationSettingsProvider.SetValue(System.String,System.String)">
            <inheritdoc/>
</member>
2019-09-23T08:18:30.3228501Z ##[error]ValidateBusConfiguration : System.Management.Automation.RuntimeException: Metho
d invocation failed because [OneHIP.Messaging.Configuration.ApplicationSettings
Provider] doesn't contain a method named 'SetValue'.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(F
unctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(Inte
rpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.
Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.
Run(InterpretedFrame frame)
At \\eqx-prd-ohfs.appliarmony.net\MSG\OneHipConfigurationValidationTfsBuildTask
\ValidateOneHIPConfiguration.ps1:217 char:9
+         ValidateBusConfiguration
+         ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorExcep 
   tion
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio 
   n,ValidateBusConfiguration

有人知道这里有什么问题吗?

【问题讨论】:

  • 我觉得这个函数叫SetPropertyValues
  • 在我们的代码中,我们确实有一个名为 Set Value 的方法。 public string SetValue(string settingName, string value) { var oldValue = GetValue(settingName); _storedValues[settingName] = 值;返回旧值; }
  • 你的方法是静态的吗?您正在使用 PowerShell 的静态方法调用约定(即使用 :: 运算符)。如果不是,您需要先创建您的类型的实例(使用New-Object),然后使用实例方法语法调用(即使用. 运算符)。使用 Get-Member:[TypeName] | Get-Member 检查 PowerShell 是否知道该方法。添加-Static 开关以查看静态方法。
  • 谢谢,有问题,方法不是静态的。
  • 酷。我添加了我的评论作为答案,以便将来更容易找到。随意接受它:-)

标签: powershell methods dll


【解决方案1】:

使用Get-Member检查PowerShell是否知道该方法:

$ObjectInstance | Get-Member

添加-Static 开关以查看静态方法:

$ObjectInstance | Get-Member -Static

后一个选项也直接适用于类型:

[TypeName] | Get-Member -Static

您正在使用 PowerShell 的静态方法调用约定(即使用 :: 运算符)。如果它不是静态的(您已经确认),您需要先创建您类型的实例(使用New-Object),然后使用实例方法语法调用(即使用. 运算符) .

【讨论】:

    【解决方案2】:

    方法不是静态的。

    通过创建一个新实例,我能够调用 SetValue 方法。

    感谢@boxdog

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 2016-08-31
      • 2016-12-18
      • 2015-07-17
      相关资源
      最近更新 更多