Get Solr Cores via Powershell

女生的网名这么多〃 提交于 2019-12-13 17:16:23

问题


Is there a way to use Powershell to get a list of Solr cores running in my local instance of Solr? I am running Solr 4.10.1, and am using Powershell 2.0.

I would like to create a text file of core names:

somecore1
somecore2
etc.

I was able to get a list of my cores in XML format by hitting this URL: http://localhost:8983/solr/admin/cores The pertinent details of the return XML are:

<response>
  <lst name="responseHeader">
    <int name="status">0</int>
    <int name="QTime">2663</int>
  </lst>
  <str name="defaultCoreName">collection1</str>
  <lst name="initFailures"/>
  <lst name="status">
    <lst name="somecore1">
       <!-- Details removed -->
    </lst>
    <lst name="somecore2">
      <!-- Details removed -->
    </lst>
  </lst>
</response>

回答1:


First, you need to load the XML response to an XML variable:

[xml]$cores = (New-Object System.Net.WebClient).DownloadString("http://localhost:8983/solr/admin/cores")

Now you can output the list of cores with this syntax:

$cores.response.lst[2].lst | % {$_.name}

This syntax states, "In XML document, get child of root element "response", then take the third child "lst", then iterate through its children and output the "name" attribute.

To save as a file, redirect:

$cores.response.lst[2].lst | % {$_.name} > c:\temp\cores.txt

Notes:

  • For more information on parsing XML in Powershell, see https://blogs.technet.microsoft.com/heyscriptingguy/2012/03/26/use-powershell-to-parse-an-xml-file-and-sort-the-data/
  • I had to use WebClient because of my version of Powershell. In version 3.0, there is a Invoke-RestMethod command. See https://superuser.com/questions/344927/powershell-equivalent-of-curl


来源:https://stackoverflow.com/questions/39937074/get-solr-cores-via-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!