Solved

Powershell help

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

17 Experts available now in Live!

Get 1:1 Help Now