1
$Analog_File = "c:\temp\test\mt.txt"
#(Get-Item $Analog_File).length
if((Test-Path(-not $Analog_File)) -or ((Get-Item $Analog_File).length -eq 0kb))
{
    exit
}
$m = Select-String -pattern  ",T" c:\temp\test\mt_1.txt
#$m.length #Get the number of rows

For ($i=0; $i -le $m.length; $i++)
{
    $s1 = $m[$i] -split ":"
    $s1[2]
    if ([int]$s1[2] -ge 9)    #le = less than, ge = greater than, eq = equal to
    {
        $s2 = $s1[3] -split ","
        $compid = $s2[2].split('''')[0] + "." + $s2[3].split('''')[0] + "." + $s2[4].split('''')[0] + "." + $s2[5].split('''')[0]
        $matches = (Select-String -pattern  $compid c:\temp\test\mt_1.txt).line
        #($matches.split(';')[0]) 
        #($matches.split(';')[0]).split(',')[0]
        #($matches.split(';')[0]).split(',')[1]
        if ($s2[6] -eq "T")
        {
            #$s2[2].split('''')[1]
            #"Not in Service"
            #"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!0!" >> $fname
        }
        if ($s2[7] -eq "T")
        {
            #"Alarm inhibit"
            #"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!1!" >> $fname
        }
        if ($s2[8] -eq "T")
        {
            #"Manual update"
            #"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!2!" >> $fname
        }
    }
}

the error message:

You cannot call a method on a null-valued expression.

At line:17 char:31

  • $compid = $s2[2].split <<<< ('''')[0] + "." + $s2[3].split('''')[0] + "." + $s2[4].split('''')[0] + "." + $s2[5].split('''')[0]

    • CategoryInfo : InvalidOperation: (split:String) [], RuntimeException

    • FullyQualifiedErrorId : InvokeMethodOnNull

what can i do??

2
  • 1
    Validate that the string is not null before calling the split method.
    – ojk
    Sep 23, 2014 at 9:13
  • What @ojk said - or show us what's inside c:\temp\test\mt_1.txt.
    – Raf
    Sep 23, 2014 at 9:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.