$dir=$PSScriptRoot $MyCommand="$dir"+"\stlink\st-info.exe --probe --hot-plug" Invoke-Expression -Command $MyCommand | Out-String -OutVariable output $DevicesNum=$output.Substring($output.IndexOf("Found") + 7, 1) Write-Host -ForegroundColor Yellow -BackgroundColor Blue 'Do you wanna run the script N/Y' $key=Read-Host if ($key.ToUpper() -notmatch 'Y') { exit } if ($DevicesNum -eq 1) { $serial=$output.Substring($output.Trim().IndexOf("serial") + 12, 24) } elseif ($DevicesNum -eq 0) { exit } else { Write-Host -ForegroundColor Yellow -BackgroundColor Blue 'Press key which number of debugger you wanna use' $key=Read-Host $res=$output.Split('.')[$key] $res=$res.Substring(0, $res.Length-2) $serial=$res.Substring($res.IndexOf("serial") + 12 , 24) Write-Host -ForegroundColor Yellow -BackgroundColor Blue "Your select is : `r`n $res" } $serial=$serial.ToCharArray() $StringForCompare='adapter serial "' for ($i = 0; $i -lt $serial.Count; $i=$i+2) { $StringForCompare=$StringForCompare.Insert($StringForCompare.Length,"$($serial.Get($i))$($serial.Get($i+1))") } $StringForCompare=$StringForCompare.Insert($StringForCompare.Length,'"') $Path = "$dir"+"/board.cfg" $Lines = [System.IO.File]::ReadAllLines($Path) foreach ($line in $lines) { if ($line -match "adapter serial") { if ($line -notmatch $StringForCompare) { $Lines[$Lines.IndexOf($line)]=$StringForCompare } [System.IO.File]::WriteAllLines($Path, $Lines) exit } } $Lines[0]=$StringForCompare [System.IO.File]::WriteAllLines($Path, $Lines)