我在自动在azure计算机上安装应用程序时遇到了困难(Windows Server 2012 R2数据中心)。
我的脚本分两步编写:
机器维护时我无法重新启动脚本。
有些技术使用 ScheduldedTask 或 schtasks ,但只有当我使用远程桌面在计算机上登录时,它才会成功。当Azure CustomScript Extension运行脚本时,不会对脚本进行重新调整。
目标是运行ARM部署,该部署将使用CustomScript Extension安装我的应用程序,而无需我的任何操作。
以下是使用ScheduldedTask创建唤醒任务的代码:
$ 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:
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执行时会出现错误。
答案 0 :(得分:1)
当用户通过RDP连接时,任务运行,这让我想到了配置文件加载。我发现KB文章https://support.microsoft.com/en-us/kb/2968540可能是Windows Server 2012 R2中的解决方案。
答案 1 :(得分:1)
我终于转移到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):
"resources": [
{
"type": "extensions",
"name": "dscExtension",
"apiVersion": "2015-05-01-preview",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
],
"properties": {
"publisher": "Microsoft.Powershell",
"type": "DSC",
"typeHandlerVersion": "2.9",
"autoUpgradeMinorVersion": "true",
"settings": {
"modulesUrl": "[parameters('provisionUrl')]",
"privacy": {
"DataCollection": "Disable"
},
"configurationFunction": "dscScript.ps1\\InstallDotNet",
"properties": {
"EventstoreLinqpadScriptUrl": "[parameters('provisionEsScriptUrl')]"
}
}
}
}
]
?
sasToken。