【问题标题】:Powershell and XMLRPCPowershell 和 XMLRPC
【发布时间】:2013-12-24 04:53:35
【问题描述】:

我们是一家广泛使用 Powershell 的 Windows 商店。我们还有一个 Spacewalk,我想从现有的 Powershell 脚本中轮询一些数据。 Spacewalk API 通过 XMLRPC 公开。

我花了一些时间寻找有关如何做到这一点的示例,但信息确实很少。我能得到的最接近的是这个链接(不再可用) https://web.archive.org/web/20080202045713/http://thepowershellguy.com/blogs/posh/archive/2008/01/31/powershell-and-xmlrpc-posh-challenge-part-12.aspx

缺乏例子让我觉得我在寻找错误的方向。我知道 new-webserviceproxy 并将它用于查询 Sharepoint,但我没有看到有人将它用于 XMLRPC 调用。

用 Perl 或 Python 编写调用很简单,但这不是我在这种特定情况下需要的......

我是不是走错路了?

【问题讨论】:

    标签: web-services api powershell xml-rpc xmlrpcclient


    【解决方案1】:

    这是我自己实现的,所以我想我会传递它。

    您实际上可以下载 DLL 而不是自己编译源代码 - 我使用 NuGet 找到了 DLL,但有人说您可以从 zip 中获取它。

    我决定在 powershell 中用 C# 代码实现接口,以最大限度地提高可移植性/易于开发。如果需要,您可以将 C# 代码编译为 DLL 并使用 powershell 加载该代码,但每次要更改 C# 代码时都必须返回并重新编译。在这里,powershell 会即时为您重新编译。 (唯一的缺点是如果你使用原生的windows powershell IDE,每次更改C#代码时都必须关闭并重新打开以清除会话)

    这是一个使用 XML-RPC.NET 和 powershell 的 OpenSubtitles API 示例(不是最简洁的代码,但希望能说明 XML-RPC.net 的用法):

    $source = @'
    namespace OpenSubtitlesAPI
    {
        using CookComputing.XmlRpc;
    
        [XmlRpcUrl("http://api.opensubtitles.org/xml-rpc")]
        public interface IOpenSubtitles : IXmlRpcProxy
        {
            [XmlRpcMethod("LogIn")]
            XmlRpcStruct LogIn(string username, string password, string language, string useragent);
    
            [XmlRpcMethod("LogOut")]
            XmlRpcStruct LogOut(string token);
    
            [XmlRpcMethod("SearchSubtitles")]
            XmlRpcStruct SearchSubtitles(string token, XmlRpcStruct[] queries);
    
            [XmlRpcMethod("SearchSubtitles")]
            XmlRpcStruct SearchSubtitles(string token, XmlRpcStruct[] queries, int limit);
        }
    
        public class ProxyFactory
        {
            public static IOpenSubtitles CreateProxy()
            {
                return XmlRpcProxyGen.Create<IOpenSubtitles>();
            }
        }
    }
    '@
    
    # Load XML-RPC.NET and custom interfaces
    if ([Type]::GetType("OpenSubtitlesAPI.ProxyFactory") -eq $null)
    {
        [Reflection.Assembly]::LoadFile("C:\path\to\CookComputing.XmlRpcV2.dll") | Out-Null
        $dynamicAssembly = Add-Type -TypeDefinition $source -ReferencedAssemblies ("C:\path\to\CookComputing.XmlRpcV2.dll")
    }
    
    # Set up proxy
    $proxy = [OpenSubtitlesAPI.ProxyFactory]::CreateProxy()
    $proxy.UserAgent = "user agent"
    $proxy.EnableCompression = $true
    
    # Log in
    $LogInResponse = $proxy.LogIn("user name", "password", "language", "user agent")
    
    # Build query
    $query = New-Object CookComputing.XmlRpc.XmlRpcStruct
    $query.Add("moviehash", "movie hash")
    $query.Add("moviebytesize", "movie size")
    $query.Add("sublanguageid", "language")
    $queries = @($query)
    
    # Search
    $SearchResponse = $proxy.SearchSubtitles($LogInResponse.token, $queries)
    
    # Log out
    $LogOutResponse = $proxy.LogOut($LogInResponse.token)
    

    我对最初的问题的回答有点延迟,但希望这对那里的人有所帮助。

    【讨论】:

      【解决方案2】:

      你看过XML-RPC.NET吗?您必须在 C# 中创建一个实现 IXmlRpcProxy 的 XmlRpcProxyGen 类,但一旦完成,您应该能够加载该 .NET 程序集并使用 PowerShell 中的代理类。

      【讨论】:

      • 谢谢基思。这个解决方案的便携性如何?可以通过将库复制到他们的桌面轻松地被多个用户使用吗?很抱歉提出明显的问题,我不是开发人员。
      • 下载的是src zip。您将需要 VS 将其编译为 .NET 程序集 (dll),但您应该能够轻松地将该 DLL 与您的脚本一起部署。
      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 2023-03-18
      • 2011-03-21
      • 2013-03-16
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多