Solved

Powershell help

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

I used to be SharePoint evangelist in our company, so my Outlook always full of questions about how to do this, or where I can find that. One day I found such an email with the following question: "how to attach 3-State workflow (one of the workflow…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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