【发布时间】:2023-04-03 06:21:01
【问题描述】:
你好吗?我的凭据有问题,我不知道如何在 bat 文件中传递密码和用户。请阅读上下文。
我创建了一个 Windows 服务,它使用 args 参数从 bat 文件中获取一些配置值。该服务在所需位置写入一个 txt 文件,但是 这是我的 Windows 服务的一些基本代码:
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
}
}
writefile 过程:
//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);
}
bat 文件:
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
**问题是我收到一个错误:“用户名或密码不正确”。
你能帮帮我吗?我尝试做所有的 stackoverflow 答案,如 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 等。但没有奏效。
我想在 bat 中插入用户名和密码。因此,在调用 File.WriteAllText 过程时,它也会获取凭据。
谢谢
【问题讨论】:
-
信息很少。发布您的 bat 代码,以便我们了解发生了什么。
标签: batch-file cmd path windows-services