UserData依赖cloudbase-init,cloudbase-init依赖光驱,请不要禁用cloudbase-init服务或改动它、不要禁用光驱,不要删除光驱盘符,如果有安全软件,可能会拦截cloudbase-init,因为cloudbase-init通过python起作用,安全软件可能会认为这是风险项,因此有必要在安全软件里进行cloudbase-init的信任。
UserData产品文档:https://cloud.tencent.com/document/product/213/17526
UserData是RunInstances的一个参数,如果没勾选Base编码,那就是明文代码,如果勾选了,UserData就是一个String类型的参数,整段UserData代码需要base64编码后作为一行字符串传给UserData
买Windows机器的时候,以下代码粘到购买界面高级设置部分的自定义数据(userdata)里,下单后,后台会先准备机器硬件、灌入系统、开机,然后cloudbase-init服务启动、cloudbase-init执行完毕,整个过程需要时间,耐心等待10分钟再登录会发现已经自动安装好chrome浏览器了,如果操之过急,会报用户名密码错误或干脆就登录不了。
注意,我在代码里写密码了,请把密码换成你自己的。要通过userdata安装chrome,必须依赖explorer,要explorer,只能是登录状态下,所以要设置自动登录。
<powershell>
#以管理员身份运行
Set-ExecutionPolicy -Scope CurrentUser Unrestricted -force
if(-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){
$arguments = "& '" $myinvocation.mycommand.definition "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
#设置自动登录,在这里指定密码,只有进入explorer才能安装软件,设置自动登录进入explorer
net user Administrator "auNv4Pg5xpc="
echo "REGEDIT4" > c:temp.reg
echo "[HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionWinlogon]" >> c:temp.reg
echo "`"AutoAdminLogon`"=`"1`"">> c:temp.reg
echo "`"DefaultUserName`"=`"Administrator`"" >> c:temp.reg
echo "`"DefaultPassword`"=`"auNv4Pg5xpc=`"" >> c:temp.reg
cmd /c "reg import c:temp.reg"
del "c:temp.reg" 2>&1 > $null
#设置重启后要执行的脚本,执行完脚本自删除
{
{
powershell (new-object System.Net.WebClient).DownloadFile('http://windowscq-1251783334.cos.ap-chongqing.myzijiebao.com/ChromeSetup.exe','C:UsersAdministratorDesktopChromeSetup.exe') 2>&1 > $null | start "C:UsersAdministratorDesktopChromeSetup.exe" 2>&1 > $null
exit 2>$null
} | Out-File "c:chromesetup.ps1" -Width 1024 2>&1 > $null
Invoke-WebRequest -uri http://windowscq-1251783334.cos.ap-chongqing.myzijiebao.com/chromesetup.xml -OutFile c:chromesetup.xml
Register-ScheduledTask -xml (Get-Content 'c:chromesetup.xml' | Out-String) -TaskName chromesetup -TaskPath -force
start-sleep 180
del "c:chromesetup.xml" 2>&1 > $null
del "c:chromesetup.ps1" 2>&1 > $null
schtasks.exe /delete /tn "chromesetup" /F 2>&1 > $null
del "C:UsersAdministratorDesktopChromeSetup.exe" 2>&1 > $null
while( (Test-Path "C:UsersAdministratorDesktopChromeSetup.exe") -eq $True ){
del "C:UsersAdministratorDesktopChromeSetup.exe" 2>&1 > $null
start-sleep 5
}
Remove-Item $MyInvocation.MyCommand.Path -force 2>&1 > $null
exit 2>$null
} | Out-File "C:Program FilesCloudbase SolutionsCloudbase-InitLocalScriptsafterrestart.ps1" -Width 1024
reg add "HKLMSYSTEMCurrentControlSetControlNetworkNewNetworkWindowOff" /f 2>&1 > $null
restart-computer -force 2>$null
exit 2>$null
</powershell>
后来我又查了资料,有更简单的办法
<powershell>
$Path = "C:";
$Installer = "chrome_installer.exe";
$client = new-object System.Net.WebClient
$client.DownloadFile("http://dl.google.com/chrome/install/375.126/chrome_installer.exe","$Path$Installer")
Start-Process -FilePath $Path$Installer -Args "/silent /install" -Verb RunAs -Wait;
Remove-Item $Path$Installer
</powershell>
或者
<powershell>
$Path = "C:";
$Installer = "chrome_installer.exe";
Invoke-WebRequest "http://dl.google.com/chrome/install/375.126/chrome_installer.exe" -OutFile $Path$Installer;
Start-Process -FilePath $Path$Installer -Args "/silent /install" -Verb RunAs -Wait;
Remove-Item $Path$Installer
</powershell>
代码精简一些的话
代码语言:javascript复制<powershell>
$Path = "C:chrome_installer.exe";
(new-object System.Net.WebClient).DownloadFile("http://dl.google.com/chrome/install/375.126/chrome_installer.exe",$Path);
Start-Process -FilePath $Path -Args "/silent /install" -Verb RunAs -Wait;
Remove-Item $Path
</powershell>
代码语言:javascript复制<powershell>
$Path = "C:chrome_installer.exe";
Invoke-WebRequest "http://dl.google.com/chrome/install/375.126/chrome_installer.exe" -OutFile $Path;
Start-Process -FilePath $Path -Args "/silent /install" -Verb RunAs -Wait;
Remove-Item $Path
</powershell>