If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
  # Relaunch as an elevated process:
  Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
  exit
}
# Now running elevated so launch the script:
$endPath = "\" + $MyInvocation.MyCommand.Name
$scriptPath = $MyInvocation.MyCommand.Path.Replace($endPath, "")
$registryKeyName = Split-Path -Path $scriptPath -Leaf -Resolve
$scriptPathTemp = $scriptPath
While ($scriptPathTemp.Contains('\') -And -Not $registryKeyName.StartsWith("IPrAccTcp_Client") -And -Not $registryKeyName.StartsWith("IPrAccUdp_Client") -And -Not $registryKeyName.StartsWith("IPrAccTcp_Server")) {
  $scriptPathTemp = $scriptPathTemp.Substring(0, $scriptPathTemp.lastIndexOf('\'))
  $registryKeyName = Split-Path -Path $scriptPathTemp -Leaf -Resolve
} 

if (-Not $registryKeyName.StartsWith("IPrAccTcp_Client") -And -Not $registryKeyName.StartsWith("IPrAccUdp_Client") -And -Not $registryKeyName.StartsWith("IPrAccTcp_Server"))
{
  write-host "Fehler: Datei-Struktur erfüllt nicht die Anforderungen für eine Registrierung der IPrAcc-Anwendungen als Dienst." -ForegroundColor red
  pause
  exit  
}
$exeName = $registryKeyName + ".exe"
$srvAnyPathName = $scriptPath + "\srvany-ng_r64.exe"
$exePathName = $scriptPath + "\" + $exeName
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Services\" + $registryKeyName

$srvAnyExists = Test-Path $srvAnyPathName
$exeExists = Test-Path $exePathName
$regPathExists = Test-Path $registryPath
if ($srvAnyExists -eq $False) {
  write-host "Fehler: Ausführbare Datei" $srvAnyPathName "wurde nicht gefunden." -ForegroundColor red
  pause
  exit
}
if ($exeExists -eq $False) {
  write-host "Fehler: Ausführbare Datei" $exePathName "wurde nicht gefunden." -ForegroundColor red
  pause
  exit
}
if ($regPathExists -eq $True) {
  Remove-Item -Path $registryPath -Recurse -Force  | Out-Null
}

New-Item -Path $registryPath -Force  | Out-Null
New-ItemProperty -Path $registryPath -Name DisplayName -Value $registryKeyName -PropertyType string -Force | Out-Null
New-ItemProperty -Path $registryPath -Name ErrorControl -Value 1 -PropertyType dword -Force | Out-Null
New-ItemProperty -Path $registryPath -Name ImagePath -Value $srvAnyPathName -PropertyType string -Force | Out-Null
New-ItemProperty -Path $registryPath -Name ObjectName -Value "LocalSystem" -PropertyType string -Force | Out-Null
New-ItemProperty -Path $registryPath -Name Start -Value 2 -PropertyType dword -Force | Out-Null
New-ItemProperty -Path $registryPath -Name Type -Value 16 -PropertyType dword -Force | Out-Null
New-ItemProperty -Path $registryPath -Name Description -Value "Accellence Technologies IP remote Access: establish secure IP connections between protected objects and control centers" -PropertyType string -Force | Out-Null
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Services\" + $registryKeyName + "\Parameters"
New-Item -Path $registryPath -Force | Out-Null
New-ItemProperty -Path $registryPath -Name AppDirectory -Value $scriptPath -PropertyType string -Force | Out-Null
New-ItemProperty -Path $registryPath -Name Application -Value $exePathName -PropertyType string -Force | Out-Null
write-host $exeName "wurde erfolgreich als Dienst in die Windows-Registry eingetragen." -ForegroundColor green
write-host "Der Rechner muss neu gestartet werden, damit" $exeName "als Dienst ausgeführt wird!" -ForegroundColor yellow
pause

