2016-10-08 42 views
0

有沒有辦法使用Powershell 2.0自動刪除以給定前綴開頭的Solr核心?例如,我想刪除以「some_prefix」開頭的所有核心。我使用Solr的4.10.1,並希望與下面的API調用來刪除內核:使用Powershell刪除以前綴開頭的Solr核心

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

回答

0

該腳本將工作:

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用於刪除核心。