0

how are you? I have a problem with credentials, I do not know how to pass password and user in a bat file. Please read the context.

I have created a windows service, that takes some configuration values from a bat file, using the args parameter.This service writes a txt file in the desired location, but Here you are some basic code of my windows service:

static void Main(string[] args)
    {  //args comes from the bat file

        CmdLineArgs cmdArgs = new CmdLineArgs(args);

        //pass the values for mydirectory from args

        AppConfig.NetworkDirectory = cmdArgs.GetArg("/mydirectory");

        writefile(AppConfig.NetworkDirectory,"text for file");

         if(couldntwriteinpath())
            { //...manage  error
                                     }
     }

The writefile procedure:

//this is a very simple way of doing this, so you can have an idea of what is going on
public static void writefile(string path, string text)
{ string savepathname=path+"\myfile.txt";
//uses the class File.writealltext function
  File.WriteAllText(savepathname, text);
 }

The bat file :

cd C:\Windows\System32

sc.exe stop MyService

sc.exe delete MyService

timeout 3

sc.exe create MyService binPath="\"C:\apps\MyService\MyService.exe\" /mydirectory"\\192.168.90.x\homes\writefolder\"" start= auto
 sc.exe start MyService
PAUSE

**The problem is that I receive an error: "The user name or password is incorrect".

Can you help me? I tried to do all stackoverflow answers like https://stackoverflow.com/questions/3854026/how-to-give-credentials-in-a-batch-script-that-copies-files-to-a-network-locatio,https://stackoverflow.com/questions/303045/connecting-to-a-network-folder-with-username-password-in-powershell, etc.. but did not work.

I want to insert the user and password in the bat. So, when calling the File.WriteAllText procedure, it takes the credentials also.

Thanks

Community
  • 1
  • 1
Theman
  • 3
  • 3

1 Answers1

0

If I understand, the File.WriteAllText procedure is run by the service, but it hasn't credentials to write to the destination file. So you want to start the service with the credentials needed to perform such task.

You may set credentials for the service using the obj= and password= switches of the sc command, as in

@echo off

pushd %__APPDIR__% & rem cd C:\Windows\System32

setlocal
set "user=system\username"
set "pass=p@ssword"

sc.exe stop MyService

sc.exe delete MyService

timeout 3

sc.exe create MyService binPath="\"C:\apps\MyService\MyService.exe\" /mydirectory"\\192.168.90.x\homes\writefolder\"" start= auto obj= "%user%" password= "%pass%"
sc.exe start MyService

endlocal
popd
PAUSE

exit/B

Note: not sure if it works for you, usually sc switches expect a space behind the = sign, and your binPath= doesn't have.

elzooilogico
  • 1,659
  • 2
  • 17
  • 18
  • thanks for your answer!! I am going to try it and make you know. – Theman Sep 19 '16 at 02:49
  • hey, I had a litle problem, "the account name is invalid or does not exist, or the password is invalid for the account name specified". I already tried to use the user=system\mycomputerusername and also user=mycomputerusername. thanks – Theman Sep 19 '16 at 05:39
  • @ theman, Not sure if i understand you. When you say `mycomputerusername` is only the `username` or `machinename_username`. For instance, my computername is `lenovo` and my username `miguel` so in my case `obj= "lenovo\miguel" password= "p@ssword"` works perfectly. Please give me feedback, for if it doesn't work maybe the account hasn't elevated privileges. There are two more ways of doing this, using powershell or updating registry keys. We may use one of them to perform the desired task – elzooilogico Sep 19 '16 at 15:47
  • thanks a lot for your help! it really worked, I had to change it using these settings: user=mycomputername\username . Thanks again! – Theman Sep 20 '16 at 03:00