PowerShell设置符号在运行脚本时不起作用 - 可能的编译器错误



我想我会疯了,但希望对这种怪异行为有一个解释。我有一个将kix文件复制到新位置的脚本,在EXE的新版本和旧版本上检查文件版本,然后在多个位置修改kix文件。以下是一个简化的版本。

第一个也是最后一个set-content总是起作用。在我运行整个脚本时,在检查文件版本后出现的set-content总是失败(在整个过程中,对于不同的文件版本,set-content的多个实例,它们都失败了(。

但是,如果我放置一个断点,或者只是突出显示该部分并运行它,则确实有效。我知道该部分被击中了,因为我在此处未显示该块中进一步处理,例如多个copy-item

当我说失败时,我的意思是内容不会在文件中更新。没有错误,只是不会更新。

我尝试将每行之间的start-sleep列入多秒钟,这无济于事。我还尝试通过声明版本而不运行fileversioninfo来使其尽可能简单,但仍然失败。我很难进行故障排除,因为当我放置断点时,它会起作用。

#Declare Paths
$Temp = "\NewLocatione$Temp"
$path = "\sharefolder"
$FinalDest = "\FinalRestingPlaceFolder"
#Copy file to Temp
Copy-item "\OtherSharefile1.kix" $Temp 
#Get-Content
$KIXOLD = (get-content $Tempfile1.kix)
#Increment Version Number
[decimal]$OLDVER = 12.0
$NEWVER = ($oldver + .1)

这有效

#Update Version Number in File 
$VerLine = select-string -Pattern $oldver -path $tempfile1.kix | select -ExpandProperty LineNumber | select -Index 1
$KIXOLD[$VerLine - 1] = "`$ScriptVer = `"$NEWVER`"                                                ; Current Script Version Number"
$KIXOLD | set-content $tempfile1.kix

这失败

if(Test-Path $path)
{
    if(Test-Path "$pathFile1.exe")
    {
    #Check Old And new versions
    $NewVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$pathfile1.exe").FileVersion
    $OLDVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$FinalDestfile1.exe").FileVersion
    #Find the old version in the text file and replace with new
    #This FAILS unless there's a breakpoint or it's run separately
    $CONV = $KIXOLD | where {$_ -like "*If `$VAR<`"$OLDVersion*`""}
    ($kixold).Replace("$CONV", "         If `$VAR<`"$NewVersion`"") | set-content $Tempfile1.kix
    }  
}

这有效

#Update notes to contain current version - THIS WORKS
$linenum = select-string -Pattern $oldver -path $Tempfile1.kix | select -ExpandProperty LineNumber | select -Index 0
$NewLine = [int]$linenum +1
$KIXOLD[$linenum] = ";$NewVer - MyName"
$KIXOLD | set-content $tempfile1.kix

有人看过类似的东西,还是对为什么失败有任何想法?我已经在Windows Server 2012 R2和Windows 10框上进行了测试。

我再次问了这个问题,并得到了答案。对于任何寻找的人:

。应用于对象的重新定位方法不会更改该对象。使用

$CONV = $KIXOLD | where {$_ -like "*If `$VAR<`"1.2.3`""}
$KIXOLD = $kixold.Replace("$CONV", "         If `$VAR<`"1.2.4`"")
$KIXOLD | Set-Content $Temp

$CONV = $KIXOLD | where {$_ -like "*If `$VAR<`"1.2.3`""}
($kixold.Replace("$CONV", "         If `$VAR<`"1.2.4`"")) | Set-Content $Temp
$KIXOLD = (Get-Content $Temp)

最新更新