目标:编写Powershell脚本将文件从.txt
更改为.bat
,执行批处理文件,然后将其转换回.txt
。
我尝试过的事情:
Write-Host "Renaming $($PatchPath)$($FileName) to LivePatch.bat"
Rename-Item "$($PatchPath)$($FileName)" "LivePatch.bat"
Write-Host "Running $($PatchPath)LivePatch.bat"
Start-Process "$($PatchPath)LivePatch.bat" 3>&1 2>&1 > "$($PatchPath)\results.txt"
Write-Host "Returning $($PatchPath)LivePatch.bat to it's original name: $($FileName)."
Rename-Item "$($PatchPath)LivePatch.bat" "$($FileName)"
在$PatchPath = "D:\Patches\"
和$FileName = "LivePatch.txt
处。
问题: 从运行时查看资源管理器可以看到,这会将文件转换为批处理,但是当它尝试执行该文件时,它将返回以下内容:
'"D:\Patches\LivePatch.bat"' is not recognized as an internal or external command,
operable program or batch file.
我已经检查并确保可以执行批处理脚本,就像我试图使用已经存在且可以正常运行的批处理脚本执行上述操作一样。
旧的工作解决方案: 这是我一直用于此目的的批处理脚本,应该替换此powershell脚本:
REM rename %PatchPath%%FileName% "LivePatch.bat"
rename %PatchPath%%FileName% "LivePatch.bat"
REM Run Live Patch file %PatchPath%LivePatch.bat
REM call %PatchPath%LivePatch.bat > "%PatchPath%results.txt" 2>&1
call %PatchPath%LivePatch.bat > "%PatchPath%results.txt" & type "%PatchPath%results.txt"
rename "%PatchPath%LivePatch.bat" "%FileName%"
答案 0 :(得分:-1)
我将测试文件是否首先存在,而不是像您所说的那样依赖睡眠。
Write-Host "Renaming $($PatchPath)$($FileName) to LivePatch.bat"
Rename-Item "$($PatchPath)$($FileName)" "LivePatch.bat"
if([System.IO.File]::Exists("$($PatchPath)LivePatch.bat")){
Write-Host "Running $($PatchPath)LivePatch.bat"
Start-Process "$($PatchPath)LivePatch.bat" 3>&1 2>&1 > "$($PatchPath)\results.txt"
Write-Host "Returning $($PatchPath)LivePatch.bat to it's original name: $($FileName)."
Rename-Item "$($PatchPath)LivePatch.bat" "$($FileName)"
}