Solved

Powershell help

Posted on 2014-09-03
2
119 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
[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 32

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 40303027
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
ID: 40312498
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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