Startup Program on W2012 R2

How do I add a windows program to the startup list in W2012R2?

Also, is there a way to make a windows program run as a service?  I found an article on this, but the resource kit it referred to is not installed on my server.  If I could make the program a service that would be ideal.
No1CoderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Paris VicenteSystems and Comunications  Administrator Commented:
Hi No1Coder,

You can tweak this PowerShell Script to install or remove a service.

Install.ps1

$serviceName = "WorkManagerDemoService" 
$serviceDisplayName = "WorkManager Demo Service" 
$serviceDescription = "WorkManager Demo Service" 
$serviceExecutable = "WindowsService1.exe"

# verify if the service already exists, and if yes remove it first 
if (Get-Service $serviceName -ErrorAction SilentlyContinue) 
{ 
    "service already installed, stopping…" 
    # using WMI to remove Windows service because PowerShell does not have CmdLet for this 
    $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name=’$serviceName’" 
    $serviceToRemove | Stop-Service 
    $serviceToRemove.delete() 
    "service removed" 
} 
else 
{ 
    # just do nothing 
    "service does not exist" 
}

"installing service…"

# detect current execution directory 
$directoryPath = Split-Path $MyInvocation.MyCommand.Path

# creating credentials which can be used to run my windows service 
#$secpasswd = ConvertTo-SecureString "MyPa$$word" -AsPlainText -Force 
#$mycreds = New-Object System.Management.Automation.PSCredential (".\MyUserName", $secpasswd) 
# OR 
#$myCredentials = Get-Credential

$binaryPath = $directoryPath + "\" + $serviceExecutable

# creating widnows service using all provided parameters, -displayName $serviceName, -credential $myCredentials 
New-Service -name $serviceName -displayName $serviceDisplayName -binaryPathName $binaryPath -startupType Automatic -Description $serviceDescription

Start-Service -Name $serviceName

Get-Service $serviceName

"installation completed"

Pause

Open in new window



Uninstall.ps1
$serviceName = "WorkManagerDemoService"

# verify if the service already exists, and if yes remove it 
if (Get-Service $serviceName -ErrorAction SilentlyContinue) 
{ 
    "service already installed, stopping…" 
    # using WMI to remove Windows service because PowerShell does not have CmdLet for this 
    $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name=’$serviceName’" 
    $serviceToRemove | Stop-Service 
    $serviceToRemove.delete() 
    "service removed" 
} 
else 
{ 
    # just do nothing 
    "service does not exist" 
}

Pause

Open in new window


Hope it helps.
0
No1CoderAuthor Commented:
I hard coded the directory, changed the exe name, etc.  When I execute I get the error below.

New-Service : Service 'WebPingService (WebPingService)' cannot be created due to the following error: Access is denied
At line:1 char:1
+ New-Service -name $serviceName -displayName $serviceDisplayName -binaryPathName  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (WebPingService:String) [New-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotNewService,Microsoft.PowerShell.Commands.NewServiceCommand

The program is at the target location and is not running.

I have never used powershell.  I copied the command to clipboard, opened powershell, and pasted in.
0
David Paris VicenteSystems and Comunications  Administrator Commented:
Open powershell as Administrator, then in powershell use this command:
set-executionpolicy -executionpolicy unresctricted

Then try to run again the script, you will need admin rights in that machine.

Let us know.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

No1CoderAuthor Commented:
That seemed to help.  I now see the service in the UI.  Says "starting".

The program in question is a windows forms program.  Will the UI appear once the service starts?
0
No1CoderAuthor Commented:
I am still not able to make this work.  The service gets created, but it will not start.  I tried to remove it with the script provided, but it won't remove.

PS D:\utilities\pingnet> d:\utilities\pingnet\uninstallservice.ps1
service already installed, stopping…
Get-WmiObject : Invalid query "select * from Win32_Service where name=’WebPingService’"
At D:\utilities\pingnet\uninstallservice.ps1:8 char:24
+     $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name=’$servic ...
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

You cannot call a method on a null-valued expression.
At D:\utilities\pingnet\uninstallservice.ps1:10 char:5
+     $serviceToRemove.delete()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

service removed


----------------
the script is running as administrator.  The name matches what I see under services.
0
David Paris VicenteSystems and Comunications  Administrator Commented:
First lets address the program to start for every user.

You can add a Reg key for start programs on start up.
Go to your registry and:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

In here create a String value and in the value data, write the path to the program you want to run o start up.

Now the powershell try this way:
$serviceName = "MYserviceName" 

# verify if the service already exists, and if yes remove it 
if (Get-Service $serviceName -ErrorAction SilentlyContinue) 
{ 
    "service already installed, stopping…" 
    # using WMI to remove Windows service because PowerShell does not have CmdLet for this 
    $serviceToRemove = Get-WmiObject -Class Win32_Service|Where-Object -Property Name -eq $serviceName
    $serviceToRemove | Stop-Service 
    $serviceToRemove.delete() 
    "service removed" 
} 
else 
{ 
    # just do nothing 
    "service does not exist" 
}

Pause

Open in new window




On the install Script line 25 "$directoryPath = Split-Path $MyInvocation.MyCommand.Path" change the variable to the physical path were is the program, something like this:
$directoryPath = c:\ProgramFiles\cmd.exe

Let me know if helped.
0
No1CoderAuthor Commented:
Thank you,  Making progress.

Using the registry key works, however the startup directory for my program must be set.  Is there another registry key for this?

The revised script works better.  The uninstall script works.

I modified the install script, and it created the service, but hangs when it tries to start the service.  I looked in task manager, and the task was there, but it was not visible on the screen.  When I killed the task, the script completed with errors:
PS C:\Windows\system32> Start-Service -Name $serviceName
Start-Service : Service 'WebPingService (WebPingService)' cannot be started due to the following error: Cannot start
service WebPingService on computer '.'.
At line:1 char:1
+ Start-Service -Name $serviceName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
   ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand




This is a windows forms application.  Is that ok to run as service?
I also tried to start the service from the services admin function. This gives me an error message "Error 1053:  The service did not start or control request in a timely way."
0
David Paris VicenteSystems and Comunications  Administrator Commented:
But that Form is called by an executable program?
If not you can´t run as a service.
If not you can create a batch file and run the batch as a service to call the Form.
0
No1CoderAuthor Commented:
Didn't understand first comment.

This is a windows forms application.  (exe). When I run the executable, it shows a window on my desktop.

Is there a way to set setup directory in registry when doing startup.  If so, my problem is solved.
0
David Paris VicenteSystems and Comunications  Administrator Commented:
The Startup Folder is in:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

Try it and let me know.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
No1CoderAuthor Commented:
I don't have this directory on my computer.

I need to specify a different folder for the program to run in.

This program was not installed using an install tool, it is just an exe that I copied to the desktop.

I tried calling a batch command file from the registry, but it doesn't work for some reason.

Just trying to get the program to run when the server re-boots.  If I specify the exe in the registry, it runs, but in the wrong directory.
0
David Paris VicenteSystems and Comunications  Administrator Commented:
Did you enable the option to show all files and folders?

Open Explorer, press ALT ->TOOLS->Folder Options-> Select the View tab.
Then in Files and Folders->Hidden files and folders-> Select show hidden files, folders, and files.
0
No1CoderAuthor Commented:
Okay.  I got it to work.

I had to put a shortcut in the folder mentioned above.  The program now starts after reboot, and in the correct directory.

Thanks for your assistance.
0
No1CoderAuthor Commented:
Never could get service to work, but was able to solve my problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Server OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.