微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何制作sysprep镜像,确保同一镜像买的不同机器的SID不一样

制作sysprep镜像的步骤主要就2步:下载自动应答文件 → 执行sysprep命令

wget http://115.159.148.149/unattend_pro.xml -outfile c:\windows\qcloud_autounattend.xml

C:\Windows\System32\sysprep\sysprep.exe /shutdown /generalize /oobe /unattend:c:\windows\qcloud_autounattend.xml

注意事项:打开控制台vnc,在vnc里以管理员身份打开powershell命令行操作

以下是制作镜像的步骤,不限于制作sysprep镜像

1、用公共镜像2012R2/2016/2019/2022 买1台能访问公网的机器,因为后面执行命令时需要wget下载一些文件,买在支持dhcp的VPC里,一般2019年后创建的vpc都是支持dhcp的,50G系统盘就可以,不要加购数据盘

2、autounattend.xml放到C:\Windows\下

以powershell管理员身份执行这些命令,主要是起确保系统健壮性、消除不必要的日志的作用,也可根据自己需要选择是否要执行,理论上,这些命令都可以不执行

允许执行powershell的.ps1脚本,这步根据个人需要而定要不要去掉#

#Set-ExecutionPolicy Unrestricted -force

更新硬盘和网卡驱动到58007,更新驱动时如果弹窗,勾选始终信任(一般认就选好了),安装

更新驱动很有必要,这里wget下载的是适用2016、2019、2022、win10x64、win11的驱动完整驱动可在cvm里下载http://mirrors.tencentyun.com/install/cts/windows/Drivers.zip ,安装方法还是用pnputil -i -a 后面跟.inf的绝对路径,驱动升级可单独阅读这篇文档,https://cloud.tencent.com/developer/article/1927302

wget http://windowsnj-1251783334.cos.ap-nanjing.myqcloud.com/Win10_2016_2019.zip -outfile c:\drivers.zip
expand-archive -path "c:\drivers.zip" -destinationpath c:\drivers -force
pnputil -i -a c:\drivers\amd64\*.inf
cmd.exe /c rd /S /Q c:\drivers 2>&1 > $null

允许命令行远程执行命令

例如 wmic /node:"10.206.64.120" /USER:"administrator" /password:"密码" process call create "shutdown.exe -s -t 0"

winrm quickconfig -force
Set-Item WSMan:localhost\client\trustedhosts -value * -Force
netstat -ato|findstr :5985

关闭锁屏(不太确定能否涵盖各种情况)

powercfg -s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
powercfg -x -monitor-timeout-dc 0
powercfg -x -monitor-timeout-ac 0
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Personalization" /v "NoLockScreen" /d 1 /t REG_DWORD /f
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "disableLockWorkstation" /d 1 /t REG_DWORD /f
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Authentication\logonUI\SessionData" /v "AllowLockScreen" /d 0 /t REG_DWORD /f
schtasks.exe /create /tn "nolockscreen" /ru SYstem /rl highest /sc ONSTART /tr "reg add 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Authentication\logonUI\SessionData' /v AllowLockScreen /d 0 /t REG_DWORD /f" /f

清理原机器监控组件和安全组件的日志以使镜像买的新机器的平台组件日志是全新的,这步很有必要

stop-service BaradAgentSvc 2>$null 1>$null
stop-service StargateSvc 2>$null 1>$null
stop-service YDLive 2>$null 1>$null
stop-service YDService 2>$null 1>$null
del "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\cloudbase-init.log" 2>$null 1>$null
del "C:\Program Files\QCloud\Logs\*" 2>$null 1>$null
del "C:\Program Files\QCloud\Monitor\Barad\logs\*" 2>$null 1>$null
del "C:\Program Files\QCloud\Stargate\logs\*" 2>$null 1>$null
del "C:\Program Files\QCloud\YunJing\log\*" 2>$null 1>$null

安装telnet客户端,这步很有必要

install-windowsfeature telnet-client

做镜像前删除历史网卡记录,下次全新分配,这步很有必要

reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles" /f 2>&1 > $null
reg add "HKLM\SYstem\CurrentControlSet\Control\Network\NewNetworkWindowOff" /f 2>&1 > $null

优化cloudbase-init,这步很有必要


sc.exe config cloudbase-init obj= LocalSystem
sc.exe config cloudbase-init depend= tcpip/dhcp/dnscache/Winmgmt
sc.exe failure cloudbase-init reset= 0 actions= restart/10000 2>&1 > $null
net user cloudbase-init /del  2>&1 > $null
(Get-Content C:\Progra~1\Cloudb~1\Cloudbase-Init\conf\cloudbase-init.conf) -replace "allow_reboot=true","allow_reboot=false" | Set-Content C:\Progra~1\Cloudb~1\Cloudbase-Init\conf\cloudbase-init.conf -Verbos -force

下载sysprep的自动应答文件,这步根据需要而定

wget http://115.159.148.149/unattend_pro.xml -outfile c:\windows\qcloud_autounattend.xml

防止有些人禁用cloudbase-init、禁用Administrator引入不必要的麻烦,这里专门加几句命令

mkdir c:\windows\setup\scripts -force
write-host > C:\windows\setup\scripts\SetupComplete.cmd
"sc.exe config cloudbase-init start= auto" | Out-File -Append C:\windows\setup\scripts\SetupComplete.cmd
"sc.exe start cloudbase-init" | Out-File -Append C:\windows\setup\scripts\SetupComplete.cmd
"net user Administrator /active:yes" | Out-File -Append C:\windows\setup\scripts\SetupComplete.cmd

清理最近历史记录,这步很有必要

cmd.exe /c del /F /Q C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Recent\* 2>$null

清理系统.evtx日志,这步很有必要

wevtutil el | Foreach-Object {wevtutil cl "$_" 2>$null}
wevtutil cl security 2>$null
wevtutil cl system 2>$null
write-host "end"

3、cmd命令行执行下面这句sysprep命令后就耐心等待,会自动关机,然后直接制作镜像即可

一般业务不需要sysprep,如果必须sysprep,执行命令前去掉开头的#

一般业务不需要sysprep,如果必须sysprep,执行命令前去掉开头的#

一般业务不需要sysprep,如果必须sysprep,执行命令前去掉开头的#

#C:\Windows\System32\sysprep\sysprep.exe /shutdown /generalize /oobe /unattend:c:\windows\qcloud_autounattend.xml

4、如果执行了sysprep命令,回车后耐心等待若干分钟(一般2分钟左右就完事了)会自动关机;如果没执行sysprep,需要手动从开始按钮上右击选择关机,等完全关机后制作镜像

新机器键盘如果是中文(最新的公共镜像已经改成英文了,存量的中文键盘需要自己调整),这种极其讨厌,因为在vnc里粘贴英文代码时总会乱跳,有可能导致意外,比如重启、关机、大小写被切换、大写按键被开启,尤其要注意,密码不要设置为最后一个字母是大写字母的密码,这样的密码在vnc上粘贴时很麻烦,很容易就打开大写按键了),针对中文键盘的问题,我找了一个命令行改认英文键盘的办法,参考了https://web.archive.org/web/20130925063951/http://msdn.microsoft.com/en-us/goglobal/bb964650

wget http://windowscq-1251783334.cos.ap-chongqing.myqcloud.com/filename.xml -outfile c:\KeyboardInput_en-us.xml
cmd /c 'control intl.cpl,, /f:"%systemdrive%\KeyboardInput_en-us.xml"'

拓展:

自动生成autounattend.xml的网站可参考:https://www.windowsafg.com/

原文地址:https://cloud.tencent.com/developer/article/2007127

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐