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

Windows Azure自定义脚本扩展不断重新启动,windows,powershell,azure,Windows,Powershell,Azure,windows VM上的Azure自定义脚本扩展有点问题。我创建了一个脚本,用于安装特定的业务应用程序。为了实现这一点,该脚本调用其他几个脚本,并且需要在两个脚本之间重新启动 该脚本的设计使得它可以通过检查某些文件是否存在或注册表项是否存在来“从中断的地方开始”。不过,我注意到,脚本处理程序设置了几个注册表项,以跟踪脚本中的状态 HKEY\ U本地\计算机\软件\ Microsoft\Windows Azure\ScriptHandler 我看到一个名为“MreSeqNumStarted”的注册

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

该脚本的设计使得它可以通过检查某些文件是否存在或注册表项是否存在来“从中断的地方开始”。不过,我注意到,脚本处理程序设置了几个注册表项,以跟踪脚本中的状态

HKEY\ U本地\计算机\软件\ 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重新启动时,脚本都会继续重新启动