我有一个Web / Worker角色,这些角色具有自定义启动脚本以在角色启动时执行,以确保安装了所需的.NET版本。
云服务的ServiceDefinition.csdef 文件:
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MyServices" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
<WebRole name="WebApiRole" vmsize="Standard_D1">
<Sites>
<Site name="Web" physicalDirectory="..\..\..\WebApiRole\">
<Bindings>
<Binding name="HttpIn" endpointName="HttpIn" />
<Binding name="Endpoint1" endpointName="HttpsIn" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="HttpIn" protocol="http" port="80" />
<InputEndpoint name="HttpsIn" protocol="https" port="443" certificate="myservices.com" />
</Endpoints>
<Certificates>
<Certificate name="myservices.com" storeLocation="LocalMachine" storeName="CA" />
</Certificates>
<LocalResources>
<LocalStorage name="InstallLogs" sizeInMB="5" cleanOnRoleRecycle="false" />
</LocalResources>
<Startup>
<Task commandLine="install.cmd" executionContext="elevated" taskType="simple">
<Environment>
<Variable name="PathToInstallLogs">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='InstallLogs']/@path" />
</Variable>
</Environment>
</Task>
</Startup>
<Imports>
<Import moduleName="RemoteAccess" />
<Import moduleName="RemoteForwarder" />
</Imports>
</WebRole>
<WorkerRole name="WorkerRole" vmsize="Standard_D1">
<LocalResources>
<LocalStorage name="CustomTempLocalStore" sizeInMB="2048" cleanOnRoleRecycle="true" />
<LocalStorage name="InstallLogs" sizeInMB="5" cleanOnRoleRecycle="false" />
</LocalResources>
<Startup>
<Task commandLine="install.cmd" executionContext="elevated" taskType="simple">
<Environment>
<Variable name="PathToInstallLogs">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='InstallLogs']/@path" />
</Variable>
</Environment>
</Task>
</Startup>
<Imports>
<Import moduleName="RemoteAccess" />
</Imports>
</WorkerRole>
</ServiceDefinition>
据我所见,运行install.cmd
的启动任务是根据microsoft docs进行的。
我可以确认部署后install.cmd
已部署在根文件夹-AppRoot\Bin
中。
如果我在云服务VM上手动执行install.cmd
,它将按预期运行。
但是,它没有在部署时运行,因此Web /工作人员角色一直在回收(由于缺少依赖于代码的.NET版本)。
作为参考,这是install.cmd
REM Set the value of netfx to install appropriate .NET Framework.
REM ***** To install .NET 4.5.2 set the variable netfx to "NDP452" *****
REM ***** To install .NET 4.6 set the variable netfx to "NDP46" *****
set netfx="NDP48"
REM ***** Setup .NET filenames and registry keys *****
if %netfx%=="NDP48" goto NDP48
if %netfx%=="NDP46" goto NDP46
set netfxinstallfile="NDP452-KB2901954-Web.exe"
set netfxregkey="0x5cbf5"
goto logtimestamp
:NDP46
set netfxinstallfile="NDP46-KB3045560-Web.exe"
set netfxregkey="0x60051"
goto logtimestamp
:NDP48
set netfxinstallfile="ndp48-web.exe"
set netfxregkey="0x80eb1"
goto logtimestamp
:logtimestamp
REM ***** Setup LogFile with timestamp *****
set timehour=%time:~0,2%
set timestamp=%date:~-4,4%%date:~-10,2%%date:~-7,2%-%timehour: =0%%time:~3,2%
set startuptasklog=%PathToInstallLogs%startuptasklog-%timestamp%.txt
set netfxinstallerlog=%PathToInstallLogs%NetFXInstallerLog-%timestamp%
echo Logfile generated at: %startuptasklog% >> %startuptasklog%
REM ***** Check if .NET is installed *****
echo Checking if .NET (%netfx%) is installed >> %startuptasklog%
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v Release | Find %netfxregkey%
if %ERRORLEVEL%== 0 goto end
REM ***** Installing .NET *****
echo Installing .NET. Logfile: %netfxinstallerlog% >> %startuptasklog%
start /wait %~dp0%netfxinstallfile% /q /serialdownload /log %netfxinstallerlog% >> %startuptasklog% 2>>&1
if %ERRORLEVEL%== 0 goto installed
echo .NET installer exited with code %ERRORLEVEL% >> %startuptasklog%
if %ERRORLEVEL%== 3010 goto restart
if %ERRORLEVEL%== 1641 goto restart
echo .NET (%netfx%) install failed with Error Code %ERRORLEVEL%. Further logs can be found in %netfxinstallerlog% >> %startuptasklog%
goto exit
:restart
echo Restarting to complete .NET (%netfx%) installation >> %startuptasklog%
shutdown.exe /r /t 5 /c "Installed .NET framework" /f /d p:2:4
:end
echo install.cmd completed: %date:~-4,4%%date:~-10,2%%date:~-7,2%-%timehour: =0%%time:~3,2% >> %startuptasklog%
:exit
EXIT /B 0
我可以确认可以在ndp48-web.exe
的根文件夹下找到install.cmd
可执行文件。
正如我所说,脚本不会在启动时执行(我在Event Viewer中找不到任何日志,也找不到任何条目)。
任何线索这有什么问题吗?
答案 0 :(得分:2)
错误在install.cmd
脚本中。
在使用here文档中的脚本时,我遇到了同样的问题。
如果仔细查看,日志文件的名称末尾带有%timestamp%
。这会使文件名的格式错误。它有/
。因此,脚本第一次尝试写入日志文件时,会收到错误:The system cannot find the path specified.
成功结束脚本,但不执行任何操作。它不会创建任何日志,也不会安装.NET Framework。
我的解决方案是通过将%timestamp%
变量替换为我使用以下脚本计算的%dtStamp%
来更改时间戳格式:
@ECHO OFF
SET HOUR=%time:~0,2%
SET dtStamp9=%date:~-4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
SET dtStamp24=%date:~-4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if "%HOUR:~0,1%" == " " (SET dtStamp=%dtStamp9%) else (SET dtStamp=%dtStamp24%)
@ECHO ON
echo timestamp: %dtStamp%