2015-04-28 22 views
4

我想使用這個腳本刪除了法文版的所有內容項和Sitecore的離開了英語版本,但想確保它看起來不錯excuting它:(PowerShell腳本刪除了法文版的所有內容項目在Sitecore的

cd 'master:/sitecore/content' 

function FilterItemsToProcess($item) 
{ 
    Get-Item $item.ProviderPath -Language "fr-CA" 
} 

$list = [System.Collections.ArrayList]@() 
$itemsToProcess = Get-ChildItem -Recurse . | foreach {FilterItemsToProcess($_)} 
if($itemsToProcess -ne $null) 
{ 

    $itemsToProcess | ForEach-Object { 
     | remove-item 
    } 
} 
+1

要拍你提供的腳本註釋。 PowerShell知道'@()'是一個數組,所以不需要指定類型。另外,如果命令(如Remove-Item)可以接受管道數據作爲輸入,則不需要ForEach-Object命令。例如,Get-Item通過管道傳遞給Remove-Item。 – Coding101

回答

7

Miroo,你需要知道

的一件事是Remove-Item總是刪除的項目作爲一個整體。即使您輸入語言特定的版本,它也不會刪除該語言。這是因爲sitecore API始終以特定語言返回一個項目,並且Remove-Item無法忽視該意圖。

你需要使用的是Remove-ItemLanguage commandlet。

例如在下面的例子中,我在我的內容中創建一個「測試」項目,然後爲每個項目添加波蘭語版本,並在下一步刪除波蘭語版本。

New-Item master:\content\ -ItemType "Sample/sample item" -Name test -Language en | Out-Null 

foreach ($i in 1..10) { 
    New-Item master:\content\test\ -ItemType "Sample/sample item" -Name $i -Language en | Out-Null 
} 

Get-ChildItem master:\content\test\ | Add-ItemLanguage -TargetLanguage pl-pl -IfExist Skip | Out-Null 

Get-ChildItem master:\content\test\ | Remove-ItemLanguage -Language pl-pl 

你的腳本可以像下面一樣簡單:

$path = "master:\content" 
@(Get-Item $path) + (Get-ChildItem $path -Recurse) | Remove-ItemLanguage -Language "fr-CA" 
+0

Adam,謝謝你指出我使用Remove-Item而不是Remove-ItemLanguage的錯誤。我會在未來記錄下這一點。 – Coding101

+0

謝謝分配:)這有助於分配 – MirooEgypt

1

我會用下面的開頭:

$path = "master:\content" 
@(Get-Item -Path $path -Language "fr") + @(Get-ChildItem -Path $path -Language "fr" -Recurse) 

一旦你肯定知道這是要刪除的項目列表,你可以管這些結果Remove-ItemLanguage

$path = "master:\content" 
@(Get-Item -Path $path -Language "fr") + @(Get-ChildItem -Path $path -Language "fr" -Recurse) | Remove-ItemLanguage -Language "fr" 

查看我們的Gitbook瞭解更多詳情here。關於使用項目的部分涵蓋版本和語言。

相關問題