Solved

Powershell help

Posted on 2014-09-03
2
115 Views
Last Modified: 2014-09-18
Hi,

I ran following script and I get  the error:

You cannot call a method on a null valued expression. It is showing that error on the field "MyField" when I invoke substring method if Myfield is null. how do I correct this problem?


Add-PSSnapin Microsoft.Sharepoint.PowerShell

$MyWeb = Get-SPWeb "http://mytestsite"
$MyList = $MyWeb.Lists["Programs"]
$exportList = @()
$MyList.Items | foreach{
$obj = New-Object PSObject -Property @{
   
      "ID" = $_["ID"]
     "Content Type" = $_["testField"]
     "Test Name" = $_["test Name"]
     "MyFieldOriginal" = $_["MyField"]
     "MyFieldtrim" = ($_["MyField"]).substring(($_["MyFields"]).IndexOf("#")+1)
     "MyFieldID" = ($_["MyField"]).split(";")[0]
   
       
}

 
  $exportList += $obj
 
  $exportlist | Where-Object {$_} | Export-Csv -Path "C:\Users\test.csv" -NoTypeInformation
}
0
Comment
Question by:Techsavy
2 Comments
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
Comment Utility
If ($str) would be enough to check if it was null or empty. So a variant on the below should sort it out;

$str = $_["MyField"]
if ($str) {
"MyFieldtrim" = ($_["MyField"]).substring(($_["MyFields"]).IndexOf("#")+1)
}

Open in new window

0
 
LVL 14

Expert Comment

by:GeorgeGergues
Comment Utility
To Add to Jamie's Solution

You need to check if the string contains  "#" and ";"  before splitting on them
as that will case the null value.

I would recommend running your script as
$MyWeb = Get-SPWeb "http://mytestsite"
$MyList = $MyWeb.Lists["Programs"]
$exportList = @()
$MyList.Items | foreach{
$obj = New-Object PSObject -Property @{   
      "ID" = $_["ID"]
     "Content Type" = $_["testField"]
     "Test Name" = $_["test Name"]
     "MyFieldOriginal" = $_["MyField"]  
       
} 
  $exportList += $obj 
  $exportlist | Where-Object {$_} | Export-Csv -Path "C:\Users\test.csv" -NoTypeInformation
} 

Open in new window



If that works ,
add the missing lines one at a time to find the err  data .
     "MyFieldtrim" = ($_["MyField"]).substring(($_["MyFields"]).IndexOf("#")+1)
     "MyFieldID" = ($_["MyField"]).split(";")[0]

Open in new window



Best of luck
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Search 2 19
Sharepoint online 3 41
Failed to upgrade SharePoint Products 8 33
SharePoint JSOM error 7 40
I have just recently built a new SharePoint 2007 farm on a complete Windows 2008 R2 server platform and part of my standard build procedure is to implement a warm up routine, usually in the form of a script that is scheduled every morning to launch …
Last week I faced a strange issue recently, i have deployed SharePoint 2003 servers for one project and one of the requirements was to open SharePoint site from same server. when i was trying to open site from the same server i was getting authentic…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

14 Experts available now in Live!

Get 1:1 Help Now