前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Windows UserData专题:安装Chrome

Windows UserData专题:安装Chrome

原创
作者头像
Windows技术交流
修改2022-12-20 21:02:17
7310
修改2022-12-20 21:02:17
举报
文章被收录于专栏:Windows技术交流

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_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]" >> 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.myqcloud.com/ChromeSetup.exe','C:\Users\Administrator\Desktop\ChromeSetup.exe') 2>&1 > $null | start "C:\Users\Administrator\Desktop\ChromeSetup.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.myqcloud.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:\Users\Administrator\Desktop\ChromeSetup.exe" 2>&1 > $null

while( (Test-Path "C:\Users\Administrator\Desktop\ChromeSetup.exe") -eq $True ){

del "C:\Users\Administrator\Desktop\ChromeSetup.exe" 2>&1 > $null

start-sleep 5

}

Remove-Item $MyInvocation.MyCommand.Path -force 2>&1 > $null

exit 2>$null

} | Out-File "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\LocalScripts\afterrestart.ps1" -Width 1024

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Network\NewNetworkWindowOff" /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>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 后来我又查了资料,有更简单的办法
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档