Azure自定义脚本扩展程序不断重新启动

时间:2016-07-21 18:10:24

标签: windows powershell azure

在Windows VM上使用Azure自定义脚本扩展时遇到一些问题。我创建了一个安装特定业务应用程序的脚本。为了实现这一点,脚本会调用其他几个脚本,并且需要在两个脚本之间重新启动。

脚本的设计使得它可以“捡起它停在的地方”。通过检查某些文件是否存在或是否存在注册表项。重启部分很好,但是,我注意到脚本处理程序设置了几个注册表项以跟踪其状态

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows Azure \ ScriptHandler

我看到一个名为" MreSeqNumStarted"的注册表值,我认为这是脚本处理程序如何跟踪它执行的脚本。当前只有一个脚本由扩展名触发,并且此值设置为0。

我所看到的是,在脚本完成后重新启动计算机时,脚本会再次触发。每次重新启动虚拟机时都会发生这种情况。我相信脚本扩展不会将脚本标记为"已完成",而且我有点不知道为什么。脚本的基本(已消毒)流程如下:

trap {$_.Exception.Message; Get-PSCallstack;continue}

if(Test-Path C:\ScriptLog.txt)
{
    Add-Content "C:\ScriptLog.txt" "`nScript Previously Run, Prior log exists."
} else {
    Write-Host "Begin Script" | Out-File "C:\ScriptLog.txt"
}

#$storagePath = [uri]::EscapeDataString($storagePath)
#Create Directory Structure and download scripts/files
if(Test-Path C:\PreparePlatform\) {
    Add-Content "C:\ScriptLog.txt" "`nFiles Already Downloaded... Continuing"
} else {
    New-Item -ItemType Directory -Force -Path C:\SoftwareName\PreparePlatform\
    (New-Object Net.WebClient).DownloadFile('SAS-URL-GOES-HERE','C:\SoftwareName\PreparePlatform\PreparePlatform.zip');(new-object -com shell.application).namespace('C:\SoftwareName\PreparePlatform').CopyHere((new-object -com shell.application).namespace('C:\SoftwareName\PreparePlatform\PreparePlatform.zip').Items(),16)
    (New-Object Net.WebClient).DownloadFile('SAS-URL-GOES-HERE','C:\SoftwareName\SOMEZIP.zip');(new-object -com shell.application).namespace('C:\SoftwareName').CopyHere((new-object -com shell.application).namespace('C:\SoftwareName\SOMEZIP.zip').Items(),16)
    Add-Content "C:\ScriptLog.txt" "`nExtracted Install Media"
}

if(Test-Path C:\SoftwareName\preparedone.txt)   {
    Add-Content "C:\ScriptLog.txt" "`nPreparePlatform already completed... Continuing"
} else {
    Invoke-Expression "Set-Location c:\SoftwareName\PreparePlatform\; C:\SoftwareName\PreparePlatform\Prepare-WindowsPlatform.ps1"
    CreateSQLUser -identifierValue $value -saPassword $sqlSaPassword -tenantPass $TenantSqlPassword
    CreateSQLLogin -identifierValue $value -saPassword $SqlSaPassword -tenantPass $TenantSqlPassword
    Write-Host "PreparePlatform Complete" | Out-File "C:\SoftwareName\preparedone.txt"
}
if(Test-Path C:\SoftwareName\UpdatePlatform.txt)    {
    Add-Content "C:\ScriptLog.txt" "`nUpdatePlatform already completed... Continuing"
} else {
    Add-Content "C:\ScriptLog.txt" "`nBegin Update-Platform"
    Invoke-Expression "Set-Location C:\SoftwareName\PreparePlatform\; C:\SoftwareName\PreparePlatform\Update-WindowsPlatform.ps1"
    Write-Host "UpdatePlatform Complete" | Out-File "C:\SoftwareName\UpdatePlatform.txt"
}

if(Test-Path C:\SoftwareName\Rebooted.txt)  {
    Add-Content "C:\ScriptLog.txt" "`nReboot already completed... Continuing"
} else {
    Write-Host "Rebooting" | Out-File "C:\SoftwareName\Rebooted.txt"
    Restart-Computer -Force
    Restart-Computer -Force
    Start-Sleep 3600 #Make sure nothing else executes
}


#  TODO:  Make more robust for potentially more nodes... need to delay 1 hour for each node
If($env:computername -eq 'pvm-SoftwareName-1') { Write-Output "Second Node, waiting 1 hour"; Add-Content "C:\ScriptLog.txt" "`nNot First Node, waiting 1 hour" ;Start-Sleep -Seconds 3600 }


Add-Content "C:\ScriptLog.txt" "`nPrepare SoftwareName Installers"
Set-Location C:\SoftwareName\Script\; C:\SoftwareName\Script\Prepare-SoftwareNameInstallers.ps1 -neededParameters $values
Add-Content "C:\ScriptLog.txt" "`nPrepare SoftwareName Installers complete"

try
{
        if(-not (Get-Service -Name 'SoftwareName server')) {
            C:\SoftwareName\Script\Install-SoftwareNameBP.ps1 -neededParameters $values
        } else {
            Add-Content "C:\ScriptLog.txt" "`n ALREADY INSTALLED!"
        }

}
catch
{
    $_.Exception.Message;Get-PSCallStack
    if (-not (Get-Service -Name 'SoftwareName Server').Status -eq 'Running') 
    {throw "Service unavailable"}

}
Write-Output "Install Finished."
try
{
    if(-not ((Invoke-WebRequest -Uri https://IISSiteURL/).StatusCode -eq '200'))
    C:\SoftwareName\Script\Install-SoftwareNameSF.ps1 -neededParameters $values
}
catch
{
    $_.Exception.Message;Get-PSCallStack
    if(-not (Test-Path -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{AGUID}") )
    {throw "Smartforms Install failed"}

}
try
{
    #Begin
    C:\SoftwareName\Script\Install-SoftwareNameSP.ps1 -neededParameters $values
}
catch
{
    $_.Exception.Message;Get-PSCallStack
    if(-not (Test-Path -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{AGUID}") )
    {throw "Sharepoint Install failed"}
}
    try
{
    #[bool]$skipDeploy
    #if($env:computername -eq 'pvm-SoftwareName-0') { $skipDeploy = $false } else { $skipDeploy = $true }

    #Begin
    C:\SoftwareName\Script\Install-SoftwareNameMG.ps1  -neededParameters $values
catch
{
    $_.Exception.Message;Get-PSCallStack
    if(-not (Test-Path -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{AGUID}") )
    {throw "Management Install failed"}
}
        try
{

    C:\SoftwareName\Script\Install-Finalize.ps1  -neededParameters $values
}
catch
{
    $_.Exception.Message;Get-PSCallStack
    if(-not (Test-Path -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{AGUID}") )
    {throw "Install Finalize Failed"}
}

#Kill the script
Add-Content "C:\ScriptLog.txt" "`nScript Completed"
Exit

以下是C:\ ScriptLog.txt

的预期输出
Begin Scheduled Job Regstration
Extracted Install Media
Begin PreparePlatform
Completed PreparePlatform
Begin Update-Platform
Script Previously Run, Prior log exists.
Files Already Downloaded... Continuing
PreparePlatform already completed... Continuing
UpdatePlatform already completed... Continuing
Reboot already completed... Continuing
Prepare SOFTWARENAME Installers
Prepare SOFTWARENAME Installers complete
C:\SOFTWARENAME\Script\Install-K2BP.ps1 -parameterValues $values
C:\SOFTWARENAME\Script\Install-K2SF.ps1 -parameterValues $values
C:\SOFTWARENAME\Script\Install-K2SP.ps1 -parameterValues $values
C:\SOFTWARENAME\Script\Install-K2MG.ps1 -parameterValues $values
C:\SOFTWARENAME\Script\Install-Finalize.ps1 -parameterValues $values
Script Completed

重启后,相同的日志文件:

Begin Scheduled Job Regstration
Extracted Install Media
Begin PreparePlatform
Completed PreparePlatform
Begin Update-Platform
Script Previously Run, Prior log exists.
Files Already Downloaded... Continuing
PreparePlatform already completed... Continuing
UpdatePlatform already completed... Continuing
Reboot already completed... Continuing
Prepare SOFTWARENAME Installers
Prepare SOFTWARENAME Installers complete
C:\SOFTWARENAME\Script\Install-SOFTWARENAMEBP.ps1 -neededParameters $values
C:\SOFTWARENAME\Script\Install-SOFTWARENAMESF.ps1 -neededParameters $values
C:\SOFTWARENAME\Script\Install-SOFTWARENAMESP.ps1 -neededParameters $values
C:\SOFTWARENAME\Script\Install-SOFTWARENAMEMG.ps1 -neededParameters $values
C:\SOFTWARENAME\Script\Install-Finalize.ps1 -neededParameters $values
Script Completed
Script Previously Run, Prior log exists.
Files Already Downloaded... Continuing
PreparePlatform already completed... Continuing
UpdatePlatform already completed... Continuing
Reboot already completed... Continuing
Prepare SOFTWARENAME Installers
Prepare SOFTWARENAME Installers complete
C:\SOFTWARENAME\Script\Install-SOFTWARENAMEBP.ps1 -neededParameters $values...

Edit1:我已经注释掉了对外部脚本的所有调用。此时脚本仅将输出写入文件effectivley并捕获任何异常。每次重新启动VM时,脚本都会继续重新启动。

0 个答案:

没有答案