0

I am calling the powershell script programmatically using Sitecore Command.. ScriptSession.ExecuteScriptPart() ... Inside the script I have written following script:

$settingItemId = "/sitecore/content/XXXX/Settings/Setting"
$settingItem = Get-Item $settingItemId 

It is throwing error:

Get-Item : Cannot find path 'C:\sitecore\content\XXXX\Settings\Setting' because it does not exist.
At line:122 char:16
+ $settingItem = Get-Item $settingItemId
+                ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\sitecore\con...zation Settings:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Cannot index into a null array.

adding C:\ drive..

Can someone help me here?

Thanks.

  • Try Get-Item -Path "master:\content\xxx" – Marek Musielak Sep 3 at 6:59
  • Yes. It is working. but it is not working when I am trying Get-Item -ID "<guid>".. in this case what can I do .. please suggest – Jitendra Sep 3 at 7:56
  • Get-Item -ID GUID -Database "master" ? – Marek Musielak Sep 3 at 8:05
  • This is working from powershell console... but not working programmatically. – Jitendra Sep 3 at 8:07
  • Do you run it from windows powershell? Or how do you execute your code? – Marek Musielak Sep 3 at 8:48
-1

I see 2 problems with your approach.

  1. First problem is related to the way you are trying to get the item. You need proper syntax like Get-Item -Path "Path" or Get-Item -ID "GUID". This has already been explained by Marek in comments

  2. Second problem is you are running the powershell script stored in a string variable in C# code directly using ExecuteScripts. It can be error prone and you wont be able to change it at runtime. I would recommend to test the script in Powershell console and then save the script as an item in Sitecore using /sitecore/templates/Modules/PowerShell Console/PowerShell Script template. Once that is done, you can execute your powershell script in C# using below code -

    using (ScriptSession scriptSession = ScriptSessionManager.NewSession("Default", true)){
    Item speScriptItem = Sitecore.Context.Database.GetItem("path-or-id-to-spe-item");
    string script = speScriptItem["Script"];
    if (!string.IsNullOrEmpty(script))
        scriptSession.ExecuteScriptPart(script);    }
    
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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