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>
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