Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function Rename-ServiceControl($instanceName, $newInstanceName) {
#Derive the current bin and data paths from the instance names
$binPath = "${Env:ProgramFiles(x86)}\Particular Software\$instanceName"
$dataPath = "${Env:ProgramData}\Particular\ServiceControl\$instanceName"
#Derive the new bin and data paths from the instance names
$newBinPath = "${Env:ProgramFiles(x86)}\Particular Software\$newInstanceName"
$newDataPath = "${Env:ProgramData}\Particular\ServiceControl\$newInstanceName"
#Check if folders already exist
if (Test-Path $newBinPath -PathType Any) {
throw "Desitnation folder for the binaries $newBinPath already exists."
}
if (Test-Path $newDataPath -PathType Any) {
throw "Desitnation folder for the data and logs $newDataPath already exists."
}
If (Get-Service $newInstanceName -ErrorAction SilentlyContinue) {
throw "Service $newInstanceName already exists. Pick a different name."
}
#Stop and delete the existing Windows service
If (Get-Service $instanceName -ErrorAction SilentlyContinue) {
Stop-Service $instanceName -ErrorAction Stop
sc.exe delete $instanceName
}
else
{
throw "Service $instanceName does not exist."
}
#Move bin and data
Rename-Item -Path $binPath -NewName $newBinPath -ErrorAction Stop
Rename-Item -Path $dataPath -NewName $newDataPath -ErrorAction Stop
#Check if we are dealing with ServiceControl or ServiceControl.Audit instance
$mainExePath = "${newBinPath}\ServiceControl.exe"
$auditExePath = "${newBinPath}\ServiceControl.Audit.exe"
if (Test-Path $mainExePath -PathType Any) {
$exePath = $mainExePath
} else {
$exePath = $auditExePath
}
#Update configuration file
#- disable ingestion of audit/error messages
#- update log and data paths to point to new locations
$configFile = "${exePath}.config"
[xml]$xmlDoc = Get-Content $configFile
foreach ($element in $xmlDoc.configuration.appSettings.add)
{
if ($element.key -eq "ServiceBus/AuditQueue") {
$element.value = "!disable"
}
if ($element.key -eq "ServiceBus/ErrorQueue") {
$element.value = "!disable"
}
if ($element.key -eq "ServiceControl.Audit/LogPath") {
$element.value = "$newDataPath\Logs"
}
if ($element.key -eq "ServiceControl.Audit/DBPath") {
$element.value = "$newDataPath\DB"
}
if ($element.key -eq "ServiceControl/LogPath") {
$element.value = "$newDataPath\Logs"
}
if ($element.key -eq "ServiceControl/DBPath") {
$element.value = "$newDataPath\DB"
}
}
$xmlDoc.Save($configFile)
#Run setup routine
& "$exePath" /s --serviceName=${newInstanceName}
#Create new Windows Service using default credentials
New-Service -Name $newInstanceName -DisplayName $newInstanceName -StartupType Automatic -BinaryPathName "$exePath"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment