
下方代码另存为fix.ps1在views文件夹,在views文件夹里面执行powershell -ExecutionPolicy Bypass -File .\fix.ps1# PowerShell 脚本备份和修改 Shared 文件夹中的 cshtml 文件 # 兼容 PowerShell 5.1 和 PowerShell 7 # 设置当前目录 $currentPath Get-Location # 创建 bak 文件夹如果不存在 $bakPath Join-Path $currentPath bak if (-not (Test-Path $bakPath)) { New-Item -Path $bakPath -ItemType Directory | Out-Null Write-Host 已创建 bak 文件夹 -ForegroundColor Green } # 获取当前目录下所有文件夹排除 bak 文件夹 $folders Get-ChildItem -Path $currentPath -Directory | Where-Object { $_.Name -ne bak } foreach ($folder in $folders) { $folderName $folder.Name $sharedPath Join-Path $folder.FullName Shared # 检查是否存在 Shared 文件夹 if (Test-Path $sharedPath) { Write-Host n处理文件夹: $folderName -ForegroundColor Cyan # 定义要处理的文件 $files ( 网站搜索.cshtml, 网站搜索-首页.cshtml, 网站顶部.cshtml ) # 创建备份目录结构 $bakFolderPath Join-Path $bakPath $folderName $bakSharedPath Join-Path $bakFolderPath Shared if (-not (Test-Path $bakSharedPath)) { New-Item -Path $bakSharedPath -ItemType Directory -Force | Out-Null } # 处理每个文件 foreach ($fileName in $files) { $sourceFile Join-Path $sharedPath $fileName if (Test-Path $sourceFile) { # 先备份原始文件 $bakFile Join-Path $bakSharedPath $fileName Copy-Item -Path $sourceFile -Destination $bakFile -Force Write-Host 已备份: $fileName -ForegroundColor Yellow # 逐行读取文件兼容方式 try { $lines (Get-Content -Path $sourceFile -Encoding UTF8 -ErrorAction Stop) } catch { # 如果 UTF8 失败尝试默认编码 $lines (Get-Content -Path $sourceFile -ErrorAction Stop) } $modified $false # 处理网站搜索.cshtml 和 网站搜索-首页.cshtml if ($fileName -eq 网站搜索.cshtml -or $fileName -eq 网站搜索-首页.cshtml) { $newLines New-Object System.Collections.ArrayList foreach ($line in $lines) { $skipLine $false # 1. 删除包含 var keyword Context.Request.Query(wd) 或 Get(wd) 的行 if ($line -match var\skeyword\s*\s*Context\.Request\.(Get|Query)\s*\(\s*wd\s*\)) { $skipLine $true $modified $true Write-Host 已删除行: $($line.Trim()) -ForegroundColor Green } # 2. 替换 idinputkeyword 或 idtopKeyWord 的 input 标签中的 valuekeyword 为 value if (-not $skipLine) { # 检查是否包含目标 id 和 valuekeyword if (($line -match id\s*\s*inputkeyword -or $line -match id\s*\s*topKeyWord) -and $line -match value\s*\s*keyword) { $originalLine $line $line $line -replace (value\s*\s*)keyword(), $1$2 if ($originalLine -ne $line) { $modified $true $idName if ($originalLine -match id\s*\s*inputkeyword) { inputkeyword } else { topKeyWord } Write-Host 已修改 value 属性: id$idName -ForegroundColor Green } } [void]$newLines.Add($line) } } # 保存修改后的内容 if ($modified) { $newLines | Set-Content -Path $sourceFile -Encoding UTF8 } } # 处理网站顶部.cshtml if ($fileName -eq 网站顶部.cshtml) { try { $content Get-Content -Path $sourceFile -Raw -Encoding UTF8 -ErrorAction Stop } catch { $content Get-Content -Path $sourceFile -Raw -ErrorAction Stop } # 替换 Power.QrCode(任何参数) 为 Power.QrCode() if ($content -match Power\.QrCode\s*\([^)]\)) { $content $content -replace Power\.QrCode\s*\([^)]\), Power.QrCode(null,100) # 兼容 PowerShell 5.1 - 使用 [System.IO.File]::WriteAllText 替代 Set-Content -NoNewline [System.IO.File]::WriteAllText($sourceFile, $content, [System.Text.Encoding]::UTF8) $modified $true Write-Host 已替换 QrCode 方法: $fileName -ForegroundColor Green } } if (-not $modified) { Write-Host 未找到需要修改的内容: $fileName -ForegroundColor DarkGray } } else { Write-Host 文件不存在: $fileName -ForegroundColor Gray } } } else { Write-Host 文件夹 $folderName 中不存在 Shared 文件夹 -ForegroundColor Gray } } Write-Host n脚本执行完成 -ForegroundColor Green Write-Host 原始文件已备份到 bak 文件夹中 -ForegroundColor Cyan