我希望使用将文件从一台服务器复制到另一台服务器--任何自动化进程。
这两台服务器都在不同的环境中,,我可以访问这两个环境。
例如,server1 in env1,server2 in env2
我想将文件从server1 C:\test\copy.txt 复制到 server2 C:\test\
注:使用WindowsServer.
我在下面添加了代码,如果两个服务器位于同一个VPN中,我可以复制文件,但在我的场景中,这两个服务器都位于不同的VPN中。
public void copyFile()
{
IntPtr admin_token = default(IntPtr);
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
WindowsIdentity wid_admin = null;
WindowsImpersonationContext wic = null;
try
{
Console.WriteLine("Copying file...");
if (LogonUser("LocalUsername", "LocalDomain", "LocalPass", 9, 0, ref admin_token) != 0)
{
wid_admin = new WindowsIdentity(admin_token);
wic = wid_admin.Impersonate();
System.IO.File.Copy("C:\\test\\copy.txt", "\\\\Server2\\test\\copy.txt", true);
Console.WriteLine("Copy succeeded");
}
else
{
Console.WriteLine("Copy Failed");
}
}
catch (System.Exception se)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine(ret.ToString(), "Error code: " + ret.ToString());
Console.WriteLine(se.Message);
}
finally
{
if (wic != null)
{
wic.Undo();
}
Console.ReadLine();
}
}
}发布于 2017-01-04 11:41:30
发布于 2017-01-04 11:45:15
如果您在两个服务器上都有权限,并且都支持FTP服务器-- windows服务器上的IIS允许这样做--这可能是一种可能的解决方案。
有了FTP,您就可以编写一个简单的程序,它可以放在机器上,也可以放在另一台机器上。这将允许您在两个服务器之间移动文件。许多编程语言都支持FTP操作,C#通过FtpWebRequest类,文档可以找到这里。
发布于 2017-01-04 11:46:46
您可以编写一个脚本,如下所示:
net use \\Server2 /user:yourUser yourPassword
ROBOCOPY C:\test\copy.txt \\Server2\test\唯一需要的是与用户yourUser共享测试文件夹并执行脚本。
https://stackoverflow.com/questions/41462902
复制相似问题