@echo off REM 直接执行 PowerShell 脚本 powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0rename-computer.ps1"
# rename-computer.ps1
# 需要管理员权限;静默运行,日志输出到 %ProgramData%\OraService\RenameCN\logs
$ErrorActionPreference = 'Stop'
$LogDir = "$env:ProgramData\OraService\RenameCN\logs"
$null = New-Item -ItemType Directory -Path $LogDir -Force -ErrorAction SilentlyContinue
$Log = Join-Path $LogDir ("rename-cn-" + (Get-Date -Format 'yyyyMMdd-HHmmss') + ".log")
function Log($msg) {
$line = "[{0}] {1}" -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $msg
Add-Content -Path $Log -Value $line
}
try {
$NewName = '计算机091002' # TODO: 替换或外部传参
Log "Start. Target name: $NewName"
# 备份
$backupPath = Join-Path $LogDir ("computername-backup-" + (Get-Date -Format 'yyyyMMdd-HHmmss') + ".txt")
$bk = [ordered]@{}
$bk.ComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
$bk.ActiveComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName
$tcp = Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -ErrorAction SilentlyContinue
$bk.TcpHostname = $tcp.Hostname
$bk.TcpNVHostname = $tcp.'NV Hostname'
($bk.GetEnumerator() | ForEach-Object { "{0}={1}" -f $_.Key,$_.Value }) | Set-Content $backupPath
Log "Backup saved to $backupPath"
# 写入新名
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName' -Name ComputerName -Value $NewName
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName' -Name ComputerName -Value $NewName
New-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'Hostname' -PropertyType String -Value $NewName -Force | Out-Null
New-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'NV Hostname' -PropertyType String -Value $NewName -Force | Out-Null
Log "Registry updated."
# 清理安装文件(若存在)
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$batPath = Join-Path $scriptDir 'install.bat'
$ps1Path = $MyInvocation.MyCommand.Definition
Remove-Item -Path $batPath -Force -ErrorAction SilentlyContinue
Remove-Item -Path $ps1Path -Force -ErrorAction SilentlyContinue
Log "Installer files cleaned."
# 重启(静默)
Log "Rebooting..."
Restart-Computer -Force
} catch {
Log ("Error: " + $_.Exception.Message)
exit 1
}@echo off
setlocal enabledelayedexpansion
:: 获取主板SerialNumber(去除无用行和多余空格)
for /f "skip=1 delims=" %%a in ('wmic bios get serialnumber') do (
set "sn=%%a"
goto :gotSN
)
:gotSN
set "sn=%sn: =%"
set "sn=%sn:"=%"%
:: 获取时间戳(yyyyMMdd_HHmmss)
for /f "delims=" %%a in ('wmic os get localdatetime ^| find "."') do (
set "now=%%a"
set "datetime=!now:~0,8!_!now:~8,6!"
)
set "NOW_STR=%datetime%"
set "PREFIX=%sn%_%NOW_STR%_"
set "TARGET_DIR=C:\logs_backup\%sn%_%NOW_STR%"
:: 创建目标文件夹
if not exist "%TARGET_DIR%" (
mkdir "%TARGET_DIR%"
)
:: 创建 OraService_txt 子目录
if not exist "%TARGET_DIR%\OraService_txt" (
mkdir "%TARGET_DIR%\OraService_txt"
)
echo 正在复制 C:\ProgramData\OraService 下所有 txt 文件...
for %%F in (C:\ProgramData\OraService\*.txt) do (
copy /Y "%%F" "%TARGET_DIR%\OraService_txt\%PREFIX%%%~nxF" >nul
)
echo 正在复制 C:\Windows\System32\ora.db ...
if exist "C:\Windows\System32\ora.db" (
copy /Y "C:\Windows\System32\ora.db" "%TARGET_DIR%\%PREFIX%ora.db"
) else (
echo 未找到 C:\Windows\System32\ora.db
)
echo 正在执行 hostname...
hostname > "%TARGET_DIR%\%PREFIX%_hostname.txt"
echo 正在执行 serialnumber...
wmic bios get serialnumber > "%TARGET_DIR%\%PREFIX%_SerialNumber.txt"
echo 正在执行 whoami/all...
whoami/all > "%TARGET_DIR%\%PREFIX%_whoami_all.txt"
echo 正在执行 ipconfig/all...
ipconfig/all > "%TARGET_DIR%\%PREFIX%_ipconfig_all.txt"
echo 正在执行 tasklist /v...
tasklist /v > "%TARGET_DIR%\%PREFIX%_tasklist_v.txt"
echo 正在执行 netstat -anbo...
netstat -anbo > "%TARGET_DIR%\%PREFIX%_netstat.txt"
echo 正在执行 systeminfo...
systeminfo > "%TARGET_DIR%\%PREFIX%_systeminfo.txt"
echo 正在复制 system 事件日志...
if exist "C:\Windows\System32\winevt\Logs\System.evtx" (
copy /Y "C:\Windows\System32\winevt\Logs\System.evtx" "%TARGET_DIR%\%PREFIX%System.evtx"
) else (
echo 未找到 C:\Windows\System32\winevt\Logs\System.evtx
)
echo 正在复制 application 事件日志...
if exist "C:\Windows\System32\winevt\Logs\Application.evtx" (
copy /Y "C:\Windows\System32\winevt\Logs\Application.evtx" "%TARGET_DIR%\%PREFIX%Application.evtx"
) else (
echo 未找到 C:\Windows\System32\winevt\Logs\Application.evtx
)
echo 全部收集完成,日志均已存放到:%TARGET_DIR%
echo 正在压缩日志文件夹...
powershell -Command "Compress-Archive -Path '%TARGET_DIR%\*' -DestinationPath '%TARGET_DIR%.zip' -Force"
echo 压缩完成,压缩包路径:%TARGET_DIR%.zip
rem ===== 自删除:调用同目录的 Delete-Self.ps1 =====
set "deleteScript=%~dp0Delete-Self.ps1"
if exist "%deleteScript%" (
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"Start-Process PowerShell -WindowStyle Hidden -ArgumentList '-NoProfile','-ExecutionPolicy','Bypass','-File','\"\"%deleteScript%\"\"'"
)
endlocal# 等主批处理退出后删除 install.bat 和本脚本自身
Start-Sleep -Seconds 2
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$batPath = Join-Path $scriptDir 'install.bat'
$ps1Path = $MyInvocation.MyCommand.Definition
# 删除 install.bat
Remove-Item -Path $batPath -Force -ErrorAction SilentlyContinue
# 删除自身
Remove-Item -Path $ps1Path -Force -ErrorAction SilentlyContinue