Solved

Powershell help

Posted on 2014-09-03
2
118 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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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