为什么Powershell会更改变量数据?我可以停止吗?

时间:2019-05-30 17:39:10

标签: powershell

我的脚本使用2个文件的内容,每个文件的第一个元素中都有一个ID。我正在尝试获取文件1中每一行的ID,然后在文件2中找到匹配的行,然后将文件1行与文件2行进行比较。如果它们不同,则该差异将发送到数据库文件进行更新。由于某些原因,保存我的数据的变量决定不再保存数据。在以下示例中,在第一个if($ web -match)语句之后的某个时间,CompareData丢失$ web和$ data的值。

function CompareData($web, $data)
{
    $webs = $web -split "@@@"
    $datas = $data -split "@@@"
    if ($web -match "1862185823")
    {
      write-host "$web"
      ###this will display the full line of the web file, and was only 
      #added to show what i'm talking about
    }
##########################The value of web seems to break sometime after these
##########################variable declarations.
    $wid = $webs[0];$wit = $webs[1];$wst = $webs[2];$wpr = $webs[3];$won = $webs[4];$wsd = $webs[5];$wtd = $webs[6];$wcd = $webs[7];$weh = $webs[8];$wdh = $webs[9];$wah = $webs[10];$wac = $webs[11];$wpd = $webs[12]
    $did = $datas[0];$dit = $datas[1];$dst = $datas[2];$dpr = $datas[3];$don = $datas[4];$dsd = $datas[5];$dtd = $datas[6];$dcd = $datas[7];$deh = $datas[8];$ddh = $datas[9];$dah = $datas[10];$dac = $datas[11];$dpd = $datas[12]
    $wehstr = [string]$weh;$wdhstr = [string]$wdh;$wahstr = [string]$wah;$dehstr = [string]$deh;$dahstr = [string]$dah;$ddhstr = [string]$ddh
    $wdCount = $wit.Length / 2
    $dtCount = $wit.Length / 2
    $newWtitle = $wit.Substring(0, $wdCount)
    $newDtitle = $dit.Substring(0, $dtCount)
    if ($web -match "1862185823")
    {

        #the actual contents of $web are now wrong!!!!!!!!!!!!!!!!
    }
    if ($wehstr.Length -gt $dehstr.Length){$dehstr = $dehstr.PadRight($dehstr.Length + 1, "0")}
    if ($wdhstr.Length -gt $ddhstr.Length){$ddhstr = $ddhstr.PadRight($ddhstr.Length + 1, "0")}
    if ($wahstr.Length -gt $dahstr.Length){$dahstr = $dahstr.PadRight($dahstr.Length + 1, "0")}
    if ($dehstr.Length -gt $wehstr.Length){$wehstr = $wehstr.PadRight($wehstr.Length + 1, "0")}
    if ($dahstr.Length -gt $wahstr.Length){$wahstr = $wahstr.PadRight($wahstr.Length + 1, "0")}
    if ($ddhstr.Length -gt $wdhstr.Length){$wdhstr = $wdhstr.PadRight($wdhstr.Length + 1, "0")}

    if ($newWtitle -match $newDtitle -and $wehstr -match $dehstr -and $wahstr -match $dahstr -and $ddhstr -match $wdhstr -and $wsd -match $dsd -and $wtd -match $dtd -and $wcd -match $dcd -and $won -match $don -and $wst -match $dst -and $wpr -match $dpr -and $wac -match $dac -and $wpd -match $dpd)
    {
        #write-host "$wid and $did match"
    }
    else
    {
        write-host "they didn't match"
    }
    $wid = "";$wit = "";$wst = "";$wpr = "";$won = "";$wsd = "";$wtd = "";$wcd = "";$weh = "";$wdh = "";$wah = "";$wac = "";$wpd = "";
    $did = "";$dit = "";$dst = "";$dpr = "";$don = "";$dsd = "";$dtd = "";$dcd = "";$deh = "";$ddh = "";$dah = "";$dac = "";$dpd = "";
    $webs = ""
    $datas = ""
}

$dc = gc "${DatabaseFile}"
$wc = gc "${WebFile}"
write-host "Started"
foreach ($w in $wc)
{
    try
    {
        $x = 0
        $ws = $w -split "@@@"#custom delimiter since people are using tabs pipes and everything else in the titles....
        $webid = $ws[0]

        $result = select-string -Path "${DatabaseFile}" -pattern $webid

        if ($result -ne $null)
        {

            $newresult = $result -replace "D\:\\phi\\phs\\Oversight\\Projects\\Database.txt:\d{1,}:", ""
            $doublecheck = $newresult -split "@@@"
            if ($doublecheck[0] -eq $webid)
            {


                CompareData $w $newresult


            }
            else
            {
                write-host "Found a mismatch id, searching individual records line by line for a matching id"
                foreach ($d in $dc)#this slow method is only called when someone has another ID in the title in addition to the
                {#first element which is also id. Select-string grabs a match in the entire document, so this is here for single
                #searching each record for a matching id in the first element
                    $ds = $d -split "@@@"
                    $did = $ds[0]
                    $found = 0
                    if ($did -eq $webid)
                    {
                        CompareData $w $d
                        $found = 1
                        break
                    }
                    if ($found -eq 0)
                    {
                        write-host "Added $w to Insert file"
                        $w|out-file -append "${InsertFile}"
                    }

                }
            }
        }
        else
        {
            write-host "Added $w to Insert file"
            $w|out-file -append "${InsertFile}"
        }
    }
    catch
    {

    }
}

1 个答案:

答案 0 :(得分:0)

事实证明,此例中的错误是与行有关

gsutil -m setmeta -h "Cache-Control:public,max-age=31536000,immutable" 'gs://my-storage-bucket-name/myimagefolder/**/*.{jpg,png}'

这些实际上占用title元素的长度,然后将其减半。我不需要确切的标题比较,只是上半部分是字母,以防万一有人确实改变了标题。新代码已更正了此问题,并且一切正常。

$wdCount = $wit.Length / 2
$dtCount = $wit.Length / 2
$newWtitle = $wit.Substring(0, $wdCount)
$newDtitle = $dit.Substring(0, $dtCount)