powershell错误类别信息:InvalidOperation:(拆分:字符串)[],RuntimeException

powershell错误类别信息:InvalidOperation:(拆分:字符串)[],RuntimeException,powershell,Powershell,错误消息: 不能对空值表达式调用方法 第17行字符:31 $compid=$s2[2]。在调用split方法之前,split验证字符串是否不为null。@ojk说了什么-或者向我们展示c:\temp\test\mt_1.txt中的内容。 $Analog_File = "c:\temp\test\mt.txt" #(Get-Item $Analog_File).length if((Test-Path(-not $Analog_File)) -or ((Get-Item $Analog_File)

错误消息:

不能对空值表达式调用方法

第17行字符:31


$compid=$s2[2]。在调用split方法之前,split验证字符串是否不为null。@ojk说了什么-或者向我们展示c:\temp\test\mt_1.txt中的内容。
$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
        }
    }
}