Search code examples
powershellazurecmdstartup

Startup Task not running on Azure Cloud Service role


I'm having difficulties trying to setup a startup task in an Azure role. The ultimate goal is to disable RC4 cipher, along with other SSL configurations. In my (VS2012Express) project (solution partially achieved following another answer here in SO that led me to https://gist.github.com/sidshetye/29d6d48dfa0c2f5488a4 ) I created a Startup.cmd file like this:

# Execute powershell command to disable RC4 and imporve SSL security settings
ECHO Batch started >> "StartupLog.txt" 2>&1
PowerShell -ExecutionPolicy Unrestricted .\HardenSSL.ps1 >> log-   HardenSSL.txt 2>&1
EXIT /B 0

HardenSSL.ps1 is the PowerShell script from the previous link. Both the .cmd and .ps1 scripts are placed in the application root directory, marked as "Content" with properties set to "CopyLocal=Always".

In my service definition, I put this:

<Startup>
   <Task commandLine="Startup.cmd" executionContext="elevated" taskType="background"></Task>
</Startup>

Now, when I deploy the application to Azure, "nothing" happens. I configured the role instance to allow remote desktop, connected to the machine. I verified the scripts where published, and there were no log files, RC4 still enabled. I tried to manually run the .cmd and the machine runs the scripts to completion, disables RC4 and restarts. So the scripts are actually "correct".

The problem is that the scripts are not getting fired up at startup. I may be wrong, but I don't see anything related looking Windows events. Actually, the server now keeps all the configurations, but I have to be sure the scripts get executed in case I'll have to publish to new instances/cloud services.

I also tried to: 1. place the scripts on a child directory 2. create other 2 "simpler" .cmd that just create a log file with "script started" to exclude problems related to the .cmd calling the PowerShell script. None of those scripts got executed.

Hope I've been sufficiently clear, any help would be greatly appreciated.

Thank you in advance,

Alberto

UPDATE 1

Reading through various discussions, I missed one very important thing: the script files are actually published in 2 distinct places, one being inside the /bin folder.

Ex: I placed my scripts in a /StartupScripts folder in my project, and when I connect via Remote Desktop to the Azure server I find the scripts both in "approot/StartupScripts" and in "approot/bin/StartupScripts".

The scripts the are actually executing are those placed inside the "bin" folder. the real problem is that I have probably a path problem inside the .cmd since I now found the execution logs with an error.

Now I will try to change it up and update the question here on SO.


Solution

  • Ok.

    In the end it was indeed a problem with a path in my Startup.cmd file: .\HardenSSL.ps1 could not be found if the StartUp Task pointed to a subfolder.

    Solution was to place both Startup.cmd and HardenSSL.ps1 files in the application root, remove the ".\" part when calling the PowerShell Script and all worked well.

    Anyway, I would like to suggest anyone to pick this other solution I found in stack exchage: https://security.stackexchange.com/a/79957

    It links to a NuGet package that does the same thing as the script I found on the link to github in the original post, just "better"; mainly:

    • Better configuration of cipher suites, with support for ForwardSecrecy for all reference browsers on SSLLabs
    • Retain SSL support for Internet Explorer 8 on windows XP (unfortunately still a necessity for us)

    Alberto.