使用Powershell删除以前缀开头的Solr核心

时间:2016-10-08 21:19:55

标签: powershell solr powershell-v2.0 solr4

有没有办法使用Powershell 2.0自动删除以给定前缀开头的Solr核心?例如,我想删除所有以" some_prefix"开头的核心。我正在使用Solr 4.10.1,并希望使用以下API调用删除核心:

http://localhost:8983/solr/admin/cores?action=UNLOAD&deleteIndex=true&deleteInstanceDir=true&core=some_prefix_etc

1 个答案:

答案 0 :(得分:1)

此脚本可以使用:

param ($prefix = $(throw "-prefix is required"))

$client = (New-Object System.Net.WebClient)
[xml]$coresXML = $client.DownloadString("http://localhost:8983/solr/admin/cores")
$cores = $coresXML.response.lst[2].lst | % {$_.name}
$success = 0
$error = 0

foreach ($core in $cores) {
  if ($core.StartsWith($prefix)) {
    $url = "http://localhost:8983/solr/admin/cores?action=UNLOAD&deleteIndex=true&deleteInstanceDir=true&core=$core"
    write-host "Deleting $core :"
    $client.DownloadString($url)
    if ($?) {$success++}
    else $error++
  }
}
write-host "Deleted $success cores.  Had $error errors."

请参阅this有关如何将核心提取到列表的语法,以及有关删除核心的Solr UNLOAD API选项的this