Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用powershell比较字符串不起作用_String_Powershell_Compare - Fatal编程技术网

String 使用powershell比较字符串不起作用

String 使用powershell比较字符串不起作用,string,powershell,compare,String,Powershell,Compare,我正在尝试使用powershell比较字符串,但它没有按要求工作 $compare_test = @(Get-Content compare_test.txt) $test = @(Get-Content test.txt) for ($c1 = 0; $c1 -lt $test.Length; $c1++) { $space_position = $test[$c1].IndexOf(" ") $leftpart = $test[$c1].Substring(0, $space

我正在尝试使用powershell比较字符串,但它没有按要求工作

$compare_test = @(Get-Content compare_test.txt)
$test = @(Get-Content test.txt)
for ($c1 = 0; $c1 -lt $test.Length; $c1++) {
    $space_position = $test[$c1].IndexOf(" ")
    $leftpart = $test[$c1].Substring(0, $space_position)
    $rightpart = $test[$c1].Substring($space_position+1)
    $type_cast_rightpart = [int]$rightpart
    for ($c3 = 0; $c3 -lt $compare_test.Length; $c3++) {
        $space_position_2 = $compare_test[$c3].IndexOf(" ")
        $leftpart_2 = $compare_test[$c3].Substring(0, $space_position_2)
        $rightpart_2 = $compare_test[$c3].Substring($space_position_2+1)
        $type_cast_rightpart_2 = [int]$rightpart_2
        $same_server = $leftpart_2.CompareTo($leftpart)
        if ($same_server -eq 0) {
            if ($type_cast_rightpart -gt $type_cast_rightpart_2) {
                $entry_server_log = $leftpart + " " + $type_cast_rightpart
                $myarray += $entry_server_log
                break
            }
            else {
                $entry_server_log = $leftpart + " " + $type_cast_rightpart_2
                $myarray += $entry_server_log
                break
            }
        }
        elseif ($same_server -eq 1 -or $same_server -eq -1) {
            $entry_server_log = $leftpart_2 + " " + $type_cast_rightpart_2
            $myarray += $entry_server_log
            break
        }
    }
}
Set-Content compare_test.txt $myarray
每次我尝试运行powershell脚本时,elseif部分都会执行。 test.txt和compare_测试都包含相同的字符串集,如下所示

NTHKGTBP126 2401
NTHKGTBP132 806

等等。

没有数据,很难回答。您是否看过
Compare-Object
cmdlet