Automating Installers .Net Config Files Scheduled Tasks And Services

This post is a continuation of the brain dump I started here.

Adding new settings to a .NET config file
Previously I showed how to manipulate the existing settings in a .NET/XML configuration file but adding new entries turned out to be a bit more typing  since you have to create the new xml element(s) and append them to the document.

For example to add the section in bold to the existing file:

<Configuration>
<CustomSettings>
<add key="ExistingSetting1Key" value="ExistingSetting1Value">
  <add key="Setting1Key" value="Setting1Value">
</CustomSettings
</Configuration>

You use the code below:

$file = 'C:\App\App.exe.config'
$xml = [xml](Get-Content $file)
$CustomSettings  = $xml.configuration.CustomSettings
$Setting1 = $xml.CreateElement("add")
$Setting1.SetAttribute("key", "SettingKey")
$Setting1.SetAttribute("value", "SettingValue")
$CustomSettings.AppendChild($Setting1 )
$xml.Save($file)

As you can see nothing really new if you are used to manipulating XML documents, just more typing.

Depending on the complexity of the xml and how much must be changed you could take a existing element, make a copy of it, manipulate it and then append it to the document as well.

Creating a scheduled task in using PowerShell
In case you did not know, Azure VMs recreate their NICs when you shut them down from the portal and I had a software component that was linked to a specific NIC. After starting up again it would loose the NIC you selected earlier. So as part of the automated deployment every time the machine boots up I have to update the configuration of my 3rd party component to select the correct NIC. One way to do this is to run a scheduled task at startup. Here is the PowerShell script to create a scheduled task, you need PowerShell 3.0 or higher and Windows 2012.

$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-File C:\Tools\SelectNIC.ps1 -NoProfile -WindowStyle Hidden'


$trigger = New-ScheduledTaskTrigger -AtStartup

Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "SelectNIC" -Description "Change the NIC used to match licence"

Sadly my server was Windows 2008 so here is the command to do it using schtask.exe.

schtasks /Create /TR "Powershell.exe -File C:\Tools\SelectNIC.ps1 -NoProfile -WindowStyle Hidden" /SC ONSTART /RU account /RP password /TN NameForTheTask /RL HIGHEST

This specific task needed admin permissions so I had to use /RU and /RP and also /RL HIGHEST parameters to run with the highest privilege. You can also use schtask.exe to import scheduled tasks from xml files, this can be handy if it becomes too cumbersome to fit into the command line.

Changing the user for a service using PowerShell
Here is a simple PowerShell script to change the account used to run a service.

$filter = 'Name=' + "'" + $ServiceName + "'" + ''
$service = Get-WMIObject -ComputerName $CompName -namespace  "root\cimv2" -class Win32_Service -Filter $filter
$service.Change $null, $null, $null, $null, $null, $null, $newAcct, $newPass, $null, $null, $null

$service.StopService
sleep 10
$service.StartService

This one seems pretty forward except the parameters of the Change method on the $Service object differs depending on your version of Windows and may be the version of WMI as well. To see the correct parameters for your environment you can use the snippet below. It will display the methods on the $Service object in grid.

$Service | Get-Member -MemberType  Method | Out-GridView

One more thing to look out for is the format of the username, it must be in the domain\user format when using domain accounts or .\username format for the local account.

Changing the startup type of a service
Using PowerShell to change the startup type of a service is very easy:

Set-Service yourservicename -startuptype "manual"

But Powershell cannot change the startup type to automatic delayed start, you have to use the sc.exe command to do that.

sc.exe \\machinename config "ServiceName" start= delayed-auto

Note you need the space between start= and delayed-auto and the account running the service must have login as a service rights.

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *