?
Solved

Debugging a PowerShell Simple Code

Posted on 2012-04-09
2
Medium Priority
?
413 Views
Last Modified: 2012-04-10
Hi Experts,

This is my first time writing a powershell script. So, I really need some help here.
Below code is a very simple code to write a new record into a list. But I am getting the following error

"Cannot index into a null array.
At D:\Batch Jobs\PowerShell Scripts\test.ps1:8 char:59
+ $root.lists.add("CTest","Custom Test",$root.listtemplates[ <<<< "Custom List"])
    + CategoryInfo          : InvalidOperation: (Custom List:String) [], RuntimeException
    + FullyQualifiedErrorId : NullArray"


Can advise what could the possible problem for the error?


[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")

# Connect to the site collection
$site = New-Object Microsoft.SharePoint.SPSite("http://sharepoint.apb.com.sg/gmis/ec")
$web     =    $site.rootweb
$list    =    $web.Lists["Email Convertor Service Check"]

$newitem =    $list.items.Add()
$newitem["Date"]=    "4/10/2012";                                                                                          
$newitem["Result"] =    "Email Converter Service Stopped";                                                                                          
 
$newitem.update() 
 
$web.Dispose()
$site.Dispose() 

Open in new window

0
Comment
Question by:NicksonKoh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Author Comment

by:NicksonKoh
ID: 37826345
Hi Expert,

I realise my mistake is in the following 3 lines of code

$site = New-Object Microsoft.SharePoint.SPSite("http://sharepoint.apb.com.sg/gmis/ec")
$web     =    $site.rootweb
$list    =    $web.Lists["Email Convertor Service Check"]

$web changes the pointer to go to the rootweb but
my "Email Converter Service Check" is a custom list in $site

What should I change such that the list should be pointing to $site and not the rootweb.
0
 
LVL 18

Accepted Solution

by:
ivan_vagunin earned 2000 total points
ID: 37826428
Hi!
Try to use $web = $site.OpenWeb("/") instead of $web = $site.rootweb.
What do you mean saying list should be pointint to $site?
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question