6

I am running a script in Sitecore powershell to find items bases on a regular expression on a custom field ('Subkey'). This script sometimes shows results and sometimes shows 'No items available'.

Why is this not consistent? And how is this possible? I am running this within the Sitecore ISE

Write-Host "Begin script"

function Get-InvalidKeysAndSubkeys {
   #param([string]$label)
   Get-ChildItem  -Recurse | Where-Object { 
    (
     ((![string]::IsNullOrEmpty($_.Subkey)) -and ($_.Subkey -match "[^a-zA-Z0-9$ \-_\.!'()]"))       
     )
    }
  }
$x = Get-InvalidKeysAndSubkeys
$x | Show-ListView -Property Name,Key,SubKey,Id,ItemPath
Write-Host "End script"

2 Answers 2

5

You are missing one important parameter (-Path) when invoking Get-ChildItem:

Get-ChildItem  -Recurse

Fix this with something like this:

Get-ChildItem -Path "/sitecore/content" -Recurse

If you do not define -Path parameter your context item (start item for getting children) will be taken from item defined here

enter image description here

3
  • I am using the Context setting. So a Path parameter is not needed?
    – Gluip
    Oct 24, 2016 at 15:38
  • Yes. I assumed that different Context item was your main problem thus I shared a different approach to invoke Get-ChildItem. Are you able to reproduce unwanted behavior while using -Path parameter as well? Oct 24, 2016 at 19:59
  • In the event that you choose not to use the -Path parameter, you can put Set-Location at the top of your script. I don't recommend this approach but would get the job done when running the script in multiple contexts (i.e. ISE, Console, Context Menu). Oct 25, 2016 at 15:00
3

Turns out the problem was not having a

-Language

parameter for the Get-ChildItem call. Sometimes it would be evaluated for the correct language and sometimes not. Forcing the language fixed the problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.