Powershell 不调用 c# 对象中的方法

Powershell not invoking method in a c# object

我有一堆 C# 库,它们是我编写的,应该被 PowerShell 脚本用来在我们的环境中执行各种 devops 任务。 最奇怪的事情正在发生,我认为这与我的 PowerShell 脚本中的库的外部加载有关。

工作示例

首先,我将演示通用代码的工作原理和一个 class 这样的代码,以便您了解我是如何使用它的: 我们首先为我的 API

加载外部库
$binpath = "$PSScriptRoot\bin"
#Reads credentials
Add-Type -Path "$binpath\CredentialHandling.dll"
#Is used for logging
Add-Type -Path "$binpath\OperationLogging.dll"
#Is used to handle the different dev environments (we have a lot of them!)
Add-Type -Path "$binpath\IdmEnvironmentHandling.dll"
#Is used to perform operations in LDAP
Add-Type -Path "$binpath\IdmLdapInteractions.dll"
#Is used to perform operations with a specific REST API service
#The base library that contains general methods and binds it all together
Add-Type -Path "$binpath\IdmApi.dll"

我设置了应该调用 C# 代码中的构造函数的对象,然后继续对其中一个连接器执行方法

#The IdmApiOperator contains multiple connectors to different platforms to perform unified tasks
$idm = [IdmApi.IdmApiOperator]::new($environment)
$searchbase = 'ou=OrganizationalUnit,o=SYSTEM'
$ldapFilter = '(cn=CommonNameExample)'
#This will return some sort of ldap search response, which works
$search = $idm.LdapOperator.Search($searchbase, ldapFilter)

此代码上下文中的 Ldap 运算符使用标准 .NET 库 System.DirectoryServices.Protocols,它的工作原理非常棒,所有实际代码都在 C# 和 PowerShell 中运行。

Idm成员Api连接器(运算符的基础class,有点像接口)

行不通的例子

我们首先为我的 API 加载外部库,这次包括一个用于 REST API 的外部库,这个对 RestOperator class 的调用将失败,我不明白为什么

$binpath = "$PSScriptRoot\bin"
#Reads credentials
Add-Type -Path "$binpath\CredentialHandling.dll"
#Is used for logging
Add-Type -Path "$binpath\OperationLogging.dll"
#Is used to handle the different dev environments (we have a lot of them!)
Add-Type -Path "$binpath\IdmEnvironmentHandling.dll"
#Is used to perform operations in LDAP
Add-Type -Path "$binpath\IdmLdapInteractions.dll"
#Is used to perform operations with a specific REST API service
Add-Type -Path "$binpath\IdmRestInteractions.dll"
#Used by IdmRestInteractions, put in here just to be safe
Add-Type -Path "$binpath\RestSharp.dll"
#The base library that contains general methods and binds it all together
Add-Type -Path "$binpath\IdmApi.dll"

$idm = [IdmApi.IdmApiOperator]::new($environment)
#Purely a test, this method will return an Oauth token for the specified connection, this method is a derivative of the actual method that will set a token property in the base class.
$token = $idm.RestOperator.GetToken()
#Will return null, not even an error is thrown

GetToken() 方法的代码,return 是一个 IdmToken,在 IdmRestInteractions.Objects 下的同一命名空间中定义:

public IdmToken GetToken()
        {
            string path = MethodPaths["OAuthUri"];
            RestRequest request = new RestRequest()
            {
                Method = Method.POST,
                Resource = path,
            };
            string authstring = GetOAuthString();
            request.AddHeader("Authorization", authstring);
            string body = ""//here it creates the body, removed in this version
            request.AddParameter("application/x-www-form-urlencoded", body, ParameterType.RequestBody);
            IRestResponse response = Client.Post(request); //Client here being an IRestClient object from the library RestSharp
            return JsonConvert.DeserializeObject<IdmToken>(response.Content);
        }

查看 tomcat 主机,该方法似乎没有通过,因为服务器没有收到任何请求。 把方法的输出改成乱码,就会return乱码,所以并不是说方法没有触发。 我还尝试将 Newtonsoft.Json 添加到我的 PowerShell 代码中,这也没有任何改变。 然后当然当我稍后在我的程序中使用令牌时它会给出一个 nullref 异常...

总结

我快速浏览了 docs and this 答案。客户端默认不会抛出异常。 Content 将只是空的,您的方法将无声地失败并且 return 为空。可能的解决方案包括检查 ErrorException 属性 或设置 ThrowOnAnyError:

# option 1
Client.ThrowOnAnyError = true;

# option 2
if (response.ErrorException != null)
{
    throw new Exception("Request failed.", response.ErrorException);
}