0

I'm trying to create a new service (AppOneTest) on windows 2016, which should be available in services.msc and whenever the service starts (AppOneTest) it should start the process explorer

$ServiceName = "ApplicationOneTest"
$ServiceDisplayName = "AppOneTest"
$ServiceDescription = "This launches process explorer"
$ExecutablePath = "C:\processexp\procexp64.exe"

$ScriptPath = "C:\Backup\LaunchScript.ps1" $LaunchScript = @" # Script to launch processexp & '$ExecutablePath' "@ $LaunchScript | Out-File -FilePath $ScriptPath -Encoding utf8

New-Service -Name $ServiceName -DisplayName $ServiceDisplayName -Description $ServiceDescription -BinaryPathName "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File $ScriptPath" -StartupType Automatic

sc.exe config $ServiceName obj= "domain\username" password= "******" type= interact

Start-Service -Name $ServiceName

This script will try to create a service and will try to start that service. However, when I try to start the service which was created, it's stuck and never starts.

Any assistance is much appreciated.

harrymc
  • 498,455
mohan
  • 21

2 Answers2

0

You're trying to run an interactive system service. This is not recommended and there may be some complexities to solve in addition to my answer. Note that not every program is made to run using the system service protocol, and I really doubt that Explorer is suitable. You might find the utilities I list in this answer to be useful in creating ad-hoc services from such unsuitable programs : srvany-ng and NSSM.

At a minimum, you need to have the service run under the same user account as the logged-in user. In PowerShell, this means using a PSCredential object that contains the user credentials.

Example code:

$username = "username"
$password = "password"
$securepassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($username, $securepassword)

New-Service [...] -Credential $cred

References:

harrymc
  • 498,455
0

Interactive services are mostly ineffective as of Windows Vista and should not be used.

Service programs must conform to a specific interface, they must interact with the Service Control Manager, signalling their status and whatnot. Process Explorer or any other average application would not do this.

You can use a service wrapper like NSSM to run arbitrary programs as services. This is a popular solution for Java server applications.


To run your UI tests, you should instead use a VM with auto-login configured and display sleep/locking disabled, and then run your tests in the regular session that’s always open.

user219095
  • 65,551