Solved

Debugging a PowerShell Simple Code

Posted on 2012-04-09
2
406 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
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 500 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now