Azure自定义脚本扩展--WindowsVM--重新启动计算机--启动时触发脚本
我很难在azure计算机(Windows Server 2012 R2数据中心)上自动安装应用程序 我的脚本分两步编写:Azure自定义脚本扩展--WindowsVM--重新启动计算机--启动时触发脚本,windows,azure,automation,cloud,Windows,Azure,Automation,Cloud,我很难在azure计算机(Windows Server 2012 R2数据中心)上自动安装应用程序 我的脚本分两步编写: 步骤1:安装.NET4.6.1,然后重新启动以完成安装 步骤2:安装我的应用程序 当机器发生故障时,我无法重新启动脚本。 有使用ScheduledTask或schtasks的技术,但只有当我使用远程桌面登录计算机时,这才是成功的。当脚本由Azure CustomScript Extension运行时,不会安排脚本重新启动 目标是运行ARM部署,该部署将使用CustomSc
- 步骤1:安装.NET4.6.1,然后重新启动以完成安装
- 步骤2:安装我的应用程序
$ Pstart = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$ Actionscript = "& `"C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.4\Downloads\0\MyScript.ps1`""
$ Action = New ScheduledTaskAction -execute $ pstart -argument $ actionscript
$ Trigger = New ScheduledTaskTrigger -AtStartUp
Register-ScheduledTask `
-TaskName $TaskName `
-Action $Action `
-Trigger $Trigger `
-RunLevel Highest `
-User "$Userdomain\$Username" `
-password $UncryptedPassword
schtasks /create `
/RU $username `
/PR $UncryptedPassword `
/SC ONSTART `
/TN $taskName `
/TR "'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' & `"C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.4\Downloads\0\MyScript.ps1`"" `
/RL HIGHEST
这是我的schtasks代码:
$ Pstart = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$ Actionscript = "& `"C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.4\Downloads\0\MyScript.ps1`""
$ Action = New ScheduledTaskAction -execute $ pstart -argument $ actionscript
$ Trigger = New ScheduledTaskTrigger -AtStartUp
Register-ScheduledTask `
-TaskName $TaskName `
-Action $Action `
-Trigger $Trigger `
-RunLevel Highest `
-User "$Userdomain\$Username" `
-password $UncryptedPassword
schtasks /create `
/RU $username `
/PR $UncryptedPassword `
/SC ONSTART `
/TN $taskName `
/TR "'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' & `"C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.4\Downloads\0\MyScript.ps1`"" `
/RL HIGHEST
这两个代码激活VM唤醒上的脚本,但仅当我通过远程桌面运行脚本时。
由AzureCustomScriptExtension执行时出现错误。当用户通过RDP连接时,任务运行时,我想到了配置文件加载。我发现这篇KB文章可能是Windows Server 2012 R2中的一个解决方案。为了能够重新启动计算机,我最终转到了DSC而不是customScript。由于DSC已经安装在windows虚拟机上,我认为这不是一个真正的解决办法 以下是DSC的代码:
# dscScript.ps1
Configuration InstallDotNet
{
Node "localhost"
{
LocalConfigurationManager
{
RebootNodeIfNeeded = $true
}
File Download_Directory
{
Ensure = "Present"
Type = "Directory"
DestinationPath = "C:\DeploymentDownloads"
}
Script Install_dotNet461
{
DependsOn = "[File]Download_Directory"
GetScript = { @{ Result = "" } }
TestScript = { $false }
SetScript = {
Write-Verbose "Install_dotNet461"
$client = New-Object -TypeName System.Net.WebClient
$url = "https://download.microsoft.com/download/E/4/1/E4173890-A24A-4936-9FC9-AF930FE3FA40/NDP461-KB3102436-x86-x64-AllOS-ENU.exe"
$dest = "C:\DeploymentDownloads\net461.exe"
for ($i=1; $i -le 10; $i++) {
try {
$client.DownloadFile($url, $dest)
}
catch {
$now = date
if ($i -eq 9) {
Write-Error "${$now} -- Error when downloading $url, exiting"
return
}
else {
Write-Warning "${$now} -- Error when downloading $url, attempting in a moment ..."
}
Start-Sleep $i * 2
}
}
$psi = new-object "Diagnostics.ProcessStartInfo"
$psi.FileName = $dest
$psi.Arguments = " /q"
$proc = [Diagnostics.Process]::Start($psi)
$proc.WaitForExit()
}
}
}
}
下面是ARM部署的代码(将该代码添加到您的VM中):
- provisionUrl是指向DSC zip文件的Url(ps1脚本位于zip的根目录下)李>
- 我直接使用与私有blob的sasToken关联的url(azure cli可以返回sasToken或带有sasToken的url),因为ARM sasToken选项似乎存在错误,并且没有在url和sasToken之间添加
?